Search in sources :

Example 6 with MockHttpServletRequest

use of com.thoughtworks.go.http.mocks.MockHttpServletRequest in project gocd by gocd.

the class CreateSessionFilterChainTest method setUp.

@BeforeEach
void setUp() throws Exception {
    response = new MockHttpServletResponse();
    request = new MockHttpServletRequest();
    apiSessionReduceIdleTimeoutFilter = spy(new ApiSessionReduceIdleTimeoutFilter(new SystemEnvironment()));
    agentSessionReduceIdleTimeoutFilter = spy(new AgentSessionReduceIdleTimeoutFilter(new SystemEnvironment()));
    alwaysCreateSessionFilter = spy(new AlwaysCreateSessionFilter());
}
Also used : SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) AgentSessionReduceIdleTimeoutFilter(com.thoughtworks.go.server.newsecurity.filters.AgentSessionReduceIdleTimeoutFilter) MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) ApiSessionReduceIdleTimeoutFilter(com.thoughtworks.go.server.newsecurity.filters.ApiSessionReduceIdleTimeoutFilter) AlwaysCreateSessionFilter(com.thoughtworks.go.server.newsecurity.filters.AlwaysCreateSessionFilter) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with MockHttpServletRequest

use of com.thoughtworks.go.http.mocks.MockHttpServletRequest in project gocd by gocd.

the class AgentSessionReduceIdleTimeoutFilterTest method shouldNotReduceSessionMaxInactiveIntervalWhenSessionAlreadyExist.

@Test
public void shouldNotReduceSessionMaxInactiveIntervalWhenSessionAlreadyExist() throws IOException, ServletException {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final SystemEnvironment systemEnvironment = mock(SystemEnvironment.class);
    assertThat(request.getSession()).isNotNull();
    when(systemEnvironment.get(SystemEnvironment.AGENT_REQUEST_IDLE_TIMEOUT_IN_SECONDS)).thenReturn(30);
    new AgentSessionReduceIdleTimeoutFilter(systemEnvironment).doFilter(request, response, filterChain);
    verify(filterChain).doFilter(request, response);
    assertThat(request.getSession(false).getMaxInactiveInterval()).isEqualTo(0);
}
Also used : SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 8 with MockHttpServletRequest

use of com.thoughtworks.go.http.mocks.MockHttpServletRequest in project gocd by gocd.

the class ApiSessionReduceIdleTimeoutFilterTest method shouldNotReduceSessionMaxInactiveIntervalWhenSessionAlreadyExist.

@Test
public void shouldNotReduceSessionMaxInactiveIntervalWhenSessionAlreadyExist() throws IOException, ServletException {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final SystemEnvironment systemEnvironment = mock(SystemEnvironment.class);
    assertThat(request.getSession()).isNotNull();
    when(systemEnvironment.get(SystemEnvironment.API_REQUEST_IDLE_TIMEOUT_IN_SECONDS)).thenReturn(10);
    new ApiSessionReduceIdleTimeoutFilter(systemEnvironment).doFilter(request, response, filterChain);
    verify(filterChain).doFilter(request, response);
    assertThat(request.getSession(false).getMaxInactiveInterval()).isEqualTo(0);
}
Also used : SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 9 with MockHttpServletRequest

use of com.thoughtworks.go.http.mocks.MockHttpServletRequest in project gocd by gocd.

the class ApiSessionReduceIdleTimeoutFilterTest method shouldReduceSessionTimeoutWhenAppliedAndNewSessionIsCreatedAfterApplyingFilter.

@Test
public void shouldReduceSessionTimeoutWhenAppliedAndNewSessionIsCreatedAfterApplyingFilter() throws IOException, ServletException {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final SystemEnvironment systemEnvironment = mock(SystemEnvironment.class);
    when(systemEnvironment.get(SystemEnvironment.API_REQUEST_IDLE_TIMEOUT_IN_SECONDS)).thenReturn(10);
    assertThat(request.getSession(false)).isNull();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            request.getSession(true);
            return null;
        }
    }).when(filterChain).doFilter(request, response);
    new ApiSessionReduceIdleTimeoutFilter(systemEnvironment).doFilter(request, response, filterChain);
    verify(filterChain).doFilter(request, response);
    assertThat(request.getSession(false).getMaxInactiveInterval()).isEqualTo(10);
    assertThat(request.getSession(false)).isNotNull();
}
Also used : SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) Answer(org.mockito.stubbing.Answer) MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 10 with MockHttpServletRequest

use of com.thoughtworks.go.http.mocks.MockHttpServletRequest in project gocd by gocd.

the class DenyAllAccessFilterTest method denyAllAccessWhenFilterIsCalled.

@Test
void denyAllAccessWhenFilterIsCalled() throws ServletException, IOException {
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    new DenyAllAccessFilter().doFilter(request, response, mock(FilterChain.class));
    MockHttpServletResponseAssert.assertThat(response).isForbidden();
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

MockHttpServletRequest (com.thoughtworks.go.http.mocks.MockHttpServletRequest)28 MockHttpServletResponse (com.thoughtworks.go.http.mocks.MockHttpServletResponse)19 Test (org.junit.jupiter.api.Test)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 BeforeEach (org.junit.jupiter.api.BeforeEach)7 ValueSource (org.junit.jupiter.params.provider.ValueSource)6 SystemEnvironment (com.thoughtworks.go.util.SystemEnvironment)5 ContentTypeAwareResponse (com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse)4 SecurityService (com.thoughtworks.go.server.service.SecurityService)4 FilterChain (javax.servlet.FilterChain)4 JsonObject (com.google.gson.JsonObject)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpSession (javax.servlet.http.HttpSession)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 SavedRequest (org.springframework.security.web.savedrequest.SavedRequest)2 MockFilterChain (com.thoughtworks.go.http.mocks.MockFilterChain)1 AgentSessionReduceIdleTimeoutFilter (com.thoughtworks.go.server.newsecurity.filters.AgentSessionReduceIdleTimeoutFilter)1 AlwaysCreateSessionFilter (com.thoughtworks.go.server.newsecurity.filters.AlwaysCreateSessionFilter)1