Search in sources :

Example 11 with FakeHttpServletResponse

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

the class AllRequestFilterFilterProxyTest method dynamicUnloading.

@Test
public void dynamicUnloading() throws Exception {
    FilterConfig config = mock(FilterConfig.class);
    HttpServletRequest req1 = new FakeHttpServletRequest();
    HttpServletRequest req2 = new FakeHttpServletRequest();
    HttpServletRequest req3 = new FakeHttpServletRequest();
    HttpServletResponse res1 = new FakeHttpServletResponse();
    HttpServletResponse res2 = new FakeHttpServletResponse();
    HttpServletResponse res3 = new FakeHttpServletResponse();
    Plugin plugin = mock(Plugin.class);
    FilterChain chain = mock(FilterChain.class);
    ArgumentCaptor<FilterChain> capturedChainA1 = ArgumentCaptor.forClass(FilterChain.class);
    ArgumentCaptor<FilterChain> capturedChainB1 = ArgumentCaptor.forClass(FilterChain.class);
    ArgumentCaptor<FilterChain> capturedChainB2 = ArgumentCaptor.forClass(FilterChain.class);
    AllRequestFilter filterA = mock(AllRequestFilter.class);
    AllRequestFilter filterB = mock(AllRequestFilter.class);
    AllRequestFilter.FilterProxy filterProxy = getFilterProxy();
    ReloadableRegistrationHandle<AllRequestFilter> handleFilterA = addFilter(filterA);
    ReloadableRegistrationHandle<AllRequestFilter> handleFilterB = addFilter(filterB);
    InOrder inorder = inOrder(filterA, filterB, chain);
    filterProxy.init(config);
    inorder.verify(filterA).init(config);
    inorder.verify(filterB).init(config);
    // Request #1 with filterA and filterB
    filterProxy.doFilter(req1, res1, chain);
    inorder.verify(filterA).doFilter(eq(req1), eq(res1), capturedChainA1.capture());
    capturedChainA1.getValue().doFilter(req1, res1);
    inorder.verify(filterB).doFilter(eq(req1), eq(res1), capturedChainB1.capture());
    capturedChainB1.getValue().doFilter(req1, res1);
    inorder.verify(chain).doFilter(req1, res1);
    // Unloading filterA
    handleFilterA.remove();
    filterProxy.onStopPlugin(plugin);
    // Cleaning up of filterA after it got unloaded
    inorder.verify(filterA).destroy();
    // Request #2 only with filterB
    filterProxy.doFilter(req2, res2, chain);
    inorder.verify(filterB).doFilter(eq(req2), eq(res2), capturedChainB2.capture());
    inorder.verify(filterA, never()).doFilter(eq(req2), eq(res2), any(FilterChain.class));
    capturedChainB2.getValue().doFilter(req2, res2);
    inorder.verify(chain).doFilter(req2, res2);
    // Unloading filterB
    handleFilterB.remove();
    filterProxy.onStopPlugin(plugin);
    // Cleaning up of filterA after it got unloaded
    inorder.verify(filterB).destroy();
    // Request #3 with no additional filters
    filterProxy.doFilter(req3, res3, chain);
    inorder.verify(chain).doFilter(req3, res3);
    inorder.verify(filterA, never()).doFilter(eq(req2), eq(res2), any(FilterChain.class));
    inorder.verify(filterB, never()).doFilter(eq(req2), eq(res2), any(FilterChain.class));
    filterProxy.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) Plugin(com.google.gerrit.server.plugins.Plugin) Test(org.junit.Test)

Example 12 with FakeHttpServletResponse

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

the class AllRequestFilterFilterProxyTest method twoFiltersNoBubbling.

@Test
public void twoFiltersNoBubbling() throws Exception {
    FilterConfig config = mock(FilterConfig.class);
    HttpServletRequest req = new FakeHttpServletRequest();
    HttpServletResponse res = new FakeHttpServletResponse();
    FilterChain chain = mock(FilterChain.class);
    AllRequestFilter filterA = mock(AllRequestFilter.class);
    AllRequestFilter filterB = mock(AllRequestFilter.class);
    AllRequestFilter.FilterProxy filterProxy = getFilterProxy();
    addFilter(filterA);
    addFilter(filterB);
    filterProxy.init(config);
    filterProxy.doFilter(req, res, chain);
    filterProxy.destroy();
    InOrder inorder = inOrder(filterA, filterB);
    inorder.verify(filterA).init(config);
    inorder.verify(filterB).init(config);
    inorder.verify(filterA).doFilter(eq(req), eq(res), any(FilterChain.class));
    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 13 with FakeHttpServletResponse

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

the class AllRequestFilterFilterProxyTest method singleFilterNoBubbling.

@Test
public void singleFilterNoBubbling() throws Exception {
    FilterConfig config = mock(FilterConfig.class);
    HttpServletRequest req = new FakeHttpServletRequest();
    HttpServletResponse res = new FakeHttpServletResponse();
    FilterChain chain = mock(FilterChain.class);
    AllRequestFilter filter = mock(AllRequestFilter.class);
    AllRequestFilter.FilterProxy filterProxy = getFilterProxy();
    addFilter(filter);
    filterProxy.init(config);
    filterProxy.doFilter(req, res, chain);
    filterProxy.destroy();
    InOrder inorder = inOrder(filter);
    inorder.verify(filter).init(config);
    inorder.verify(filter).doFilter(eq(req), eq(res), any(FilterChain.class));
    inorder.verify(filter).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 14 with FakeHttpServletResponse

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

the class ProjectBasicAuthFilterTest method setUp.

@Before
public void setUp() throws Exception {
    req = new FakeHttpServletRequest("gerrit.example.com", 80, "", "");
    res = new FakeHttpServletResponse();
    extIdKeyFactory = new ExternalIdKeyFactory(new ExternalIdKeyFactory.ConfigImpl(authConfig));
    extIdFactory = new ExternalIdFactory(extIdKeyFactory, authConfig);
    authRequestFactory = new AuthRequest.Factory(extIdKeyFactory);
    pwdVerifier = new PasswordVerifier(extIdKeyFactory, authConfig);
    authSuccessful = new AuthResult(AUTH_ACCOUNT_ID, extIdKeyFactory.create("username", AUTH_USER), false);
    doReturn(Optional.of(accountState)).when(accountCache).getByUsername(AUTH_USER);
    doReturn(Optional.of(accountState)).when(accountCache).get(AUTH_ACCOUNT_ID);
    doReturn(account).when(accountState).account();
    doReturn(true).when(account).isActive();
    doReturn(authSuccessful).when(accountManager).authenticate(any());
    doReturn(new WebSessionManager.Key(AUTH_COOKIE_VALUE)).when(webSessionManager).createKey(any());
    WebSessionManager.Val webSessionValue = new WebSessionManager.Val(AUTH_ACCOUNT_ID, 0L, false, null, 0L, "", "");
    doReturn(webSessionValue).when(webSessionManager).createVal(any(), any(), eq(false), any(), any(), any());
}
Also used : AuthRequest(com.google.gerrit.server.account.AuthRequest) FakeHttpServletRequest(com.google.gerrit.util.http.testutil.FakeHttpServletRequest) ExternalIdFactory(com.google.gerrit.server.account.externalids.ExternalIdFactory) AuthResult(com.google.gerrit.server.account.AuthResult) PasswordVerifier(com.google.gerrit.server.account.externalids.PasswordVerifier) ExternalIdKeyFactory(com.google.gerrit.server.account.externalids.ExternalIdKeyFactory) FakeHttpServletResponse(com.google.gerrit.util.http.testutil.FakeHttpServletResponse) Before(org.junit.Before)

Example 15 with FakeHttpServletResponse

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

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