Search in sources :

Example 91 with Request

use of org.eclipse.jetty.server.Request in project gocd by gocd.

the class DeploymentContextWriterTest method shouldSkipRedirectWhenSiteUrlIsNotConfigured.

@Test
public void shouldSkipRedirectWhenSiteUrlIsNotConfigured() throws URISyntaxException {
    final ServerConfigService serverConfigService = mock(ServerConfigService.class);
    when(serverConfigService.hasAnyUrlConfigured()).thenReturn(false);
    Request req = new Request(mock(HttpChannel.class), mock(HttpInput.class));
    req.setUri(new HttpURI("/go/admin?tab=oAuth"));
    req.setServerName("url");
    req.setServerPort(8153);
    // req.setProtocol("http");
    DeploymentContextWriter writer = new DeploymentContextWriter() {

        @Override
        protected BaseUrlProvider getBaseUrlProvider(HttpServletRequest req) {
            return serverConfigService;
        }
    };
    writer.writeSecureSiteUrl(req);
    assertThat(req.getAttribute("secure_site"), is(nullValue()));
    assertThat(req.getAttribute("force_ssl"), is(nullValue()));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerConfigService(com.thoughtworks.go.server.service.ServerConfigService) HttpInput(org.eclipse.jetty.server.HttpInput) Request(org.eclipse.jetty.server.Request) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpChannel(org.eclipse.jetty.server.HttpChannel) HttpURI(org.eclipse.jetty.http.HttpURI) Test(org.junit.Test)

Example 92 with Request

use of org.eclipse.jetty.server.Request in project gocd by gocd.

the class GoVelocityViewTest method shouldSetAssetsPathVariableWhenRailsNewWithCompressedJavascriptsIsUsed.

@Test
public void shouldSetAssetsPathVariableWhenRailsNewWithCompressedJavascriptsIsUsed() throws Exception {
    SystemEnvironment systemEnvironment = mock(SystemEnvironment.class);
    when(systemEnvironment.useCompressedJs()).thenReturn(true);
    when(railsAssetsService.getAssetPath("application.js")).thenReturn("assets/application-digest.js");
    when(railsAssetsService.getAssetPath("application.css")).thenReturn("assets/application-digest.css");
    when(railsAssetsService.getAssetPath("vm/application.css")).thenReturn("assets/vm/application-digest.css");
    when(railsAssetsService.getAssetPath("css/application.css")).thenReturn("assets/css/application-digest.css");
    when(railsAssetsService.getAssetPath("g9/stage_bar_cancelled_icon.png")).thenReturn("assets/g9/stage_bar_cancelled_icon.png");
    when(railsAssetsService.getAssetPath("spinner.gif")).thenReturn("assets/spinner.gif");
    when(railsAssetsService.getAssetPath("cruise.ico")).thenReturn("assets/cruise.ico");
    GoVelocityView view = spy(new GoVelocityView(systemEnvironment));
    doReturn(railsAssetsService).when(view).getRailsAssetsService();
    doReturn(versionInfoService).when(view).getVersionInfoService();
    doReturn(pluginInfoFinder).when(view).getPluginInfoFinder();
    Request servletRequest = mock(Request.class);
    when(servletRequest.getSession()).thenReturn(mock(HttpSession.class));
    view.exposeHelpers(velocityContext, servletRequest);
    assertThat(velocityContext.get(GoVelocityView.CONCATENATED_JAVASCRIPT_FILE_PATH), is("assets/application-digest.js"));
    assertThat(velocityContext.get(GoVelocityView.CONCATENATED_APPLICATION_CSS_FILE_PATH), is("assets/application-digest.css"));
    assertThat(velocityContext.get(GoVelocityView.CONCATENATED_VM_APPLICATION_CSS_FILE_PATH), is("assets/vm/application-digest.css"));
    assertThat(velocityContext.get(GoVelocityView.CONCATENATED_CSS_APPLICATION_CSS_FILE_PATH), is("assets/css/application-digest.css"));
    assertThat(velocityContext.get(GoVelocityView.CONCATENATED_STAGE_BAR_CANCELLED_ICON_FILE_PATH), is("assets/g9/stage_bar_cancelled_icon.png"));
    assertThat(velocityContext.get(GoVelocityView.CONCATENATED_SPINNER_ICON_FILE_PATH), is("assets/spinner.gif"));
    assertThat(velocityContext.get(GoVelocityView.CONCATENATED_CRUISE_ICON_FILE_PATH), is("assets/cruise.ico"));
    assertThat(velocityContext.get(GoVelocityView.PATH_RESOLVER), is(railsAssetsService));
}
Also used : SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) HttpSession(javax.servlet.http.HttpSession) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.Test)

Example 93 with Request

use of org.eclipse.jetty.server.Request in project airlift by airlift.

the class TestDelimitedRequestLog method testTraceTokenHeader.

@Test
public void testTraceTokenHeader() throws Exception {
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    TraceTokenManager tokenManager = new TraceTokenManager();
    InMemoryEventClient eventClient = new InMemoryEventClient();
    DelimitedRequestLog logger = new DelimitedRequestLog(file.getAbsolutePath(), 1, 256, Long.MAX_VALUE, tokenManager, eventClient, new SystemCurrentTimeMillisProvider(), false);
    String token = "test-trace-token";
    when(request.getHeader(TRACETOKEN_HEADER)).thenReturn(token);
    // log a request without a token set by tokenManager
    logger.log(request, response);
    // create and set a new token with tokenManager
    tokenManager.createAndRegisterNewRequestToken();
    logger.log(request, response);
    // clear the token HTTP header
    when(request.getHeader(TRACETOKEN_HEADER)).thenReturn(null);
    logger.log(request, response);
    logger.stop();
    List<Object> events = eventClient.getEvents();
    assertEquals(events.size(), 3);
    // first two events should have the token set from the header
    for (int i = 0; i < 2; i++) {
        assertEquals(((HttpRequestEvent) events.get(i)).getTraceToken(), token);
    }
    // last event should have the token set by the tokenManager
    assertEquals(((HttpRequestEvent) events.get(2)).getTraceToken(), tokenManager.getCurrentRequestToken());
}
Also used : Response(org.eclipse.jetty.server.Response) InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Request(org.eclipse.jetty.server.Request) TraceTokenManager(io.airlift.tracetoken.TraceTokenManager) Test(org.testng.annotations.Test)

Example 94 with Request

use of org.eclipse.jetty.server.Request in project airlift by airlift.

the class TestDelimitedRequestLog method testXForwardedForSkipPrivateAddresses.

@Test
public void testXForwardedForSkipPrivateAddresses() throws Exception {
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    String clientIp = "1.1.1.1";
    when(request.getRemoteAddr()).thenReturn("9.9.9.9");
    when(request.getHeaders("X-FORWARDED-FOR")).thenReturn(Collections.enumeration(ImmutableList.of(clientIp, "192.168.1.2, 172.16.0.1", "169.254.1.2, 127.1.2.3", "10.1.2.3")));
    InMemoryEventClient eventClient = new InMemoryEventClient();
    DelimitedRequestLog logger = new DelimitedRequestLog(file.getAbsolutePath(), 1, 256, Long.MAX_VALUE, null, eventClient, false);
    logger.log(request, response);
    logger.stop();
    List<Object> events = eventClient.getEvents();
    assertEquals(events.size(), 1);
    HttpRequestEvent event = (HttpRequestEvent) events.get(0);
    assertEquals(event.getClientAddress(), clientIp);
}
Also used : Response(org.eclipse.jetty.server.Response) InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Request(org.eclipse.jetty.server.Request) Test(org.testng.annotations.Test)

Example 95 with Request

use of org.eclipse.jetty.server.Request in project airlift by airlift.

the class TestDelimitedRequestLog method testNoXForwardedFor.

@Test
public void testNoXForwardedFor() throws Exception {
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    String clientIp = "1.1.1.1";
    when(request.getRemoteAddr()).thenReturn(clientIp);
    InMemoryEventClient eventClient = new InMemoryEventClient();
    DelimitedRequestLog logger = new DelimitedRequestLog(file.getAbsolutePath(), 1, 256, Long.MAX_VALUE, null, eventClient, false);
    logger.log(request, response);
    logger.stop();
    List<Object> events = eventClient.getEvents();
    assertEquals(events.size(), 1);
    HttpRequestEvent event = (HttpRequestEvent) events.get(0);
    assertEquals(event.getClientAddress(), clientIp);
}
Also used : Response(org.eclipse.jetty.server.Response) InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Request(org.eclipse.jetty.server.Request) Test(org.testng.annotations.Test)

Aggregations

Request (org.eclipse.jetty.server.Request)297 HttpServletRequest (javax.servlet.http.HttpServletRequest)250 HttpServletResponse (javax.servlet.http.HttpServletResponse)225 Test (org.junit.Test)177 IOException (java.io.IOException)169 ServletException (javax.servlet.ServletException)150 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)147 CountDownLatch (java.util.concurrent.CountDownLatch)66 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)64 InterruptedIOException (java.io.InterruptedIOException)45 Server (org.eclipse.jetty.server.Server)41 InputStream (java.io.InputStream)37 AtomicReference (java.util.concurrent.atomic.AtomicReference)35 ByteArrayInputStream (java.io.ByteArrayInputStream)27 Response (org.eclipse.jetty.client.api.Response)27 Result (org.eclipse.jetty.client.api.Result)27 ServletInputStream (javax.servlet.ServletInputStream)24 ByteArrayOutputStream (java.io.ByteArrayOutputStream)23 ServletOutputStream (javax.servlet.ServletOutputStream)23 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)23