RequestDefaultHeaders implements HttpRequestInterceptor { public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (request == null) { throw new IllegalArgumentException("HTTP request may not be null"); } String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase("CONNECT")) { return; } @SuppressWarnings("unchecked") Collection<Header> defHeaders = (Collection<Header>) request.getParams().getParameter( ClientPNames.DEFAULT_HEADERS); if (defHeaders != null) { for (Header defHeader : defHeaders) { request.addHeader(defHeader); } } } RequestDefaultHeaders(); void process(final HttpRequest request, final HttpContext context);  }
@Test(expected=IllegalArgumentException.class) public void testRequestParameterCheck() throws Exception { HttpContext context = new BasicHttpContext(); HttpRequestInterceptor interceptor = new RequestDefaultHeaders(); interceptor.process(null, context); }
@Test public void testNoDefaultHeadersForConnectRequest() throws Exception { HttpRequest request = new BasicHttpRequest("CONNECT", "www.somedomain.com"); List<Header> defheaders = new ArrayList<Header>(); defheaders.add(new BasicHeader("custom", "stuff")); request.getParams().setParameter(AllClientPNames.DEFAULT_HEADERS, defheaders); HttpContext context = new BasicHttpContext(); HttpRequestInterceptor interceptor = new RequestDefaultHeaders(); interceptor.process(request, context); Header header1 = request.getFirstHeader("custom"); Assert.assertNull(header1); }
@Test public void testDefaultHeaders() throws Exception { HttpRequest request = new BasicHttpRequest("GET", "/"); request.addHeader("custom", "stuff"); List<Header> defheaders = new ArrayList<Header>(); defheaders.add(new BasicHeader("custom", "more stuff")); request.getParams().setParameter(AllClientPNames.DEFAULT_HEADERS, defheaders); HttpContext context = new BasicHttpContext(); HttpRequestInterceptor interceptor = new RequestDefaultHeaders(); interceptor.process(request, context); Header[] headers = request.getHeaders("custom"); Assert.assertNotNull(headers); Assert.assertEquals(2, headers.length); Assert.assertEquals("stuff", headers[0].getValue()); Assert.assertEquals("more stuff", headers[1].getValue()); }