Search in sources :

Example 21 with MockHttpServletRequest

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

the class ContentTypeNegotiationMessageRendererTest method shouldGenerateXMLResponseMessageForContentType.

@ParameterizedTest
@ValueSource(strings = { MediaType.TEXT_XML_VALUE, MediaType.APPLICATION_XML_VALUE })
void shouldGenerateXMLResponseMessageForContentType(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("<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) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 22 with MockHttpServletRequest

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

the class ContentTypeNegotiationMessageRendererTest method shouldJsonResponseMessageWhenRequestIsMadeWithoutContentTypeAndItIsNotForXMLFile.

@Test
void shouldJsonResponseMessageWhenRequestIsMadeWithoutContentTypeAndItIsNotForXMLFile() {
    final MockHttpServletRequest request = HttpRequestBuilder.GET("/random").build();
    final ContentTypeAwareResponse response = new ContentTypeNegotiationMessageRenderer().getResponse(request);
    assertThat(response.getContentType().toString()).isEqualTo("application/json");
    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) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 23 with MockHttpServletRequest

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

the class BasicAuthenticationWithRedirectToLoginFilterTest method shouldInvokeHandler.

@Test
void shouldInvokeHandler() throws IOException {
    final BasicAuthenticationWithRedirectToLoginFilter filter = new BasicAuthenticationWithRedirectToLoginFilter(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 24 with MockHttpServletRequest

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

the class ArtifactSizeEnforcementFilterChainTest method shouldDoNothingWhenNoArtifactSizeHeaderPresent.

@ParameterizedTest
@ValueSource(strings = { "/files/bar/foo.zip", "/remoting/files/bar/foo.zip" })
void shouldDoNothingWhenNoArtifactSizeHeaderPresent(String path) throws IOException, ServletException {
    MockHttpServletRequest request = HttpRequestBuilder.POST(path).build();
    filter.doFilter(request, response, filterChain);
    assertThat(response).isOk();
    verify(filterChain).doFilter(wrap(request), wrap(response));
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 25 with MockHttpServletRequest

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

the class ArtifactSizeEnforcementFilterChainTest method shouldAllowIfEnoughDiskSpaceIsAvailable.

@ParameterizedTest
@ValueSource(strings = { "/files/bar/foo.zip", "/remoting/files/bar/foo.zip" })
void shouldAllowIfEnoughDiskSpaceIsAvailable(String path) throws IOException, ServletException {
    MockHttpServletRequest request = HttpRequestBuilder.POST(path).withHeader("X-GO-ARTIFACT-SIZE", FileSize.valueOf("100MB").getSize()).build();
    filter.doFilter(request, response, filterChain);
    assertThat(response).isOk();
    verify(filterChain).doFilter(wrap(request), wrap(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