Search in sources :

Example 6 with Resource

use of com.google.gerrit.httpd.raw.ResourceServlet.Resource 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 7 with Resource

use of com.google.gerrit.httpd.raw.ResourceServlet.Resource 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)

Example 8 with Resource

use of com.google.gerrit.httpd.raw.ResourceServlet.Resource 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 9 with Resource

use of com.google.gerrit.httpd.raw.ResourceServlet.Resource in project gerrit by GerritCodeReview.

the class ResourceServletTest method largeFileWithGzip.

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

Aggregations

Resource (com.google.gerrit.httpd.raw.ResourceServlet.Resource)9 FakeHttpServletResponse (com.google.gerrit.util.http.testutil.FakeHttpServletResponse)9 Path (java.nio.file.Path)9 Test (org.junit.Test)9 FakeHttpServletRequest (com.google.gerrit.util.http.testutil.FakeHttpServletRequest)3