Search in sources :

Example 1 with MockHttpServletRequest

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

the class ReAuthenticationWithRedirectToLoginFilterTest method shouldInvokeHandler.

@Test
void shouldInvokeHandler() throws IOException {
    final ReAuthenticationWithRedirectToLoginFilter filter = new ReAuthenticationWithRedirectToLoginFilter(null, null, null, null, null, null);
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final String message = "foo";
    SavedRequest savedRequest = mock(SavedRequest.class);
    SessionUtils.saveRequest(request, savedRequest);
    HttpSession originalSession = request.getSession(true);
    filter.onAuthenticationFailure(request, response, message);
    assertThat(SessionUtils.getAuthenticationError(request)).isEqualTo("foo");
    assertThat(request.getSession(false)).isNotSameAs(originalSession);
    assertThat(SessionUtils.savedRequest(request)).isSameAs(savedRequest);
    assertThat(SessionUtils.hasAuthenticationToken(request)).isFalse();
    MockHttpServletResponseAssert.assertThat(response).redirectsTo("/go/auth/login");
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest) Test(org.junit.jupiter.api.Test)

Example 2 with MockHttpServletRequest

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

the class UserEnabledCheckFilterForApiRequestTest method setUp.

@BeforeEach
void setUp() {
    filter = new UserEnabledCheckFilterForApiRequest(null, null);
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with MockHttpServletRequest

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

the class ContentTypeNegotiationMessageRendererTest method shouldGenerateJSONResponseMessageForContentType.

@ParameterizedTest
@ValueSource(strings = { MediaType.APPLICATION_JSON_VALUE, "application/vnd.go.cd.v1+json", "application/vnd.go.cd.v2+json", "application/vnd.go.cd.v3+json", "application/vnd.go.cd.v4+json", "application/vnd.go.cd.v5+json", "application/vnd.go.cd.v6+json", "application/vnd.go.cd.v7+json", "application/vnd.go.cd.v8+json", "application/vnd.go.cd.v9+json", "application/vnd.go.cd.v50+json", "application/vnd.go.cd.v99+json" })
void shouldGenerateJSONResponseMessageForContentType(String contentType) {
    final MockHttpServletRequest request = HttpRequestBuilder.GET("/").withHeader("Accept", contentType).build();
    final ContentTypeAwareResponse response = new ContentTypeNegotiationMessageRenderer().getResponse(request);
    assertThat(response.getContentType().toString()).isEqualTo(contentType);
    assertThat(response.getFormattedMessage("foo")).isEqualTo("{\n  \"message\": \"foo\"\n}");
}
Also used : ContentTypeAwareResponse(com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse) MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with MockHttpServletRequest

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

the class ContentTypeNegotiationMessageRendererTest method shouldGenerateXMLResponseMessageWhenRequestIsForXMLFile.

@Test
void shouldGenerateXMLResponseMessageWhenRequestIsForXMLFile() {
    final MockHttpServletRequest request = HttpRequestBuilder.GET("/foo.xml").build();
    final ContentTypeAwareResponse response = new ContentTypeNegotiationMessageRenderer().getResponse(request);
    assertThat(response.getContentType().toString()).isEqualTo("application/xml");
    assertThat(response.getFormattedMessage("foo")).isEqualTo("<access-denied>\n  <message>foo</message>\n</access-denied>\n");
}
Also used : ContentTypeAwareResponse(com.thoughtworks.go.server.newsecurity.models.ContentTypeAwareResponse) MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with MockHttpServletRequest

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

the class ArtifactSizeEnforcementFilterChainTest method shouldDisallowIfNotEnoughDiskSpaceIsAvailable.

@ParameterizedTest
@ValueSource(strings = { "/files/bar/foo.zip", "/remoting/files/bar/foo.zip" })
void shouldDisallowIfNotEnoughDiskSpaceIsAvailable(String path) throws IOException, ServletException {
    MockHttpServletRequest request = HttpRequestBuilder.POST(path).withHeader("X-GO-ARTIFACT-SIZE", FileSize.valueOf("600MB").getSize()).build();
    filter.doFilter(request, response, filterChain);
    assertThat(response).isEntityTooLarge();
    verify(filterChain, never()).doFilter(request, response);
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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