Search in sources :

Example 1 with FakeHttpServletRequest

use of com.google.gerrit.util.http.testutil.FakeHttpServletRequest in project gerrit by GerritCodeReview.

the class AllRequestFilterFilterProxyTest method postponedLoading.

@Test
public void postponedLoading() throws Exception {
    FilterConfig config = mock(FilterConfig.class);
    HttpServletRequest req1 = new FakeHttpServletRequest();
    HttpServletRequest req2 = new FakeHttpServletRequest();
    HttpServletResponse res1 = new FakeHttpServletResponse();
    HttpServletResponse res2 = new FakeHttpServletResponse();
    FilterChain chain = mock(FilterChain.class);
    ArgumentCaptor<FilterChain> capturedChainA1 = ArgumentCaptor.forClass(FilterChain.class);
    ArgumentCaptor<FilterChain> capturedChainA2 = ArgumentCaptor.forClass(FilterChain.class);
    ArgumentCaptor<FilterChain> capturedChainB = ArgumentCaptor.forClass(FilterChain.class);
    AllRequestFilter filterA = mock(AllRequestFilter.class);
    AllRequestFilter filterB = mock(AllRequestFilter.class);
    InOrder inorder = inOrder(filterA, filterB, chain);
    AllRequestFilter.FilterProxy filterProxy = getFilterProxy();
    addFilter(filterA);
    filterProxy.init(config);
    filterProxy.doFilter(req1, res1, chain);
    inorder.verify(filterA).init(config);
    inorder.verify(filterA).doFilter(eq(req1), eq(res1), capturedChainA1.capture());
    capturedChainA1.getValue().doFilter(req1, res1);
    inorder.verify(chain).doFilter(req1, res1);
    // <-- Adds filter after filterProxy's init got called.
    addFilter(filterB);
    filterProxy.doFilter(req2, res2, chain);
    // after filterProxy's init finished. Nonetheless filterB gets initialized.
    inorder.verify(filterA).doFilter(eq(req2), eq(res2), capturedChainA2.capture());
    capturedChainA2.getValue().doFilter(req2, res2);
    // <-- This is crucial part. filterB got loaded
    inorder.verify(filterB).init(config);
    inorder.verify(filterB).doFilter(eq(req2), eq(res2), capturedChainB.capture());
    capturedChainB.getValue().doFilter(req2, res2);
    inorder.verify(chain).doFilter(req2, res2);
    filterProxy.destroy();
    inorder.verify(filterA).destroy();
    inorder.verify(filterB).destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FakeHttpServletRequest(com.google.gerrit.util.http.testutil.FakeHttpServletRequest) FakeHttpServletRequest(com.google.gerrit.util.http.testutil.FakeHttpServletRequest) FakeHttpServletResponse(com.google.gerrit.util.http.testutil.FakeHttpServletResponse) InOrder(org.mockito.InOrder) FilterChain(javax.servlet.FilterChain) FakeHttpServletResponse(com.google.gerrit.util.http.testutil.FakeHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterConfig(javax.servlet.FilterConfig) Test(org.junit.Test)

Example 2 with FakeHttpServletRequest

use of com.google.gerrit.util.http.testutil.FakeHttpServletRequest in project gerrit by GerritCodeReview.

the class ResourceServletTest method smallFileWithGzip.

@Test
public void smallFileWithGzip() throws Exception {
    Cache<Path, Resource> cache = newCache(1);
    Servlet servlet = new Servlet(fs, cache, true);
    String content = Strings.repeat("a", 100);
    writeFile("/foo", content);
    FakeHttpServletRequest req = request("/foo").addHeader("Accept-Encoding", "gzip");
    FakeHttpServletResponse res = new FakeHttpServletResponse();
    servlet.doGet(req, res);
    assertThat(res.getStatus()).isEqualTo(SC_OK);
    assertThat(res.getHeader("Content-Encoding")).isEqualTo("gzip");
    assertThat(gunzip(res.getActualBody())).isEqualTo(content);
    assertHasETag(res);
    assertCacheable(res, true);
}
Also used : Path(java.nio.file.Path) FakeHttpServletRequest(com.google.gerrit.util.http.testutil.FakeHttpServletRequest) FakeHttpServletResponse(com.google.gerrit.util.http.testutil.FakeHttpServletResponse) Resource(com.google.gerrit.httpd.raw.ResourceServlet.Resource) Test(org.junit.Test)

Example 3 with FakeHttpServletRequest

use of com.google.gerrit.util.http.testutil.FakeHttpServletRequest in project gerrit by GerritCodeReview.

the class ResourceServletTest method verySmallFileDoesntBotherWithGzip.

@Test
public void verySmallFileDoesntBotherWithGzip() throws Exception {
    Cache<Path, Resource> cache = newCache(1);
    Servlet servlet = new Servlet(fs, cache, true);
    writeFile("/foo", "foo1");
    FakeHttpServletRequest req = request("/foo").addHeader("Accept-Encoding", "gzip");
    FakeHttpServletResponse res = new FakeHttpServletResponse();
    servlet.doGet(req, res);
    assertThat(res.getStatus()).isEqualTo(SC_OK);
    assertThat(res.getHeader("Content-Encoding")).isNull();
    assertThat(res.getActualBodyString()).isEqualTo("foo1");
    assertHasETag(res);
    assertCacheable(res, true);
}
Also used : Path(java.nio.file.Path) FakeHttpServletRequest(com.google.gerrit.util.http.testutil.FakeHttpServletRequest) FakeHttpServletResponse(com.google.gerrit.util.http.testutil.FakeHttpServletResponse) Resource(com.google.gerrit.httpd.raw.ResourceServlet.Resource) Test(org.junit.Test)

Example 4 with FakeHttpServletRequest

use of com.google.gerrit.util.http.testutil.FakeHttpServletRequest in project gerrit by GerritCodeReview.

the class ParameterParserTest method parseAccessToken.

@Test
public void parseAccessToken() throws BadRequestException {
    FakeHttpServletRequest req = new FakeHttpServletRequest();
    req.setQueryString("query=status%3aopen&access_token=secr%65t");
    QueryParams qp = ParameterParser.getQueryParams(req);
    assertThat(qp.accessToken()).isEqualTo("secret");
    assertThat(qp.xdMethod()).isNull();
    assertThat(qp.xdContentType()).isNull();
    assertThat(qp.hasXdOverride()).isFalse();
    assertThat(qp.config()).isEmpty();
    assertThat(qp.params()).containsKey("query");
    assertThat(qp.params().get("query")).containsExactly("status:open");
    req = new FakeHttpServletRequest();
    req.setQueryString("access_token=secret");
    qp = ParameterParser.getQueryParams(req);
    assertThat(qp.accessToken()).isEqualTo("secret");
    assertThat(qp.xdMethod()).isNull();
    assertThat(qp.xdContentType()).isNull();
    assertThat(qp.hasXdOverride()).isFalse();
    assertThat(qp.config()).isEmpty();
    assertThat(qp.params()).isEmpty();
}
Also used : FakeHttpServletRequest(com.google.gerrit.util.http.testutil.FakeHttpServletRequest) QueryParams(com.google.gerrit.httpd.restapi.ParameterParser.QueryParams) Test(org.junit.Test)

Example 5 with FakeHttpServletRequest

use of com.google.gerrit.util.http.testutil.FakeHttpServletRequest in project gerrit by GerritCodeReview.

the class ParameterParserTest method parseQuery.

@Test
public void parseQuery() throws BadRequestException {
    FakeHttpServletRequest req = new FakeHttpServletRequest();
    req.setQueryString("query=status%3aopen");
    QueryParams qp = ParameterParser.getQueryParams(req);
    assertThat(qp.accessToken()).isNull();
    assertThat(qp.xdMethod()).isNull();
    assertThat(qp.xdContentType()).isNull();
    assertThat(qp.hasXdOverride()).isFalse();
    assertThat(qp.config()).isEmpty();
    assertThat(qp.params()).containsKey("query");
    assertThat(qp.params().get("query")).containsExactly("status:open");
}
Also used : FakeHttpServletRequest(com.google.gerrit.util.http.testutil.FakeHttpServletRequest) QueryParams(com.google.gerrit.httpd.restapi.ParameterParser.QueryParams) Test(org.junit.Test)

Aggregations

FakeHttpServletRequest (com.google.gerrit.util.http.testutil.FakeHttpServletRequest)18 Test (org.junit.Test)17 FakeHttpServletResponse (com.google.gerrit.util.http.testutil.FakeHttpServletResponse)12 FilterChain (javax.servlet.FilterChain)7 FilterConfig (javax.servlet.FilterConfig)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 InOrder (org.mockito.InOrder)6 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)3 Resource (com.google.gerrit.httpd.raw.ResourceServlet.Resource)3 QueryParams (com.google.gerrit.httpd.restapi.ParameterParser.QueryParams)3 Path (java.nio.file.Path)3 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 GerritApi (com.google.gerrit.extensions.api.GerritApi)1 Accounts (com.google.gerrit.extensions.api.accounts.Accounts)1 Config (com.google.gerrit.extensions.api.config.Config)1 Server (com.google.gerrit.extensions.api.config.Server)1 ServerInfo (com.google.gerrit.extensions.common.ServerInfo)1