Search in sources :

Example 11 with FakeHttpServletRequest

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

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

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

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

Example 15 with FakeHttpServletRequest

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

the class IndexServletTest method renderTemplate.

@Test
public void renderTemplate() throws Exception {
    Accounts accountsApi = mock(Accounts.class);
    when(accountsApi.self()).thenThrow(new AuthException("user needs to be authenticated"));
    Server serverApi = mock(Server.class);
    when(serverApi.getVersion()).thenReturn("123");
    when(serverApi.topMenus()).thenReturn(ImmutableList.of());
    ServerInfo serverInfo = new ServerInfo();
    serverInfo.defaultTheme = "my-default-theme";
    when(serverApi.getInfo()).thenReturn(serverInfo);
    Config configApi = mock(Config.class);
    when(configApi.server()).thenReturn(serverApi);
    GerritApi gerritApi = mock(GerritApi.class);
    when(gerritApi.accounts()).thenReturn(accountsApi);
    when(gerritApi.config()).thenReturn(configApi);
    String testCanonicalUrl = "foo-url";
    String testCdnPath = "bar-cdn";
    String testFaviconURL = "zaz-url";
    // Pick any known experiment enabled by default;
    String disabledDefault = ExperimentFeaturesConstants.UI_FEATURE_PATCHSET_COMMENTS;
    assertThat(ExperimentFeaturesConstants.DEFAULT_ENABLED_FEATURES).contains(disabledDefault);
    org.eclipse.jgit.lib.Config serverConfig = new org.eclipse.jgit.lib.Config();
    serverConfig.setStringList("experiments", null, "enabled", ImmutableList.of("NewFeature", "DisabledFeature"));
    serverConfig.setStringList("experiments", null, "disabled", ImmutableList.of("DisabledFeature", disabledDefault));
    ExperimentFeatures experimentFeatures = new ConfigExperimentFeatures(serverConfig);
    IndexServlet servlet = new IndexServlet(testCanonicalUrl, testCdnPath, testFaviconURL, gerritApi, experimentFeatures);
    FakeHttpServletResponse response = new FakeHttpServletResponse();
    servlet.doGet(new FakeHttpServletRequest(), response);
    String output = response.getActualBodyString();
    assertThat(output).contains("<!DOCTYPE html>");
    assertThat(output).contains("window.CANONICAL_PATH = '" + testCanonicalUrl);
    assertThat(output).contains("<link rel=\"preload\" href=\"" + testCdnPath);
    assertThat(output).contains("<link rel=\"icon\" type=\"image/x-icon\" href=\"" + testCanonicalUrl + "/" + testFaviconURL);
    assertThat(output).contains("window.INITIAL_DATA = JSON.parse(" + "'\\x7b\\x22\\/config\\/server\\/version\\x22: \\x22123\\x22, " + "\\x22\\/config\\/server\\/info\\x22: \\x7b\\x22default_theme\\x22:" + "\\x22my-default-theme\\x22\\x7d, \\x22\\/config\\/server\\/top-menus\\x22: " + "\\x5b\\x5d\\x7d');");
    ImmutableSet<String> enabledDefaults = ExperimentFeaturesConstants.DEFAULT_ENABLED_FEATURES.stream().filter(e -> !e.equals(disabledDefault)).collect(ImmutableSet.toImmutableSet());
    List<String> expectedEnabled = new ArrayList<>();
    expectedEnabled.add("NewFeature");
    expectedEnabled.addAll(enabledDefaults);
    assertThat(output).contains("window.ENABLED_EXPERIMENTS = JSON.parse('\\x5b\\x22" + String.join("\\x22,", expectedEnabled) + "\\x22\\x5d');</script>");
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) FakeHttpServletResponse(com.google.gerrit.util.http.testutil.FakeHttpServletResponse) ExperimentFeatures(com.google.gerrit.server.experiments.ExperimentFeatures) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) ServerInfo(com.google.gerrit.extensions.common.ServerInfo) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Accounts(com.google.gerrit.extensions.api.accounts.Accounts) Config(com.google.gerrit.extensions.api.config.Config) Server(com.google.gerrit.extensions.api.config.Server) ExperimentFeaturesConstants(com.google.gerrit.server.experiments.ExperimentFeaturesConstants) AuthException(com.google.gerrit.extensions.restapi.AuthException) FakeHttpServletRequest(com.google.gerrit.util.http.testutil.FakeHttpServletRequest) ConfigExperimentFeatures(com.google.gerrit.server.experiments.ConfigExperimentFeatures) GerritApi(com.google.gerrit.extensions.api.GerritApi) Mockito.mock(org.mockito.Mockito.mock) FakeHttpServletRequest(com.google.gerrit.util.http.testutil.FakeHttpServletRequest) Server(com.google.gerrit.extensions.api.config.Server) ServerInfo(com.google.gerrit.extensions.common.ServerInfo) Config(com.google.gerrit.extensions.api.config.Config) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) GerritApi(com.google.gerrit.extensions.api.GerritApi) ExperimentFeatures(com.google.gerrit.server.experiments.ExperimentFeatures) ConfigExperimentFeatures(com.google.gerrit.server.experiments.ConfigExperimentFeatures) FakeHttpServletResponse(com.google.gerrit.util.http.testutil.FakeHttpServletResponse) ConfigExperimentFeatures(com.google.gerrit.server.experiments.ConfigExperimentFeatures) Accounts(com.google.gerrit.extensions.api.accounts.Accounts) 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