@Test public void should_honor_constructor_params() { //given String invalidHeaderValue = UUID.randomUUID().toString(); String message = UUID.randomUUID().toString(); Exception cause = new Exception("kaboom"); //when InvalidCharsetInContentTypeHeaderException ex = new InvalidCharsetInContentTypeHeaderException(message, cause, invalidHeaderValue); //then assertThat(ex.getMessage(), is(message)); assertThat(ex.getCause(), is(cause)); assertThat(ex.invalidContentTypeHeader, is(invalidHeaderValue)); }
/** * @param headers * The headers containing the content-type header to parse * @param def * The default charset to use if one wasn't found in the headers * * @return The encoding specified in the header or the default Charset if not specified. **/ public static Charset determineCharsetFromContentType(HttpHeaders headers, Charset def) { if (headers == null) return def; String contentTypeHeader = headers.get(HttpHeaders.Names.CONTENT_TYPE); if (contentTypeHeader == null) return def; String charset; Matcher m = CONTENT_TYPE_CHARSET_EXTRACTOR_PATTERN.matcher(contentTypeHeader); if (m.find()) { charset = m.group(1).trim().toUpperCase(); try { return Charset.forName(charset); } catch (Exception ex) { throw new InvalidCharsetInContentTypeHeaderException("Invalid charset in Content-Type header", ex, contentTypeHeader); } } return def; }
@Test public void shouldHandleInvalidCharsetInContentTypeHeaderException() { verifyExceptionHandled(new InvalidCharsetInContentTypeHeaderException("intentional boom", null, null), singletonError(testProjectApiErrors.getUnsupportedMediaTypeApiError())); }