/** * Assert the Servlet response error message with the given Hamcrest {@link Matcher}. */ public ResultMatcher reason(final Matcher<? super String> matcher) { return result -> assertThat("Response status reason", result.getResponse().getErrorMessage(), matcher); }
private String assembleStatusLine(MockHttpServletResponse response, Exception resolvedException) { StringBuilder builder = new StringBuilder(); builder.append(response.getStatus()); if (isNotBlank(response.getErrorMessage())) { builder.append(" ").append(response.getErrorMessage()); } else if (resolvedException != null) { builder.append(" ").append(resolvedException.getMessage()); } return builder.toString(); }
private String statusMessage(int statusCode) { String errorMessage = this.response.getErrorMessage(); if (StringUtils.hasText(errorMessage)) { return errorMessage; } try { return HttpStatus.valueOf(statusCode).getReasonPhrase(); } catch (IllegalArgumentException ex) { // ignore } return DEFAULT_STATUS_MESSAGE; }
/** * Assert the Servlet response error message. */ public ResultMatcher reason(final String reason) { return result -> assertEquals("Response status reason", reason, result.getResponse().getErrorMessage()); }
@Test public void doFilterWhenNotAuthorizationRequestAndClientAuthorizationRequiredExceptionThrownButAuthorizationRequestNotResolvedThenStatusInternalServerError() throws Exception { String requestUri = "/path"; MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); request.setServletPath(requestUri); MockHttpServletResponse response = new MockHttpServletResponse(); FilterChain filterChain = mock(FilterChain.class); doThrow(new ClientAuthorizationRequiredException(this.registration1.getRegistrationId())) .when(filterChain).doFilter(any(ServletRequest.class), any(ServletResponse.class)); OAuth2AuthorizationRequestResolver resolver = mock(OAuth2AuthorizationRequestResolver.class); OAuth2AuthorizationRequestRedirectFilter filter = new OAuth2AuthorizationRequestRedirectFilter(resolver); filter.doFilter(request, response, filterChain); verify(filterChain).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class)); verifyZeroInteractions(filterChain); assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.value()); assertThat(response.getErrorMessage()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()); }
@Test public void doFilterWhenAuthorizationRequestWithInvalidClientThenStatusInternalServerError() throws Exception { String requestUri = OAuth2AuthorizationRequestRedirectFilter.DEFAULT_AUTHORIZATION_REQUEST_BASE_URI + "/" + this.registration1.getRegistrationId() + "-invalid"; MockHttpServletRequest request = new MockHttpServletRequest("GET", requestUri); request.setServletPath(requestUri); MockHttpServletResponse response = new MockHttpServletResponse(); FilterChain filterChain = mock(FilterChain.class); this.filter.doFilter(request, response, filterChain); verifyZeroInteractions(filterChain); assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.value()); assertThat(response.getErrorMessage()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()); }
assertValue(heading, "Error message", response.getErrorMessage()); assertValue(heading, "Headers", headers); assertValue(heading, "Content type", this.response.getContentType());
/** * Print the response. */ protected void printResponse(MockHttpServletResponse response) throws Exception { String body = (response.getCharacterEncoding() != null ? response.getContentAsString() : MISSING_CHARACTER_ENCODING); this.printer.printValue("Status", response.getStatus()); this.printer.printValue("Error message", response.getErrorMessage()); this.printer.printValue("Headers", getResponseHeaders(response)); this.printer.printValue("Content type", response.getContentType()); this.printer.printValue("Body", body); this.printer.printValue("Forwarded URL", response.getForwardedUrl()); this.printer.printValue("Redirected URL", response.getRedirectedUrl()); printCookies(response.getCookies()); }
@Test public void error_is_not() throws Exception { filter.setPermittedEndpoints(new HashSet(Arrays.asList("/oauth/token/**"))); filter.setStatusFile(statusFile); for (String accept : Arrays.asList("text/html", "text/plain")) { request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); setPathInfo("/not/allowed"); request.setMethod(POST.name()); request.addHeader(ACCEPT, accept); filter.doFilterInternal(request, response, chain); assertEquals(SC_SERVICE_UNAVAILABLE, response.getStatus()); assertEquals(filter.getErrorData().get("description"), response.getErrorMessage()); } }
.requestAttr(RequestDispatcher.ERROR_REQUEST_URI, uri.toString()); if (servletResponse.getErrorMessage() != null) { requestBuilder.requestAttr(RequestDispatcher.ERROR_MESSAGE, servletResponse.getErrorMessage());
@Test public void testHandleWhenCsrfMissingForJson() throws Exception { request.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE); AccessDeniedException ex = new MissingCsrfTokenException("something"); handler.handle(request, response, ex); assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getStatus()); assertEquals("{\"error\":\"Could not verify the provided CSRF token because your session was not found.\"}", response.getContentAsString()); assertEquals(null, response.getErrorMessage()); }
@Test public void testHandleWhenNotLoggedInJson() throws Exception { request.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE); AccessDeniedException ex = new AccessDeniedException("something"); handler.handle(request, response, ex); assertEquals(HttpServletResponse.SC_FORBIDDEN, response.getStatus()); assertEquals("{\"error\":\"something\"}", response.getContentAsString()); assertEquals(null, response.getErrorMessage()); }
/** * Assert the Servlet response error message with the given Hamcrest {@link Matcher}. */ public ResultMatcher reason(final Matcher<? super String> matcher) { return result -> assertThat("Response status reason", result.getResponse().getErrorMessage(), matcher); }
private String statusMessage(int statusCode) { String errorMessage = this.response.getErrorMessage(); if (StringUtils.hasText(errorMessage)) { return errorMessage; } try { return HttpStatus.valueOf(statusCode).getReasonPhrase(); } catch (IllegalArgumentException ex) { // ignore } return DEFAULT_STATUS_MESSAGE; }
/** * Assert the Servlet response error message. */ public ResultMatcher reason(final String reason) { return result -> assertEquals("Response status reason", reason, result.getResponse().getErrorMessage()); }
@Test public void testGetCollectionsPagingValidation() throws Exception { MockHttpServletResponse response = getAsServletResponse("/rest/oseo/collections?offset=-1"); assertEquals(400, response.getStatus()); assertThat(response.getErrorMessage(), containsString("offset")); response = getAsServletResponse("/rest/oseo/collections?limit=-1"); assertEquals(400, response.getStatus()); assertThat(response.getErrorMessage(), containsString("limit")); response = getAsServletResponse("/rest/oseo/collections?limit=1000"); assertEquals(400, response.getStatus()); assertThat(response.getErrorMessage(), containsString("limit")); }
@Test public void testGetProductsPagingValidation() throws Exception { MockHttpServletResponse response = getAsServletResponse("/rest/oseo/collections/SENTINEL2/products?offset=-1"); assertEquals(400, response.getStatus()); assertThat(response.getErrorMessage(), containsString("offset")); response = getAsServletResponse("/rest/oseo/collections/SENTINEL2/products?limit=-1"); assertEquals(400, response.getStatus()); assertThat(response.getErrorMessage(), containsString("limit")); response = getAsServletResponse("/rest/oseo/collections/SENTINEL2/products?limit=1000"); assertEquals(400, response.getStatus()); assertThat(response.getErrorMessage(), containsString("limit")); }
@Test public void testKycReminder_kycNotStarted() throws Exception { KycInfo kycInfo = createKycInfo(false, 0, false); when(mockKycInfoService.getKycInfoByInvestorId(INVESTOR_ID)).thenReturn(kycInfo); MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post(KYC_REMIND)) .andExpect(status().isBadRequest()) .andDo(print()) .andReturn(); assertThat(result.getResponse().getErrorMessage()).isEqualTo(KYC_NOT_YET_STARTED_REASON); }
@Test public void testStartKycWithMalformedUri() throws Exception { KycStartRequestDTO requestWithMalformedUri = new KycStartRequestDTO("this is not a url"); when(mockKycInfoService.getKycInfoByInvestorId(INVESTOR_ID)).thenThrow(new InvestorNotFoundException()); MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post(KYC_INVESTOR_START) .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(requestWithMalformedUri))) .andExpect(status().isBadRequest()) .andDo(print()) .andReturn(); assertThat(result.getResponse().getErrorMessage()).isEqualTo(KYC_LINK_REASON); }
/** Print the response */ protected void printResponse(MockHttpServletResponse response) throws Exception { this.printer.printValue("Status", response.getStatus()); this.printer.printValue("Error message", response.getErrorMessage()); this.printer.printValue("Headers", getResponseHeaders(response)); this.printer.printValue("Content type", response.getContentType()); this.printer.printValue("Body", response.getContentAsString()); this.printer.printValue("Forwarded URL", response.getForwardedUrl()); this.printer.printValue("Redirected URL", response.getRedirectedUrl()); this.printer.printValue("Cookies", response.getCookies()); }