/** * Resolve the given status code to an {@code HttpStatus}, if possible. * @param statusCode the HTTP status code (potentially non-standard) * @return the corresponding {@code HttpStatus}, or {@code null} if not found * @since 5.0 */ @Nullable public static HttpStatus resolve(int statusCode) { for (HttpStatus status : values()) { if (status.value == statusCode) { return status; } } return null; }
/** * Resolve the given status code to an {@code HttpStatus}, if possible. * @param statusCode the HTTP status code (potentially non-standard) * @return the corresponding {@code HttpStatus}, or {@code null} if not found * @since 5.0 */ @Nullable public static HttpStatus resolve(int statusCode) { for (HttpStatus status : values()) { if (status.value == statusCode) { return status; } } return null; }
@Test public void fromEnumToMap() { for (HttpStatus status : HttpStatus.values()) { int value = status.value(); if (value == 302 || value == 413 || value == 414) { continue; } assertTrue("Map has no value for [" + value + "]", statusCodes.containsKey(value)); assertEquals("Invalid name for [" + value + "]", statusCodes.get(value), status.name()); } } }
@Before public void setUp() throws Exception { handler = new UaaOauth2ErrorHandler(null); for (HttpStatus status : HttpStatus.values()) { ClientHttpResponse r = mock(ClientHttpResponse.class); when(r.getStatusCode()).thenReturn(status); responses.put(status, r); } }
@Test public void testHttpStatusCodeResultMatchers() throws Exception { List<AssertionError> failures = new ArrayList<>(); for (HttpStatus status : HttpStatus.values()) { MockHttpServletResponse response = new MockHttpServletResponse(); response.setStatus(status.value()); MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response); try { Method method = getMethodForHttpStatus(status); ResultMatcher matcher = (ResultMatcher) ReflectionUtils.invokeMethod(method, this.matchers); try { matcher.match(mvcResult); } catch (AssertionError error) { failures.add(error); } } catch (Exception ex) { throw new Exception("Failed to obtain ResultMatcher for status " + status, ex); } } if (!failures.isEmpty()) { fail("Failed status codes: " + failures); } }
@Test public void test500Errors() throws Exception { handler.setErrorAtLevel(HttpStatus.Series.SERVER_ERROR); for (HttpStatus status : HttpStatus.values()) { ClientHttpResponse response = responses.get(status); if (status.is5xxServerError()) { Assert.assertTrue(handler.hasError(response)); } else { Assert.assertFalse(handler.hasError(response)); } } }
@Test public void test400_500Errors() throws Exception { handler.setErrorAtLevel(HttpStatus.Series.CLIENT_ERROR); for (HttpStatus status : HttpStatus.values()) { ClientHttpResponse response = responses.get(status); if (status.is5xxServerError() || status.is4xxClientError()) { Assert.assertTrue(handler.hasError(response)); } else { Assert.assertFalse(handler.hasError(response)); } } }
@Test public void statusRanges() throws Exception { for (HttpStatus status : HttpStatus.values()) { MockHttpServletResponse response = new MockHttpServletResponse(); response.setStatus(status.value()); MvcResult mvcResult = new StubMvcResult(request, null, null, null, null, null, response); switch (status.series().value()) { case 1: this.matchers.is1xxInformational().match(mvcResult); break; case 2: this.matchers.is2xxSuccessful().match(mvcResult); break; case 3: this.matchers.is3xxRedirection().match(mvcResult); break; case 4: this.matchers.is4xxClientError().match(mvcResult); break; case 5: this.matchers.is5xxServerError().match(mvcResult); break; default: fail("Unexpected range for status code value " + status); } } }
/** * Return the enum constant of this type with the specified numeric value. * @param statusCode the numeric value of the enum to be returned * @return the enum constant with the specified numeric value * @throws IllegalArgumentException if this enum has no constant for the specified numeric value */ public static HttpStatus valueOf(int statusCode) { for (HttpStatus status : values()) { if (status.value == statusCode) { return status; } } throw new IllegalArgumentException("No matching constant for [" + statusCode + "]"); }
/** * Resolve the given status code to an {@code HttpStatus}, if possible. * @param statusCode the HTTP status code (potentially non-standard) * @return the corresponding {@code HttpStatus}, or {@code null} if not found * @since 5.0 */ @Nullable public static HttpStatus resolve(int statusCode) { for (HttpStatus status : values()) { if (status.value == statusCode) { return status; } } return null; }
/** * Resolve the given status code to an {@code HttpStatus}, if possible. * @param statusCode the HTTP status code (potentially non-standard) * @return the corresponding {@code HttpStatus}, or {@code null} if not found * @since 5.0 */ @Nullable public static HttpStatus resolve(int statusCode) { for (HttpStatus status : values()) { if (status.value == statusCode) { return status; } } return null; }
/** * Delegates to {@link #hasError(HttpStatus)} with the response status code. */ @Override public boolean hasError(ClientHttpResponse response) throws IOException { int rawStatusCode = response.getRawStatusCode(); for (HttpStatus statusCode : HttpStatus.values()) { if (statusCode.value() == rawStatusCode) { return hasError(statusCode); } } return false; }
@PostConstruct public void setup(){ for(GlobalBusinessCodes code : GlobalBusinessCodes.values()) { businessCodes.put(code.getCode(), code.getDescription()); } codes.put("businessCodes", businessCodes); for(HttpStatus httpStatus : HttpStatus.values()) { httpCodes.put(httpStatus.value(), httpStatus.getReasonPhrase()); } codes.put("httpCodes", httpCodes); }
@PostConstruct public void setup(){ for(GlobalBusinessCodes code : GlobalBusinessCodes.values()) { businessCodes.put(code.getCode(), code.getDescription()); } codes.put("businessCodes", businessCodes); for(HttpStatus httpStatus : HttpStatus.values()) { httpCodes.put(httpStatus.value(), httpStatus.getReasonPhrase()); } codes.put("httpCodes", httpCodes); }