@Override public String getAcceptFormat() { String format = request.getParameter("_format"); if (format != null) { return format; } format = request.getHeader("Accept"); return acceptHeaderToFormat.getFormat(format) ; }
@Test public void testPrecendenceInAComplexAcceptHeaderHtmlShouldPrevailWhenTied() { Assert.assertEquals("html", mimeTypeToFormat.getFormat("application/json, text/html, */*")); }
@Test public void shouldComplainIfThereIsNothingRegistered() { Assert.assertEquals("unknown", mimeTypeToFormat.getFormat("unknown")); }
@Test public void testHtml() { Assert.assertEquals("html", mimeTypeToFormat.getFormat("text/html")); }
@Test public void testXMLInAComplexAcceptHeaderWithParametersNotOrdered() { Assert.assertEquals("xml", mimeTypeToFormat.getFormat("application/json; q=0.1, application/xml; q=0.7, */*")); }
@Test public void shouldReturnHtmlWhenRequestingAnyContentType() { Assert.assertEquals("html", mimeTypeToFormat.getFormat("*/*")); }
@Test public void shouldReturnHtmlWhenRequestingUnknownAsFirstAndAnyContentType() { Assert.assertEquals("html", mimeTypeToFormat.getFormat("unknow, */*")); }
@Test public void testNull() { Assert.assertEquals("html", mimeTypeToFormat.getFormat(null)); }
@Test public void testJson() { Assert.assertEquals("json", mimeTypeToFormat.getFormat("application/json")); }
@Test public void testPrecendenceInABizzarreMSIE8AcceptHeader() { Assert.assertEquals("html", mimeTypeToFormat.getFormat("image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, */*")); }
@Test public void testPrecendenceInAComplexAcceptHeaderHtmlShouldPrevailWhenTied2() { Assert.assertEquals("html", mimeTypeToFormat.getFormat("text/html, application/json, */*")); }
@Test public void testJsonInAComplexAcceptHeaderWithParameters() { Assert.assertEquals("json", mimeTypeToFormat.getFormat("application/json; q=0.7, application/xml; q=0.1, */*")); }
@Test public void shouldReturnHtmlWhenAcceptsIsBlankContentType() { Assert.assertEquals("html", mimeTypeToFormat.getFormat("")); }
@Test public void testJsonWithQualifier() { Assert.assertEquals("json", mimeTypeToFormat.getFormat("application/json; q=0.4")); }
@Test public void testJsonInAComplexAcceptHeader() { Assert.assertEquals("json", mimeTypeToFormat.getFormat("application/json, text/javascript, */*")); }
@Test public void testPrecendenceInABizzarreMSIE8AcceptHeaderWithHtml() { Assert.assertEquals("html", mimeTypeToFormat.getFormat("image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, text/html, */*")); }
@Test public void if_formatNotSpecifiedShouldReturnRequestAcceptFormat() { when(request.getParameter("_format")).thenReturn(null); when(request.getHeader("Accept")).thenReturn("application/xml"); when(acceptHeaderToFormat.getFormat("application/xml")).thenReturn("xml"); String format = resolver.getAcceptFormat(); assertThat(format, is("xml")); verify(request).getHeader("Accept"); }