### Question:
ContextsMediaTypeAspect { @Before("execution(public * ContextsJaxrsService.*(..))") public void checkMediaType(JoinPoint joinPoint) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); String mediaType = request.getHeader(CONTENT_TYPE); String accept = request.getHeader(ACCEPT); failIfHeaderEndsWithXml(mediaType, Response.Status.UNSUPPORTED_MEDIA_TYPE); failIfHeaderEndsWithXml(accept, Response.Status.NOT_ACCEPTABLE); }  @Before("execution(* ContextsJaxrsService.*(..))") void checkMediaType(JoinPoint joinPoint);  }### Answer:
@Test public void checkMediaType_someContentTypeIsXmlAndAcceptIsJson_success() { mockContentTypeAndAccept("application/repository.xmlConnection+xml", "application/repository.xmlConnection+json"); try { contextsMediaTypeAspect.checkMediaType(joinPoint); fail(); } catch (WebApplicationException ex) { assertEquals(Response.Status.UNSUPPORTED_MEDIA_TYPE, ex.getResponse().getStatusInfo()); } }
@Test public void checkMediaType_someContentTypeIsJsonAndAcceptIsXml_success() { mockContentTypeAndAccept("application/repository.xmlConnection+json", "application/repository.xmlConnection+xml"); try { contextsMediaTypeAspect.checkMediaType(joinPoint); fail(); } catch (WebApplicationException ex) { assertEquals(Response.Status.NOT_ACCEPTABLE, ex.getResponse().getStatusInfo()); } }
@Test public void checkMediaType_someContentTypeIsXmlAndAcceptAndCaseInsensitiveIsJson_success() { mockContentTypeAndAccept("application/repository.xmlConnection+XmL", "application/repository.xmlConnection+JsOn"); try { contextsMediaTypeAspect.checkMediaType(joinPoint); fail(); } catch (WebApplicationException ex) { assertEquals(Response.Status.UNSUPPORTED_MEDIA_TYPE, ex.getResponse().getStatusInfo()); } }
@Test public void checkMediaType_someContentTypeIsJsonAndAcceptIsXmlAndCaseInsensitive_success() { mockContentTypeAndAccept("application/repository.xmlConnection+JsoN", "application/repository.xmlConnection+XMl"); try { contextsMediaTypeAspect.checkMediaType(joinPoint); fail(); } catch (WebApplicationException ex) { assertEquals(Response.Status.NOT_ACCEPTABLE, ex.getResponse().getStatusInfo()); } }
@Test public void checkMediaType_bothAreJson_success() { mockContentTypeAndAccept("application/repository.xmlConnection+JsoN", "application/repository.xmlConnection+Json"); contextsMediaTypeAspect.checkMediaType(joinPoint); }