org.openqa.selenium.remote.http.HttpClient Java Examples

The following examples show how to use org.openqa.selenium.remote.http.HttpClient. 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: AddingNodesTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Before
public void setUpDistributor() throws MalformedURLException {
  tracer = DefaultTestTracer.createTracer();
  bus = new GuavaEventBus();

  handler = new CombinedHandler();
  externalUrl = new URL("http://example.com");
  HttpClient.Factory clientFactory = new RoutableHttpClientFactory(
    externalUrl,
    handler,
    HttpClient.Factory.createDefault());

  LocalSessionMap sessions = new LocalSessionMap(tracer, bus);
  Distributor local = new LocalDistributor(tracer, bus, clientFactory, sessions, null);
  handler.addHandler(local);
  distributor = new RemoteDistributor(tracer, clientFactory, externalUrl);

  wait = new FluentWait<>(new Object()).withTimeout(Duration.ofSeconds(2));
}
 
Example #2
Source File: ReactorHandlerTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Override
protected HttpClient.Factory createFactory() {
  return config -> {
    ReactorHandler reactorHandler = new ReactorHandler(config, httpClient);

    return new HttpClient() {
      @Override
      public WebSocket openSocket(HttpRequest request, WebSocket.Listener listener) {
        throw new UnsupportedOperationException("openSocket");
      }

      @Override
      public HttpResponse execute(HttpRequest req) throws UncheckedIOException {
        return reactorHandler.execute(req);
      }
    };
  };
}
 
Example #3
Source File: ProtocolConverterTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void newJwpSessionResponseShouldBeConvertedToW3CCorrectly() {
  Map<String, Object> w3cResponse = ImmutableMap.of(
    "value", ImmutableMap.of(
      "capabilities", ImmutableMap.of("cheese", "brie"),
      "sessionId", "i like cheese very much"));

  Map<String, Object> jwpNewSession = ImmutableMap.of(
    "desiredCapabilities", ImmutableMap.of());

  HttpClient client = mock(HttpClient.class);
  Mockito.when(client.execute(any())).thenReturn(new HttpResponse().setContent(asJson(w3cResponse)));

  ProtocolConverter converter = new ProtocolConverter(tracer, client, OSS, W3C);

  HttpResponse response = converter.execute(new HttpRequest(POST, "/session").setContent(asJson(jwpNewSession)));

  Map<String, Object> convertedResponse = json.toType(string(response), MAP_TYPE);
  assertThat(convertedResponse.get("status")).isEqualTo(0L);
  assertThat(convertedResponse.get("sessionId")).isEqualTo("i like cheese very much");
  assertThat(convertedResponse.get("value")).isEqualTo(ImmutableMap.of("cheese", "brie"));
}
 
Example #4
Source File: ProtocolConverterTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void newW3CSessionResponseShouldBeCorrectlyConvertedToJwp() {
  Map<String, Object> w3cNewSession = ImmutableMap.of(
    "capabilities", ImmutableMap.of());

  Map<String, Object> jwpResponse = ImmutableMap.of(
    "status", 0,
    "sessionId", "i like cheese very much",
    "value", ImmutableMap.of("cheese", "brie"));

  HttpClient client = mock(HttpClient.class);
  Mockito.when(client.execute(any())).thenReturn(new HttpResponse().setContent(asJson(jwpResponse)));

  ProtocolConverter converter = new ProtocolConverter(tracer, client, W3C, OSS);

  HttpResponse response = converter.execute(new HttpRequest(POST, "/session").setContent(asJson(w3cNewSession)));

  Map<String, Object> convertedResponse = json.toType(string(response), MAP_TYPE);
  Map<?, ?> value = (Map<?, ?>) convertedResponse.get("value");
  assertThat(value.get("capabilities"))
    .as("capabilities: " + convertedResponse)
    .isEqualTo(jwpResponse.get("value"));
  assertThat(value.get("sessionId"))
    .as("session id: " + convertedResponse)
    .isEqualTo(jwpResponse.get("sessionId"));
}
 
Example #5
Source File: Router.java    From selenium with Apache License 2.0 6 votes vote down vote up
public Router(
  Tracer tracer,
  HttpClient.Factory clientFactory,
  SessionMap sessions,
  Distributor distributor) {
  Require.nonNull("Tracer to use", tracer);
  Require.nonNull("HTTP client factory", clientFactory);

  this.sessions = Require.nonNull("Session map", sessions);
  this.distributor = Require.nonNull("Distributor", distributor);

  routes =
    combine(
      get("/status")
        .to(() -> new GridStatusHandler(new Json(), tracer, clientFactory, distributor)),
      sessions.with(new SpanDecorator(tracer, req -> "session_map")),
      distributor.with(new SpanDecorator(tracer, req -> "distributor")),
      matching(req -> req.getUri().startsWith("/session/"))
        .to(() -> new HandleSession(tracer, clientFactory, sessions)));
}
 
Example #6
Source File: DriverServiceSessionFactoryTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldInstantiateSessionIfEverythingIsOK() throws IOException {
  HttpClient httpClient = mock(HttpClient.class);
  when(httpClient.execute(any(HttpRequest.class))).thenReturn(
      new HttpResponse().setStatus(200).setContent(() -> new ByteArrayInputStream(
          "{ \"value\": { \"sessionId\": \"1\", \"capabilities\": {} } }".getBytes())));
  when(clientFactory.createClient(any(URL.class))).thenReturn(httpClient);

  DriverServiceSessionFactory factory = factoryFor("chrome", builder);

  Optional<ActiveSession> session = factory.apply(new CreateSessionRequest(
      ImmutableSet.of(Dialect.W3C), toPayload("chrome"), ImmutableMap.of()));

  assertThat(session).isNotEmpty();

  verify(builder, times(1)).build();
  verifyNoMoreInteractions(builder);
  verify(driverService, times(1)).start();
  verify(driverService, atLeastOnce()).getUrl();
  verifyNoMoreInteractions(driverService);
}
 
Example #7
Source File: JreAppServer.java    From selenium with Apache License 2.0 6 votes vote down vote up
@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 #8
Source File: ProtocolConverter.java    From selenium with Apache License 2.0 6 votes vote down vote up
public ProtocolConverter(
  Tracer tracer,
  HttpClient client,
  Dialect downstream,
  Dialect upstream) {
  this.tracer = Require.nonNull("Tracer", tracer);
  this.client = Require.nonNull("HTTP client", client);

  this.downstream = getCommandCodec(Require.nonNull("Downstream dialect", downstream));
  this.downstreamResponse = getResponseCodec(downstream);

  this.upstream = getCommandCodec(Require.nonNull("Upstream dialect", upstream));
  this.upstreamResponse = getResponseCodec(upstream);

  converter = new JsonToWebElementConverter(null);

  newSessionConverter = downstream == W3C ? this::createW3CNewSessionResponse : this::createJwpNewSessionResponse;
}
 
Example #9
Source File: ProtocolConverterTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void newJwpSessionResponseShouldBeCorrectlyConvertedToW3C() {
  Map<String, Object> jwpNewSession = ImmutableMap.of("desiredCapabilities", ImmutableMap.of("cheese", "brie"));

  Map<String, Object> w3cResponse = ImmutableMap.of(
    "value", ImmutableMap.of(
      "sessionId", "i like cheese very much",
      "capabilities", ImmutableMap.of("cheese", "brie")));

  HttpClient client = mock(HttpClient.class);
  Mockito.when(client.execute(any())).thenReturn(new HttpResponse().setContent(asJson(w3cResponse)));

  ProtocolConverter converter = new ProtocolConverter(tracer, client, OSS, W3C);

  HttpResponse response = converter.execute(new HttpRequest(POST, "/session").setContent(asJson(jwpNewSession)));

  Map<String, Object> convertedResponse = json.toType(string(response), MAP_TYPE);

  assertThat(convertedResponse.get("sessionId")).isEqualTo("i like cheese very much");
  assertThat(convertedResponse.get("status")).isEqualTo(0L);
  assertThat(convertedResponse.get("value")).isEqualTo(ImmutableMap.of("cheese", "brie"));
}
 
Example #10
Source File: LocalDistributor.java    From selenium with Apache License 2.0 6 votes vote down vote up
public LocalDistributor(
    Tracer tracer,
    EventBus bus,
    HttpClient.Factory clientFactory,
    SessionMap sessions,
    String registrationSecret) {
  super(tracer, clientFactory);
  this.tracer = Require.nonNull("Tracer", tracer);
  this.bus = Require.nonNull("Event bus", bus);
  this.clientFactory = Require.nonNull("HTTP client factory", clientFactory);
  this.sessions = Require.nonNull("Session map", sessions);
  this.registrationSecret = registrationSecret;

  bus.addListener(NODE_STATUS, event -> refresh(event.getData(NodeStatus.class)));
  bus.addListener(NODE_DRAIN_COMPLETE, event -> remove(event.getData(UUID.class)));
}
 
Example #11
Source File: DriverServiceSessionFactoryTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldNotInstantiateSessionIfRemoteEndReturnsInvalidResponse() throws IOException {
  HttpClient httpClient = mock(HttpClient.class);
  when(httpClient.execute(any(HttpRequest.class))).thenReturn(
      new HttpResponse().setStatus(200).setContent(() -> new ByteArrayInputStream(
          "Hello, world!".getBytes())));
  when(clientFactory.createClient(any(URL.class))).thenReturn(httpClient);

  DriverServiceSessionFactory factory = factoryFor("chrome", builder);

  Optional<ActiveSession> session = factory.apply(new CreateSessionRequest(
      ImmutableSet.of(Dialect.W3C), toPayload("chrome"), ImmutableMap.of()));

  assertThat(session).isEmpty();

  verify(builder, times(1)).build();
  verifyNoMoreInteractions(builder);
  verify(driverService, times(1)).start();
  verify(driverService, atLeastOnce()).getUrl();
  verify(driverService, times(1)).stop();
  verifyNoMoreInteractions(driverService);
}
 
Example #12
Source File: ProxyCdpIntoGrid.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<Consumer<Message>> apply(String uri, Consumer<Message> downstream) {
  Objects.requireNonNull(uri);
  Objects.requireNonNull(downstream);

  Optional<SessionId> sessionId = HttpSessionId.getSessionId(uri).map(SessionId::new);
  if (!sessionId.isPresent()) {
    return Optional.empty();
  }

  try {
    Session session = sessions.get(sessionId.get());

    HttpClient client = clientFactory.createClient(ClientConfig.defaultConfig().baseUri(session.getUri()));
    WebSocket upstream = client.openSocket(new HttpRequest(GET, uri), new ForwardingListener(downstream));

    return Optional.of(upstream::send);

  } catch (NoSuchSessionException e) {
    LOG.info("Attempt to connect to non-existant session: " + uri);
    return Optional.empty();
  }
}
 
Example #13
Source File: EndToEndTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAllowPassthroughForJWPMode() {
  HttpRequest request = new HttpRequest(POST, "/session");
  request.setContent(asJson(
      ImmutableMap.of(
          "desiredCapabilities", ImmutableMap.of(
              "browserName", "cheese"))));

  HttpClient client = clientFactory.createClient(server.getUrl());
  HttpResponse response = client.execute(request);

  assertEquals(200, response.getStatus());

  Map<String, Object> topLevel = json.toType(string(response), MAP_TYPE);

  // There should be a numeric status field
  assertEquals(topLevel.toString(), 0L, topLevel.get("status"));
  // The session id
  assertTrue(string(request), topLevel.containsKey("sessionId"));

  // And the value should be the capabilities.
  Map<?, ?> value = (Map<?, ?>) topLevel.get("value");
  assertEquals(string(request), "cheese", value.get("browserName"));
}
 
Example #14
Source File: RouterTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
  tracer = DefaultTestTracer.createTracer();
  bus = new GuavaEventBus();

  handler = new CombinedHandler();
  HttpClient.Factory clientFactory = new PassthroughHttpClient.Factory(handler);

  sessions = new LocalSessionMap(tracer, bus);
  handler.addHandler(sessions);

  distributor = new LocalDistributor(tracer, bus, clientFactory, sessions, null);
  handler.addHandler(distributor);

  router = new Router(tracer, clientFactory, sessions, distributor);
}
 
Example #15
Source File: JettyServerTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void exceptionsThrownByHandlersAreConvertedToAProperPayload() {
  Server<?> server = new JettyServer(
    emptyOptions,
    req -> {
      throw new UnableToSetCookieException("Yowza");
    });

  server.start();
  URL url = server.getUrl();
  HttpClient client = HttpClient.Factory.createDefault().createClient(url);
  HttpResponse response = client.execute(new HttpRequest(GET, "/status"));

  assertThat(response.getStatus()).isEqualTo(HTTP_INTERNAL_ERROR);

  Throwable thrown = null;
  try {
    thrown = ErrorCodec.createDefault().decode(new Json().toType(string(response), MAP_TYPE));
  } catch (IllegalArgumentException ignored) {
    fail("Apparently the command succeeded" + string(response));
  }

  assertThat(thrown).isInstanceOf(UnableToSetCookieException.class);
  assertThat(thrown.getMessage()).startsWith("Yowza");
}
 
Example #16
Source File: HttpCommandExecutor.java    From selenium with Apache License 2.0 6 votes vote down vote up
public HttpCommandExecutor(
    Map<String, CommandInfo> additionalCommands,
    URL addressOfRemoteServer,
    HttpClient.Factory httpClientFactory) {
  try {
    remoteServer = addressOfRemoteServer == null
        ? new URL(System.getProperty("webdriver.remote.server", "http://localhost:4444/"))
        : addressOfRemoteServer;
  } catch (MalformedURLException e) {
    throw new WebDriverException(e);
  }

  this.additionalCommands = additionalCommands;
  this.httpClientFactory = httpClientFactory;
  this.client = httpClientFactory.createClient(remoteServer);
}
 
Example #17
Source File: EndToEndTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAllowPassthroughForW3CMode() {
  HttpRequest request = new HttpRequest(POST, "/session");
  request.setContent(asJson(
      ImmutableMap.of(
          "capabilities", ImmutableMap.of(
              "alwaysMatch", ImmutableMap.of("browserName", "cheese")))));

  HttpClient client = clientFactory.createClient(server.getUrl());
  HttpResponse response = client.execute(request);

  assertEquals(200, response.getStatus());

  Map<String, Object> topLevel = json.toType(string(response), MAP_TYPE);

  // There should not be a numeric status field
  assertFalse(string(request), topLevel.containsKey("status"));

  // And the value should have all the good stuff in it: the session id and the capabilities
  Map<?, ?> value = (Map<?, ?>) topLevel.get("value");
  assertThat(value.get("sessionId")).isInstanceOf(String.class);

  Map<?, ?> caps = (Map<?, ?>) value.get("capabilities");
  assertEquals("cheese", caps.get("browserName"));
}
 
Example #18
Source File: LocalNodeFactory.java    From selenium with Apache License 2.0 6 votes vote down vote up
private static Collection<SessionFactory> createSessionFactory(
  Tracer tracer,
  HttpClient.Factory clientFactory,
  List<DriverService.Builder<?, ?>> builders,
  WebDriverInfo info) {
  ImmutableList.Builder<SessionFactory> toReturn = ImmutableList.builder();

  Capabilities caps = info.getCanonicalCapabilities();

  builders.stream()
    .filter(builder -> builder.score(caps) > 0)
    .forEach(builder -> {
      DriverService.Builder<?, ?> freePortBuilder = builder.usingAnyFreePort();
      toReturn.add(new DriverServiceSessionFactory(
        tracer,
        clientFactory,
        c -> freePortBuilder.score(c) > 0,
        freePortBuilder));
    });

  return toReturn.build();
}
 
Example #19
Source File: SessionLogsTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> getValueForPostRequest(URL serverUrl) throws Exception {
  String url = serverUrl + "/logs";
  HttpClient.Factory factory = HttpClient.Factory.createDefault();
  HttpClient client = factory.createClient(new URL(url));
  HttpResponse response = client.execute(new HttpRequest(HttpMethod.POST, url));
  Map<String, Object> map = new Json().toType(string(response), MAP_TYPE);
  return (Map<String, Object>) map.get("value");
}
 
Example #20
Source File: DriverServiceSessionFactory.java    From selenium with Apache License 2.0 5 votes vote down vote up
public DriverServiceSessionFactory(
    Tracer tracer,
    HttpClient.Factory clientFactory,
    Predicate<Capabilities> predicate,
    DriverService.Builder builder) {
  this.tracer = Require.nonNull("Tracer", tracer);
  this.clientFactory = Require.nonNull("HTTP client factory", clientFactory);
  this.predicate = Require.nonNull("Accepted capabilities predicate", predicate);
  this.builder = Require.nonNull("Driver service bulder", builder);
}
 
Example #21
Source File: AddNode.java    From selenium with Apache License 2.0 5 votes vote down vote up
AddNode(
    Tracer tracer,
    Distributor distributor,
    Json json,
    HttpClient.Factory httpFactory) {
  this.tracer = Require.nonNull("Tracer", tracer);
  this.distributor = Require.nonNull("Distributor", distributor);
  this.json = Require.nonNull("Json converter", json);
  this.httpFactory = Require.nonNull("HTTP Factory", httpFactory);
}
 
Example #22
Source File: RouterServer.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
protected void execute(Config config) {
  LoggingOptions loggingOptions = new LoggingOptions(config);
  Tracer tracer = loggingOptions.getTracer();

  NetworkOptions networkOptions = new NetworkOptions(config);
  HttpClient.Factory clientFactory = networkOptions.getHttpClientFactory(tracer);

  SessionMapOptions sessionsOptions = new SessionMapOptions(config);
  SessionMap sessions = sessionsOptions.getSessionMap();

  BaseServerOptions serverOptions = new BaseServerOptions(config);

  DistributorOptions distributorOptions = new DistributorOptions(config);
  URL distributorUrl = fromUri(distributorOptions.getDistributorUri());
  Distributor distributor = new RemoteDistributor(
      tracer,
      clientFactory,
      distributorUrl
  );

  GraphqlHandler graphqlHandler = new GraphqlHandler(distributor, serverOptions.getExternalUri());

  Route handler = Route.combine(
    new Router(tracer, clientFactory, sessions, distributor),
    Route.post("/graphql").to(() -> graphqlHandler),
    get("/readyz").to(() -> req -> new HttpResponse().setStatus(HTTP_NO_CONTENT)));

  Server<?> server = new NettyServer(serverOptions, handler, new ProxyCdpIntoGrid(clientFactory, sessions));
  server.start();

  BuildInfo info = new BuildInfo();
  LOG.info(String.format(
    "Started Selenium router %s (revision %s): %s",
    info.getReleaseLabel(),
    info.getBuildRevision(),
    server.getUrl()));
}
 
Example #23
Source File: ProtocolHandshake.java    From selenium with Apache License 2.0 5 votes vote down vote up
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 #24
Source File: NettyClientTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBeAbleToConnectToAUnixDomainSocketUrl() {
  ClientConfig config = ClientConfig.defaultConfig().baseUri(socket);
  HttpClient client = new NettyClient.Factory().createClient(config);

  String emphaticCheeseEnjoyment = "I like cheese!";
  responseText.set(emphaticCheeseEnjoyment);

  HttpResponse res = client.execute(new HttpRequest(GET, "/do-you-like-cheese"));

  assertThat(Contents.string(res)).isEqualTo(emphaticCheeseEnjoyment);
}
 
Example #25
Source File: JettyAppServer.java    From selenium with Apache License 2.0 5 votes vote down vote up
@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 #26
Source File: ProxyNodeCdp.java    From selenium with Apache License 2.0 5 votes vote down vote up
private Consumer<Message> createCdpEndPoint(URI uri, Consumer<Message> downstream) {
  Objects.requireNonNull(uri);

  LOG.info("Establishing CDP connection to " + uri);

  HttpClient client = clientFactory.createClient(ClientConfig.defaultConfig().baseUri(uri));
  WebSocket upstream = client.openSocket(new HttpRequest(GET, uri.toString()), new ForwardingListener(downstream));
  return upstream::send;
}
 
Example #27
Source File: AppServerTestBase.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void manifestHasCorrectMimeType() throws IOException {
  String url = server.whereIs("html5/test.appcache");
  HttpClient.Factory factory = HttpClient.Factory.createDefault();
  HttpClient client = factory.createClient(new URL(url));
  HttpResponse response = client.execute(new HttpRequest(HttpMethod.GET, url));

  System.out.printf("Content for %s was %s\n", url, string(response));

  assertTrue(StreamSupport.stream(response.getHeaders("Content-Type").spliterator(), false)
      .anyMatch(header -> header.contains(APPCACHE_MIME_TYPE)));
}
 
Example #28
Source File: NettyClient.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
public HttpClient with(Filter filter) {
  Require.nonNull("Filter", filter);

  // TODO: We should probably ensure that websocket requests are run through the filter.
  return new NettyClient(handler.with(filter), toWebSocket);
}
 
Example #29
Source File: JettyServerTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAllowAHandlerToBeRegistered() {
  Server<?> server = new JettyServer(
    emptyOptions,
    get("/cheese").to(() -> req -> new HttpResponse().setContent(utf8String("cheddar"))));

  server.start();
  URL url = server.getUrl();
  HttpClient client = HttpClient.Factory.createDefault().createClient(url);
  HttpResponse response = client.execute(new HttpRequest(GET, "/cheese"));

  assertEquals("cheddar", string(response));
}
 
Example #30
Source File: HandleSession.java    From selenium with Apache License 2.0 5 votes vote down vote up
private Callable<HttpHandler> loadSessionId(Tracer tracer, Span span, SessionId id) {
  return span.wrap(
    () -> {
      Session session = sessions.get(id);
        if (session instanceof HttpHandler) {
          return (HttpHandler) session;
        }
        HttpClient client = httpClientFactory.createClient(Urls.fromUri(session.getUri()));
        return new ReverseProxyHandler(tracer, client);
    }
  );
}