Java Code Examples for org.openqa.selenium.remote.http.HttpRequest#setHeader()
The following examples show how to use
org.openqa.selenium.remote.http.HttpRequest#setHeader() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JreAppServer.java From selenium with Apache License 2.0 | 6 votes |
@Override public String create(Page page) { try { byte[] data = new Json() .toJson(ImmutableMap.of("content", page.toString())) .getBytes(UTF_8); HttpClient client = HttpClient.Factory.createDefault().createClient(new URL(whereIs("/"))); HttpRequest request = new HttpRequest(HttpMethod.POST, "/common/createPage"); request.setHeader(CONTENT_TYPE, JSON_UTF_8.toString()); request.setContent(bytes(data)); HttpResponse response = client.execute(request); return string(response); } catch (IOException ex) { throw new RuntimeException(ex); } }
Example 2
Source File: JettyServerTest.java From selenium with Apache License 2.0 | 6 votes |
@Test public void shouldDisableAllowOrigin() { // TODO: Server setup Server<?> server = new JettyServer(emptyOptions, req -> new HttpResponse()).start(); // TODO: Client setup URL url = server.getUrl(); HttpClient client = HttpClient.Factory.createDefault().createClient(url); HttpRequest request = new HttpRequest(DELETE, "/session"); String exampleUrl = "http://www.example.com"; request.setHeader("Origin", exampleUrl); request.setHeader("Accept", "*/*"); HttpResponse response = client.execute(request); // TODO: Assertion assertEquals("Access-Control-Allow-Credentials should be null", null, response.getHeader("Access-Control-Allow-Credentials")); assertEquals("Access-Control-Allow-Origin should be null", null, response.getHeader("Access-Control-Allow-Origin")); }
Example 3
Source File: JettyServerTest.java From selenium with Apache License 2.0 | 6 votes |
@Test public void shouldAllowCORS() { // TODO: Server setup Config cfg = new CompoundConfig( new MapConfig(ImmutableMap.of("server", ImmutableMap.of("allow-cors", "true")))); BaseServerOptions options = new BaseServerOptions(cfg); assertTrue("Allow CORS should be enabled", options.getAllowCORS()); Server<?> server = new JettyServer(options, req -> new HttpResponse()).start(); // TODO: Client setup URL url = server.getUrl(); HttpClient client = HttpClient.Factory.createDefault().createClient(url); HttpRequest request = new HttpRequest(DELETE, "/session"); String exampleUrl = "http://www.example.com"; request.setHeader("Origin", exampleUrl); request.setHeader("Accept", "*/*"); HttpResponse response = client.execute(request); // TODO: Assertion assertEquals("Access-Control-Allow-Credentials should be true", "true", response.getHeader("Access-Control-Allow-Credentials")); assertEquals("Access-Control-Allow-Origin should be equal to origin in request header", exampleUrl, response.getHeader("Access-Control-Allow-Origin")); }
Example 4
Source File: ProtocolHandshake.java From selenium with Apache License 2.0 | 5 votes |
private Optional<Result> createSession(HttpClient client, InputStream newSessionBlob, long size) { // Create the http request and send it HttpRequest request = new HttpRequest(HttpMethod.POST, "/session"); HttpResponse response; long start = System.currentTimeMillis(); request.setHeader(CONTENT_LENGTH, String.valueOf(size)); request.setHeader(CONTENT_TYPE, JSON_UTF_8.toString()); request.setContent(() -> newSessionBlob); response = client.execute(request); long time = System.currentTimeMillis() - start; // Ignore the content type. It may not have been set. Strictly speaking we're not following the // W3C spec properly. Oh well. Map<?, ?> blob; try { blob = new Json().toType(string(response), Map.class); } catch (JsonException e) { throw new WebDriverException( "Unable to parse remote response: " + string(response), e); } InitialHandshakeResponse initialResponse = new InitialHandshakeResponse( time, response.getStatus(), blob); return Stream.of( new W3CHandshakeResponse().getResponseFunction(), new JsonWireProtocolResponse().getResponseFunction()) .map(func -> func.apply(initialResponse)) .filter(Objects::nonNull) .findFirst(); }
Example 5
Source File: AbstractHttpCommandCodec.java From selenium with Apache License 2.0 | 5 votes |
@Override public HttpRequest encode(Command command) { String name = aliases.getOrDefault(command.getName(), command.getName()); CommandSpec spec = nameToSpec.get(name); if (spec == null) { throw new UnsupportedCommandException(command.getName()); } Map<String, ?> parameters = amendParameters(command.getName(), command.getParameters()); String uri = buildUri(name, command.getSessionId(), parameters, spec); HttpRequest request = new HttpRequest(spec.method, uri); if (HttpMethod.POST == spec.method) { String content = json.toJson(parameters); byte[] data = content.getBytes(UTF_8); request.setHeader(CONTENT_LENGTH, String.valueOf(data.length)); request.setHeader(CONTENT_TYPE, JSON_UTF_8.toString()); request.setContent(bytes(data)); } if (HttpMethod.GET == spec.method) { request.setHeader(CACHE_CONTROL, "no-cache"); } return request; }
Example 6
Source File: JettyAppServer.java From selenium with Apache License 2.0 | 5 votes |
@Override public String create(Page page) { try { byte[] data = new Json().toJson(singletonMap("content", page.toString())).getBytes(UTF_8); HttpClient client = HttpClient.Factory.createDefault().createClient(new URL(whereIs("/"))); HttpRequest request = new HttpRequest(HttpMethod.POST, "/common/createPage"); request.setHeader(CONTENT_TYPE, JSON_UTF_8.toString()); request.setContent(bytes(data)); HttpResponse response = client.execute(request); return string(response); } catch (IOException ex) { throw new RuntimeException(ex); } }
Example 7
Source File: JsonHttpCommandCodecTest.java From selenium with Apache License 2.0 | 5 votes |
@Test public void decodeRequestWithUtf16Encoding() { codec.defineCommand("num", POST, "/one"); byte[] data = "{\"char\":\"水\"}".getBytes(UTF_16); HttpRequest request = new HttpRequest(POST, "/one"); request.setHeader(CONTENT_TYPE, JSON_UTF_8.withCharset(UTF_16).toString()); request.setHeader(CONTENT_LENGTH, String.valueOf(data.length)); request.setContent(bytes(data)); Command command = codec.decode(request); assertThat((String) command.getParameters().get("char")).isEqualTo("水"); }
Example 8
Source File: JsonHttpCommandCodecTest.java From selenium with Apache License 2.0 | 5 votes |
@Test public void decodingUsesUtf8IfNoEncodingSpecified() { codec.defineCommand("num", POST, "/one"); byte[] data = "{\"char\":\"水\"}".getBytes(UTF_8); HttpRequest request = new HttpRequest(POST, "/one"); request.setHeader(CONTENT_TYPE, JSON_UTF_8.withoutParameters().toString()); request.setHeader(CONTENT_LENGTH, String.valueOf(data.length)); request.setContent(bytes(data)); Command command = codec.decode(request); assertThat((String) command.getParameters().get("char")).isEqualTo("水"); }
Example 9
Source File: JsonHttpCommandCodecTest.java From selenium with Apache License 2.0 | 5 votes |
@Test public void treatsEmptyPathAsRoot_recognizedCommand() { codec.defineCommand("num", POST, "/"); byte[] data = "{\"char\":\"水\"}".getBytes(UTF_8); HttpRequest request = new HttpRequest(POST, ""); request.setHeader(CONTENT_TYPE, JSON_UTF_8.withoutParameters().toString()); request.setHeader(CONTENT_LENGTH, String.valueOf(data.length)); request.setContent(bytes(data)); Command command = codec.decode(request); assertThat(command.getName()).isEqualTo("num"); }
Example 10
Source File: JsonHttpCommandCodecTest.java From selenium with Apache License 2.0 | 5 votes |
@Test public void treatsNullPathAsRoot_recognizedCommand() { codec.defineCommand("num", POST, "/"); byte[] data = "{\"char\":\"水\"}".getBytes(UTF_8); HttpRequest request = new HttpRequest(POST, null); request.setHeader(CONTENT_TYPE, JSON_UTF_8.withoutParameters().toString()); request.setHeader(CONTENT_LENGTH, String.valueOf(data.length)); request.setContent(bytes(data)); Command command = codec.decode(request); assertThat(command.getName()).isEqualTo("num"); }
Example 11
Source File: JsonHttpCommandCodecTest.java From selenium with Apache License 2.0 | 5 votes |
@Test public void treatsEmptyPathAsRoot_unrecognizedCommand() { codec.defineCommand("num", GET, "/"); byte[] data = "{\"char\":\"水\"}".getBytes(UTF_8); HttpRequest request = new HttpRequest(POST, ""); request.setHeader(CONTENT_TYPE, JSON_UTF_8.withoutParameters().toString()); request.setHeader(CONTENT_LENGTH, String.valueOf(data.length)); request.setContent(bytes(data)); assertThatExceptionOfType(UnsupportedCommandException.class) .isThrownBy(() -> codec.decode(request)); }
Example 12
Source File: JsonHttpCommandCodecTest.java From selenium with Apache License 2.0 | 5 votes |
@Test public void treatsNullPathAsRoot_unrecognizedCommand() { codec.defineCommand("num", GET, "/"); byte[] data = "{\"char\":\"水\"}".getBytes(UTF_8); HttpRequest request = new HttpRequest(POST, null); request.setHeader(CONTENT_TYPE, JSON_UTF_8.withoutParameters().toString()); request.setHeader(CONTENT_LENGTH, String.valueOf(data.length)); request.setContent(bytes(data)); assertThatExceptionOfType(UnsupportedCommandException.class) .isThrownBy(() -> codec.decode(request)); }