Example usage for org.springframework.mock.web MockFilterChain MockFilterChain

List of usage examples for org.springframework.mock.web MockFilterChain MockFilterChain

Introduction

In this page you can find the example usage for org.springframework.mock.web MockFilterChain MockFilterChain.

Prototype

public MockFilterChain() 

Source Link

Document

Register a single do-nothing Filter implementation.

Usage

From source file:org.sventon.web.filter.ConfigAuthorizationFilterTest.java

@Test
public void testDoFilterInternalApplicationConfiguredEditDisabled() throws Exception {
    final ConfigAuthorizationFilter filter = new ConfigAuthorizationFilter(application);

    final HttpServletRequest request = new MockHttpServletRequest();
    final HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
    final MockFilterChain filterChain = new MockFilterChain();

    application.setConfigured(true);//from   w  w w . ja va2s. c o  m
    application.setEditableConfig(false);

    response.sendRedirect("/repos/list");

    EasyMock.replay(response);
    filter.doFilterInternal(request, response, filterChain);
    EasyMock.verify(response);

    assertNull(filterChain.getRequest());
    assertNull(filterChain.getResponse());
}

From source file:com.gopivotal.cla.security.AdminEmailDomainFilterTest.java

@Test(expected = AccessDeniedException.class)
public void attemptAuthenticationNonValidAdminDomain() throws IOException, ServletException {
    Emails emails = new Emails();
    when(this.gitHubClient.getEmails()).thenReturn(emails);
    emails.add(new Email("email@other.domain", false, true));

    this.filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
}

From source file:at.molindo.notify.servlet.NotifyFilterTest.java

@Test
public void pull() throws Exception {
    new MockTest() {

        NotifyFilterBean filter;/* w w w.ja  v a2 s.  c  o m*/
        FilterChain mockFilterChain;
        MockFilterConfig config;

        @Override
        @SuppressWarnings(value = "NP_NULL_PARAM_DEREF_ALL_TARGETS_DANGEROUS", justification = "mocks accept null")
        protected void setup(EasyMockContext context) throws Exception {
            filter = new NotifyFilterBean();
            mockFilterChain = new MockFilterChain();
            config = new MockFilterConfig();
            config.addInitParameter(NotifyFilterBean.PARAMTER_BASE_URL, "http://www.example.com/");

            context.create(IPullChannel.class);

            expect(context.get(IPullChannel.class).getId()).andReturn(CHANNELID).anyTimes();
            expect(context.get(IPullChannel.class).getNotificationTypes()).andReturn(Type.TYPES_ALL).anyTimes();
            expect(context.get(IPullChannel.class).pull(eq(USERID),
                    anyObject(ConfigurableChannelPreferences.class))).andReturn(BODY);
            expect(context.get(IPullChannel.class).newDefaultPreferences())
                    .andReturn(new ConfigurableChannelPreferences());

            eq(USERID);
            reportMatcher(new IArgumentMatcher() {

                @Override
                public boolean matches(Object argument) {
                    ConfigurableChannelPreferences cPrefs = (ConfigurableChannelPreferences) argument;
                    return SECRET.equals(cPrefs.getParams().get(AbstractPullChannel.SECRET));
                }

                @Override
                public void appendTo(StringBuffer buffer) {
                    buffer.append("secret not included");
                }
            });
            expect(context.get(IPullChannel.class).isAuthorized(null, null)).andReturn(true);

            expect(context.get(IPullChannel.class).isConfigured(anyObject(Params.class))).andReturn(true);

            // toPullPath
            expect(context.get(IPullChannel.class).newDefaultPreferences())
                    .andReturn(new PushChannelPreferences());
        }

        @Override
        protected void test(EasyMockContext context) throws Exception {
            filter.setChannels(CollectionUtils.set(context.get(IPullChannel.class)));
            filter.init(config);

            MockHttpServletRequest request = new MockHttpServletRequest("GET",
                    "/notify/" + NotifyFilterBean.DEFAULT_PULL_PREFIX + "/" + CHANNELID + "/" + USERID);

            request.setServletPath("/notify");
            request.setParameter(AbstractPullChannel.SECRET.getName(), SECRET);

            MockHttpServletResponse response = new MockHttpServletResponse();

            filter.doFilter(request, response, mockFilterChain);

            assertEquals(200, response.getStatus());
            assertEquals(BODY, response.getContentAsString());

            Confirmation confirmation = new Confirmation(new Notification());
            assertEquals("http://www.example.com/notify/confirm/" + confirmation.getKey(),
                    filter.toConfirmPath(confirmation));

            assertEquals("http://www.example.com/notify/pull/test-channel/test-user",
                    filter.toPullPath(CHANNELID, USERID, new Params()));
        }

    }.run();
}

From source file:org.eclipse.virgo.snaps.core.SnapHostFilterTests.java

@Test
public void testRouteToSnap() throws ServletException, IOException {
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setServletPath("/hotels/booking");

    Snap snap = createMock(Snap.class);
    snap.handleRequest(request, response);
    expectLastCall();/*  ww  w  .  j a  va  2  s.c o  m*/

    this.stubRegistry.snaps.put("/hotels", snap);

    MockServletContext servletContext = new MockServletContext();
    MockFilterConfig config = new MockFilterConfig(servletContext);
    MockFilterChain chain = new MockFilterChain();

    replay(snap);
    TestFilter filter = new TestFilter();
    filter.init(config);
    filter.doFilter(request, response, chain);
    filter.destroy();
    verify(snap);
}

From source file:io.jmnarloch.spring.request.correlation.filter.RequestCorrelationFilterTest.java

@Test
public void shouldUseExistingCorrelationId() throws IOException, ServletException {

    // given/*from ww  w .  j  a  v  a 2  s  .  c om*/
    final String requestId = UUID.randomUUID().toString();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockFilterChain chain = new MockFilterChain();

    request.addHeader(RequestCorrelationConsts.HEADER_NAME, requestId);

    // when
    instance.doFilter(request, response, chain);

    // then
    final Object requestCorrelation = request.getAttribute(RequestCorrelationConsts.ATTRIBUTE_NAME);
    assertNotNull(requestCorrelation);
    assertEquals(requestId, ((RequestCorrelation) requestCorrelation).getRequestId());

    final String header = ((HttpServletRequest) chain.getRequest())
            .getHeader(RequestCorrelationConsts.HEADER_NAME);
    assertNotNull(header);
    assertEquals(requestId, header);
}

From source file:io.lavagna.web.security.SecurityFilterTest.java

@Test
public void test() throws IOException, ServletException {

    SecurityFilter sf = new SecurityFilter();

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("GET");

    Map<Key, String> conf = new EnumMap<>(Key.class);
    conf.put(Key.SETUP_COMPLETE, "true");
    when(configurationRepository.findConfigurationFor(Mockito.<Set<Key>>any())).thenReturn(conf);

    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();

    sf.init(filterConfig);// w  w  w . j a  v a 2 s . c om

    sf.doFilter(request, response, chain);
}

From source file:com.surevine.alfresco.audit.integration.TagSupportTest.java

/**
 * Test that during creation of a resource the tags are correctly reported.
 *///from   w  ww. j av a  2s  . com
public void testAdditionOfTagsAtCreationTime() {

    String pageName = "Watchin the pool";
    try {

        JSONObject json = new JSONObject();

        json.put(AlfrescoJSONKeys.PAGE, "wiki-page");
        json.put(AlfrescoJSONKeys.PAGETITLE, pageName);
        json.put(AlfrescoJSONKeys.WIKI_PAGE_CONTENT, "<p>in europa league action.</p>");
        eslFixture.addToJSON(json);
        JSONArray jsonTags = new JSONArray();

        // Add each of the fixture tags.
        StringBuffer expectedStringBuffer = new StringBuffer();
        for (int i = 0; i < tags.length; i++) {
            String tag = tags[i];
            jsonTags.put(tag);

            // Now populate the expected string as we go.
            expectedStringBuffer.append(tag);
            if (i != tags.length) {
                expectedStringBuffer.append(", ");
            }
        }
        json.put(AlfrescoJSONKeys.TAGS, jsonTags);

        mockRequest.setRequestURI("/alfresco/s/slingshot/wiki/page/mytestsite/Watchin_the_pool");
        mockRequest.setContent(json.toString().getBytes());
        mockResponse = new MockHttpServletResponse();
        mockChain = new MockFilterChain();

        springAuditFilterBean.doFilter(mockRequest, mockResponse, mockChain);

        Auditable audited = getSingleAuditedEvent();

        // Now only verify the presence of the tags that were added earlier
        assertEquals(expectedStringBuffer.toString(), audited.getTags());

    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}

From source file:org.sventon.web.filter.ConfigAuthorizationFilterTest.java

@Test
public void testDoFilterInternalApplicationConfiguredEditEnabledAlreadyLoggedIn() throws Exception {
    final ConfigAuthorizationFilter filter = new ConfigAuthorizationFilter(application);

    final MockHttpSession session = new MockHttpSession();
    session.setAttribute("isAdminLoggedIn", true);

    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.setSession(session);// w  w  w.  j a  v  a 2s .  c o m

    final HttpServletResponse response = new MockHttpServletResponse();
    final MockFilterChain filterChain = new MockFilterChain();

    application.setConfigured(true);
    application.setEditableConfig(true);

    filter.doFilterInternal(request, response, filterChain);

    assertSame(request, filterChain.getRequest());
    assertSame(response, filterChain.getResponse());
    assertTrue((Boolean) request.getAttribute("isEdit"));
}

From source file:org.ambraproject.admin.filter.RedirectToAmbraFilterTest.java

@Test(dataProvider = "journalContext")
public void testFilter(VirtualJournalContext journalContext, String expectedRedirectUrl)
        throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain filterChain = new MockFilterChain();
    request.setAttribute(VirtualJournalContext.PUB_VIRTUALJOURNAL_CONTEXT, journalContext);
    request.setRequestURI("/article");
    request.setQueryString("id=foo:bar");

    filter.doFilter(request, response, filterChain);
    assertEquals(response.getRedirectedUrl(), expectedRedirectUrl + "/article?id=foo:bar",
            "response didn't get redirected correctly");
}