AbstractContext implements Context.Impl { @Override public String getAcceptContentType() { String contentType = getHeader("accept"); if (Strings.isNullOrEmpty(contentType)) { return Result.TEXT_HTML; } String bestMatch = splitter.splitToList(contentType).stream() .map(MediaRange::new) .filter(mediaRange -> mediaRange.weight != 0.0) .sorted() .findFirst() .flatMap(mediaRange -> Optional.of(mediaRange.mediaRange)) .orElse("text/html"); if (bestMatch.contains("application/xhtml") || bestMatch.contains("text/html") || bestMatch.startsWith("**")) { return Result.TEXT_HTML; } return bestMatch; } @Inject  AbstractContext(
            BodyParserEngineManager bodyParserEngineManager,
            FlashScope flashScope,
            NinjaProperties ninjaProperties,
            Session session,
            Validation validation,
            Injector injector,
            ParamParsers paramParsers); @Override void setRoute(Route route); @Override Route getRoute(); @Override Validation getValidation(); @Deprecated @Override FlashScope getFlashCookie(); @Override FlashScope getFlashScope(); @Deprecated @Override Session getSessionCookie(); @Override Session getSession(); @Override String getContextPath(); @Override String getRequestPath(); @Override String getRemoteAddr(); @Override T getAttribute(String name, Class<T> clazz); @Override String getPathParameter(String key); @Override String getPathParameterEncoded(String key); @Override Integer getPathParameterAsInteger(String key); @Override String getParameter(String key, String defaultValue); @Override Integer getParameterAsInteger(String key); @Override Integer getParameterAsInteger(String key, Integer defaultValue); @Override T getParameterAs(String key, Class<T> clazz); @Override T getParameterAs(String key, Class<T> clazz, T defaultValue); @Override T parseBody(Class<T> classOfT); @Override String getCookieValue(String name); @Override void unsetCookie(Cookie cookie); @Override void asyncRequestComplete(); @Override ResponseStreams finalizeHeadersWithoutFlashAndSessionCookie(Result result); @Override ResponseStreams finalizeHeaders(Result result); @Override String getAcceptContentType(); @Override String getAcceptEncoding(); @Override String getAcceptLanguage(); @Override String getAcceptCharset(); @Override boolean isRequestJson(); @Override boolean isRequestXml();  }
@Test public void getAcceptContentTypeLegacy() { AbstractContextImpl context = spy(abstractContext); doReturn("application/protobuf;q=1, application/json;q=0.7").when(context).getHeader("accept"); assertEquals("application/protobuf", context.getAcceptContentType()); doReturn("text*;q=0.5").when(context).getHeader("accept"); assertEquals("text/html", context.getAcceptContentType()); doReturn(null).when(context).getHeader("accept"); assertEquals(Result.TEXT_HTML, context.getAcceptContentType()); doReturn("").when(context).getHeader("accept"); assertEquals(Result.TEXT_HTML, context.getAcceptContentType()); doReturn("totally_unknown").when(context).getHeader("accept"); assertEquals("totally_unknown", context.getAcceptContentType()); doReturn("application/protobuf").when(context).getHeader("accept"); assertEquals("application/protobuf", context.getAcceptContentType()); doReturn("application/json").when(context).getHeader("accept"); assertEquals(Result.APPLICATION_JSON, context.getAcceptContentType()); doReturn("text/html, application/json").when(context).getHeader("accept"); assertEquals(Result.TEXT_HTML, context.getAcceptContentType()); doReturn("application/xhtml, application/json").when(context).getHeader("accept"); assertEquals(Result.TEXT_HTML, context.getAcceptContentType()); doReturn("text/plain").when(context).getHeader("accept"); assertEquals(Result.TEXT_PLAIN, context.getAcceptContentType()); doReturn("application/json, text/plain").when(context).getHeader("accept"); assertEquals(Result.APPLICATION_JSON, context.getAcceptContentType()); }
@Test public void getAcceptContentType() { AbstractContextImpl context = spy(abstractContext); doReturn("application/protobuf;q=1, application/json;q=0.7").when(context).getHeader("accept"); assertEquals("application/protobuf", context.getAcceptContentType()); doReturn("application/protobuf;q=0, application/json;q=0.7").when(context).getHeader("accept"); assertNotEquals("application/protobuf", context.getAcceptContentType()); doReturn("text*;q=0.5").when(context).getHeader("accept"); assertEquals("text/html", context.getAcceptContentType()); doReturn(null).when(context).getHeader("accept"); assertEquals(Result.TEXT_HTML, context.getAcceptContentType()); doReturn("").when(context).getHeader("accept"); assertEquals(Result.TEXT_HTML, context.getAcceptContentType()); doReturn("totally_unknown").when(context).getHeader("accept"); assertEquals("totally_unknown", context.getAcceptContentType()); doReturn("application/protobuf").when(context).getHeader("accept"); assertEquals("application/protobuf", context.getAcceptContentType()); doReturn("application/json").when(context).getHeader("accept"); assertEquals(Result.APPLICATION_JSON, context.getAcceptContentType()); doReturn("text/html;q=1, application/json;q=0.9").when(context).getHeader("accept"); assertEquals(Result.TEXT_HTML, context.getAcceptContentType()); doReturn("application/xhtml;q=1, application/json;q=0.9").when(context).getHeader("accept"); assertEquals(Result.TEXT_HTML, context.getAcceptContentType()); doReturn("text/plain").when(context).getHeader("accept"); assertEquals(Result.TEXT_PLAIN, context.getAcceptContentType()); doReturn("text/plain;q=0.5, application/json;q=0.9").when(context).getHeader("accept"); assertEquals(Result.APPLICATION_JSON, context.getAcceptContentType()); }