예제 #1
0
  @Test
  public void eventCreation() throws Exception {
    final AtomicReference<RequestHandler> requestHandlerRef = new AtomicReference<>();
    when(mockHttpListenerConfig.addRequestHandler(
            any(ListenerRequestMatcher.class), any(RequestHandler.class)))
        .then(
            new Answer<RequestHandlerManager>() {
              @Override
              public RequestHandlerManager answer(InvocationOnMock invocation) throws Throwable {
                requestHandlerRef.set((RequestHandler) invocation.getArguments()[1]);
                return null;
              }
            });
    usePath("/");

    assertThat(RequestContext.getEvent(), is(nullValue()));

    HttpResponseReadyCallback responseCallback = mock(HttpResponseReadyCallback.class);
    doAnswer(
            new Answer<Void>() {
              @Override
              public Void answer(InvocationOnMock invocation) throws Throwable {
                assertThat(RequestContext.getEvent(), not(nullValue()));
                return null;
              }
            })
        .when(responseCallback)
        .responseReady(any(HttpResponse.class), any(ResponseStatusCallback.class));

    HttpRequest request = buildGetRootRequest(HTTP_1_1);
    when(request.getHeaderValue("host")).thenReturn("localhost");
    HttpRequestContext requestContext = buildRequestContext(request);

    requestHandlerRef.get().handleRequest(requestContext, responseCallback);

    assertThat(RequestContext.getEvent(), is(nullValue()));
  }
예제 #2
0
 protected HttpRequest buildGetRootRequest(HttpProtocol protocol) {
   HttpRequest request = mock(HttpRequest.class);
   when(request.getHeaderValue(HOST)).thenReturn(null);
   when(request.getPath()).thenReturn("/");
   when(request.getUri()).thenReturn("/");
   when(request.getMethod()).thenReturn("GET");
   when(request.getProtocol()).thenReturn(protocol);
   return request;
 }