Search in sources :

Example 1 with FakeHttpServletResponse

use of com.google.gerrit.util.http.testutil.FakeHttpServletResponse 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 FakeHttpServletResponse

use of com.google.gerrit.util.http.testutil.FakeHttpServletResponse 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 FakeHttpServletResponse

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

the class ResourceServletTest method smallFileWithoutClientCache.

@Test
public void smallFileWithoutClientCache() throws Exception {
    Cache<Path, Resource> cache = newCache(1);
    Servlet servlet = new Servlet(fs, cache, false, false);
    writeFile("/foo", "foo1");
    FakeHttpServletResponse res = new FakeHttpServletResponse();
    servlet.doGet(request("/foo"), res);
    assertThat(res.getStatus()).isEqualTo(SC_OK);
    assertThat(res.getActualBodyString()).isEqualTo("foo1");
    assertNotCacheable(res);
    // Miss on getIfPresent, miss on get.
    assertCacheHits(cache, 0, 2);
    res = new FakeHttpServletResponse();
    servlet.doGet(request("/foo"), res);
    assertThat(res.getStatus()).isEqualTo(SC_OK);
    assertThat(res.getActualBodyString()).isEqualTo("foo1");
    assertNotCacheable(res);
    assertCacheHits(cache, 1, 2);
    writeFile("/foo", "foo2");
    res = new FakeHttpServletResponse();
    servlet.doGet(request("/foo"), res);
    assertThat(res.getStatus()).isEqualTo(SC_OK);
    assertThat(res.getActualBodyString()).isEqualTo("foo1");
    assertNotCacheable(res);
    assertCacheHits(cache, 2, 2);
}
Also used : Path(java.nio.file.Path) FakeHttpServletResponse(com.google.gerrit.util.http.testutil.FakeHttpServletResponse) Resource(com.google.gerrit.httpd.raw.ResourceServlet.Resource) Test(org.junit.Test)

Example 4 with FakeHttpServletResponse

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

the class ResourceServletTest method smallFileWithRefresh.

@Test
public void smallFileWithRefresh() throws Exception {
    Cache<Path, Resource> cache = newCache(1);
    Servlet servlet = new Servlet(fs, cache, true);
    writeFile("/foo", "foo1");
    FakeHttpServletResponse res = new FakeHttpServletResponse();
    servlet.doGet(request("/foo"), res);
    assertThat(res.getStatus()).isEqualTo(SC_OK);
    assertThat(res.getActualBodyString()).isEqualTo("foo1");
    assertCacheable(res, true);
    assertHasETag(res);
    // Miss on getIfPresent, miss on get.
    assertCacheHits(cache, 0, 2);
    res = new FakeHttpServletResponse();
    servlet.doGet(request("/foo"), res);
    assertThat(res.getStatus()).isEqualTo(SC_OK);
    assertThat(res.getActualBodyString()).isEqualTo("foo1");
    assertCacheable(res, true);
    assertHasETag(res);
    assertCacheHits(cache, 1, 2);
    writeFile("/foo", "foo2");
    res = new FakeHttpServletResponse();
    servlet.doGet(request("/foo"), res);
    assertThat(res.getStatus()).isEqualTo(SC_OK);
    assertThat(res.getActualBodyString()).isEqualTo("foo2");
    assertCacheable(res, true);
    assertHasETag(res);
    // Hit, invalidate, miss.
    assertCacheHits(cache, 2, 3);
}
Also used : Path(java.nio.file.Path) FakeHttpServletResponse(com.google.gerrit.util.http.testutil.FakeHttpServletResponse) Resource(com.google.gerrit.httpd.raw.ResourceServlet.Resource) Test(org.junit.Test)

Example 5 with FakeHttpServletResponse

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

the class ResourceServletTest method notFoundWithRefresh.

@Test
public void notFoundWithRefresh() throws Exception {
    Cache<Path, Resource> cache = newCache(1);
    Servlet servlet = new Servlet(fs, cache, true);
    FakeHttpServletResponse res = new FakeHttpServletResponse();
    servlet.doGet(request("/notfound"), res);
    assertThat(res.getStatus()).isEqualTo(SC_NOT_FOUND);
    assertNotCacheable(res);
    assertCacheHits(cache, 0, 1);
    res = new FakeHttpServletResponse();
    servlet.doGet(request("/notfound"), res);
    assertThat(res.getStatus()).isEqualTo(SC_NOT_FOUND);
    assertNotCacheable(res);
    assertCacheHits(cache, 1, 1);
}
Also used : Path(java.nio.file.Path) FakeHttpServletResponse(com.google.gerrit.util.http.testutil.FakeHttpServletResponse) Resource(com.google.gerrit.httpd.raw.ResourceServlet.Resource) Test(org.junit.Test)

Aggregations

FakeHttpServletResponse (com.google.gerrit.util.http.testutil.FakeHttpServletResponse)18 Test (org.junit.Test)17 FakeHttpServletRequest (com.google.gerrit.util.http.testutil.FakeHttpServletRequest)12 Resource (com.google.gerrit.httpd.raw.ResourceServlet.Resource)9 Path (java.nio.file.Path)9 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 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 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 AuthRequest (com.google.gerrit.server.account.AuthRequest)1