Example usage for org.openqa.selenium.remote.http HttpResponse getStatus

List of usage examples for org.openqa.selenium.remote.http HttpResponse getStatus

Introduction

In this page you can find the example usage for org.openqa.selenium.remote.http HttpResponse getStatus.

Prototype

public int getStatus() 

Source Link

Usage

From source file:io.appium.java_client.remote.AppiumProtocolHandShake.java

License:Apache License

private Optional<Result> createSession(HttpClient client, JsonObject params) throws IOException {
    // Create the http request and send it
    HttpRequest request = new HttpRequest(HttpMethod.POST, "/session");
    String content = params.toString();
    byte[] data = content.getBytes(UTF_8);

    request.setHeader(CONTENT_LENGTH, String.valueOf(data.length));
    request.setHeader(CONTENT_TYPE, JSON_UTF_8.toString());
    request.setContent(data);//from www.  ja v  a  2s  .  co  m
    HttpResponse response = client.execute(request, true);

    Map<?, ?> jsonBlob = new HashMap<>();
    String resultString = response.getContentString();
    try {
        jsonBlob = new JsonToBeanConverter().convert(Map.class, resultString);
    } catch (ClassCastException e) {
        return Optional.empty();
    } catch (JsonException e) {
        // Fine. Handle that below
    }

    // If the result looks positive, return the result.
    Object sessionId = jsonBlob.get("sessionId");
    Object value = jsonBlob.get("value");
    Object w3cError = jsonBlob.get("error");
    Object ossStatus = jsonBlob.get("status");
    Map<String, ?> capabilities = null;
    if (value != null && value instanceof Map) {
        capabilities = (Map<String, ?>) value;
    } else if (value != null && value instanceof Capabilities) {
        capabilities = ((Capabilities) capabilities).asMap();
    }

    if (response.getStatus() == HttpURLConnection.HTTP_OK && sessionId != null && capabilities != null) {
        Dialect dialect = ossStatus == null ? Dialect.W3C : Dialect.OSS;
        return Optional.of(new Result(dialect, String.valueOf(sessionId), capabilities));
    }

    // If the result was an error that we believe has to do with the remote end failing to start the
    // session, create an exception and throw it.
    Response tempResponse = null;
    if ("session not created".equals(w3cError)) {
        tempResponse = new Response(null);
        tempResponse.setStatus(SESSION_NOT_CREATED);
        tempResponse.setValue(jsonBlob);
    } else if (ossStatus instanceof Number && ((Number) ossStatus).intValue() == SESSION_NOT_CREATED) {
        tempResponse = new Response(null);
        tempResponse.setStatus(SESSION_NOT_CREATED);
        tempResponse.setValue(jsonBlob);
    }

    if (tempResponse != null) {
        new ErrorHandler().throwIfResponseFailed(tempResponse, 0);
    }

    // Otherwise, just return empty.
    return Optional.empty();
}

From source file:org.openqa.grid.web.servlet.DisplayHelpHandlerTest.java

License:Apache License

@Test
public void testGetHelpPageForStandalone() throws IOException {
    assertThat(handler.getHelperType()).isEqualTo("Standalone");

    HttpRequest request = new HttpRequest(GET, "/");
    HttpResponse response = new HttpResponse();
    handler.execute(request, response);/*  w  ww.ja v a 2  s . c  om*/
    assertThat(response.getStatus()).isEqualTo(HTTP_OK);

    String body = response.getContentString();
    assertThat(body).isNotNull().contains("Whoops! The URL specified routes to this help page.",
            "\"type\": \"Standalone\"", "\"consoleLink\": \"\\u002fwd\\u002fhub\"");
}

From source file:org.openqa.grid.web.servlet.DisplayHelpHandlerTest.java

License:Apache License

@Test
public void testGetHelpPageAsset() throws IOException {
    HttpResponse response = new HttpResponse();

    handler.execute(new HttpRequest(GET, "/assets/displayhelpservlet.css"), response);

    assertThat(response.getStatus()).isEqualTo(HTTP_OK);
    assertThat(response.getContentString()).isNotNull().contains("#help-heading #logo");
}

From source file:org.openqa.grid.web.servlet.DisplayHelpHandlerTest.java

License:Apache License

@Test
public void testNoSuchAsset() throws IOException {
    HttpResponse response = new HttpResponse();

    handler.execute(new HttpRequest(GET, "/assets/foo.bar"), response);

    assertThat(response.getStatus()).isEqualTo(HTTP_NOT_FOUND);
}

From source file:org.openqa.grid.web.servlet.DisplayHelpHandlerTest.java

License:Apache License

@Test
public void testAccessRoot() throws IOException {
    HttpResponse response = new HttpResponse();

    handler.execute(new HttpRequest(GET, "/"), response);

    assertThat(response.getStatus()).isEqualTo(HTTP_OK);
}