HeadersContentTypeDetector implements ContentTypeDetector { @Override public FilterEngine.ContentType detect(final WebResourceRequest request) { final Map<String, String> headers = request.getRequestHeaders(); final boolean isXmlHttpRequest = headers.containsKey(HEADER_REQUESTED_WITH) && HEADER_REQUESTED_WITH_XMLHTTPREQUEST.equals(headers.get(HEADER_REQUESTED_WITH)); if (isXmlHttpRequest) { Timber.w("using xmlhttprequest content type"); return FilterEngine.ContentType.XMLHTTPREQUEST; } final String acceptType = headers.get(HEADER_ACCEPT); if (acceptType != null && acceptType.contains(MIME_TYPE_TEXT_HTML)) { Timber.w("using subdocument content type"); return FilterEngine.ContentType.SUBDOCUMENT; } return null; }  @Override FilterEngine.ContentType detect(final WebResourceRequest request);  }
@Test public void testHeaderRequests() { assertEquals(FilterEngine.ContentType.XMLHTTPREQUEST, detector.detect(mockRequest(URI_EXAMPLE, XML_HEADER))); assertEquals(FilterEngine.ContentType.SUBDOCUMENT, detector.detect(mockRequest(URI_EXAMPLE, SUBDOCUMENT_HEADER))); assertNull(detector.detect(mockRequest(URI_EXAMPLE, BROKEN_XML_HEADER))); assertNull(detector.detect(mockRequest(URI_EXAMPLE, CONTENT_LENGTH_HEADER))); }