Search in sources :

Example 16 with FakeHttpServletResponse

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

the class ResourceServletTest method largeFileBypassesCacheRegardlessOfRefreshParamter.

@Test
public void largeFileBypassesCacheRegardlessOfRefreshParamter() throws Exception {
    for (boolean refresh : Lists.newArrayList(true, false)) {
        Cache<Path, Resource> cache = newCache(1);
        Servlet servlet = new Servlet(fs, cache, refresh, 3);
        writeFile("/foo", "foo1");
        FakeHttpServletResponse res = new FakeHttpServletResponse();
        servlet.doGet(request("/foo"), res);
        assertThat(res.getStatus()).isEqualTo(SC_OK);
        assertThat(res.getActualBodyString()).isEqualTo("foo1");
        assertThat(res.getHeader("Last-Modified")).isNotNull();
        assertCacheable(res, refresh);
        assertHasLastModified(res);
        assertCacheHits(cache, 0, 1);
        writeFile("/foo", "foo1");
        res = new FakeHttpServletResponse();
        servlet.doGet(request("/foo"), res);
        assertThat(res.getStatus()).isEqualTo(SC_OK);
        assertThat(res.getActualBodyString()).isEqualTo("foo1");
        assertThat(res.getHeader("Last-Modified")).isNotNull();
        assertCacheable(res, refresh);
        assertHasLastModified(res);
        assertCacheHits(cache, 0, 2);
        writeFile("/foo", "foo2");
        res = new FakeHttpServletResponse();
        servlet.doGet(request("/foo"), res);
        assertThat(res.getStatus()).isEqualTo(SC_OK);
        assertThat(res.getActualBodyString()).isEqualTo("foo2");
        assertThat(res.getHeader("Last-Modified")).isNotNull();
        assertCacheable(res, refresh);
        assertHasLastModified(res);
        assertCacheHits(cache, 0, 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 17 with FakeHttpServletResponse

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

the class ResourceServletTest method notFoundWithoutRefresh.

@Test
public void notFoundWithoutRefresh() throws Exception {
    Cache<Path, Resource> cache = newCache(1);
    Servlet servlet = new Servlet(fs, cache, false);
    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)

Example 18 with FakeHttpServletResponse

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

the class ResourceServletTest method smallFileWithoutRefresh.

@Test
public void smallFileWithoutRefresh() throws Exception {
    Cache<Path, Resource> cache = newCache(1);
    Servlet servlet = new Servlet(fs, cache, false);
    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, false);
    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, false);
    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("foo1");
    assertCacheable(res, false);
    assertHasETag(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)

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