private void usePath(String listenerPath) throws InitialisationException, RegistrationException { MuleContext mockMuleContext = mock(MuleContext.class); when(mockMuleContext.getRegistry()).thenReturn(mock(MuleRegistry.class)); when(mockMuleContext.getConfiguration()).thenReturn(mock(MuleConfiguration.class)); when(mockMuleContext.getConfiguration().getDefaultEncoding()).thenReturn("UTF-8"); final DefaultHttpListener httpListener = new DefaultHttpListener(); httpListener.setMuleContext(mockMuleContext); httpListener.setFlowConstruct(mockFlowConstruct); httpListener.setConfig(mockHttpListenerConfig); when(mockHttpListenerConfig.getFullListenerPath(anyString())) .thenReturn(new ListenerPath(null, listenerPath)); MessageProcessingManager messageProcessingManager = mock(MessageProcessingManager.class); doAnswer( new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { HttpMessageProcessorTemplate template = (HttpMessageProcessorTemplate) invocation.getArguments()[0]; template.sendResponseToClient(null, null); return null; } }) .when(messageProcessingManager) .processMessage(any(HttpMessageProcessorTemplate.class), any(MessageProcessContext.class)); when(mockMuleContext.getRegistry().lookupObject(MessageProcessingManager.class)) .thenReturn(messageProcessingManager); when(mockMuleContext.getRegistry().get(HTTP_LISTENER_CONNECTION_MANAGER)) .thenReturn(mockHttpListenerConnectionManager); httpListener.setPath(listenerPath); httpListener.initialise(); }
@Test public void noHostHeaderOn11Request() 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("/"); HttpRequest request = buildGetRootRequest(HTTP_1_1); HttpRequestContext requestContext = buildRequestContext(request); HttpResponseReadyCallback responseCallback = mock(HttpResponseReadyCallback.class); requestHandlerRef.get().handleRequest(requestContext, responseCallback); assertResponse( responseCallback, BAD_REQUEST.getStatusCode(), BAD_REQUEST.getReasonPhrase(), "Missing 'host' header"); }
private void useInvalidPath(String listenerPath) throws InitialisationException { final DefaultHttpListener httpListener = new DefaultHttpListener(); httpListener.setMuleContext(mockMuleContext); httpListener.setFlowConstruct(mockFlowConstruct); httpListener.setConfig(mockHttpListenerConfig); when(mockHttpListenerConfig.getFullListenerPath(anyString())) .thenReturn(new ListenerPath(null, listenerPath)); when(mockMuleContext.getRegistry().get(HTTP_LISTENER_CONNECTION_MANAGER)) .thenReturn(mockHttpListenerConnectionManager); httpListener.setPath(listenerPath); expectedException.expect(InitialisationException.class); httpListener.initialise(); }
@Test public void eventCreationWithInvalidPath() 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; } }); useInvalidPath("/"); assertThat(RequestContext.getEvent(), is(nullValue())); requestHandlerRef .get() .handleRequest(mock(HttpRequestContext.class), mock(HttpResponseReadyCallback.class)); assertThat(RequestContext.getEvent(), is(nullValue())); }
@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())); }