001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/protocol/TestStandardInterceptors.java $
003: * $Revision: 586026 $
004: * $Date: 2007-10-18 18:22:27 +0200 (Thu, 18 Oct 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.protocol;
033:
034: import junit.framework.Test;
035: import junit.framework.TestCase;
036: import junit.framework.TestSuite;
037:
038: import org.apache.http.Header;
039: import org.apache.http.HttpHost;
040: import org.apache.http.HttpResponse;
041: import org.apache.http.HttpStatus;
042: import org.apache.http.HttpVersion;
043: import org.apache.http.ProtocolException;
044: import org.apache.http.entity.BasicHttpEntity;
045: import org.apache.http.entity.StringEntity;
046: import org.apache.http.message.BasicHeader;
047: import org.apache.http.message.BasicHttpEntityEnclosingRequest;
048: import org.apache.http.message.BasicHttpRequest;
049: import org.apache.http.message.BasicHttpResponse;
050: import org.apache.http.params.CoreProtocolPNames;
051:
052: /**
053: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
054: */
055: public class TestStandardInterceptors extends TestCase {
056:
057: public TestStandardInterceptors(String testName) {
058: super (testName);
059: }
060:
061: public static void main(String args[]) {
062: String[] testCaseName = { TestStandardInterceptors.class
063: .getName() };
064: junit.textui.TestRunner.main(testCaseName);
065: }
066:
067: public static Test suite() {
068: return new TestSuite(TestStandardInterceptors.class);
069: }
070:
071: public void testRequestConnControlGenerated() throws Exception {
072: HttpContext context = new BasicHttpContext(null);
073: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
074: RequestConnControl interceptor = new RequestConnControl();
075: interceptor.process(request, context);
076: Header header = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
077: assertNotNull(header);
078: assertEquals(HTTP.CONN_KEEP_ALIVE, header.getValue());
079: }
080:
081: public void testRequestConnControlCustom() throws Exception {
082: HttpContext context = new BasicHttpContext(null);
083: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
084: Header myheader = new BasicHeader(HTTP.CONN_DIRECTIVE,
085: HTTP.CONN_CLOSE);
086: request.addHeader(myheader);
087: RequestConnControl interceptor = new RequestConnControl();
088: interceptor.process(request, context);
089: Header header = request.getFirstHeader(HTTP.CONN_DIRECTIVE);
090: assertNotNull(header);
091: assertEquals(HTTP.CONN_CLOSE, header.getValue());
092: assertTrue(header == myheader);
093: }
094:
095: public void testRequestConnControlInvalidInput() throws Exception {
096: RequestConnControl interceptor = new RequestConnControl();
097: try {
098: interceptor.process(null, null);
099: fail("IllegalArgumentException should have been thrown");
100: } catch (IllegalArgumentException ex) {
101: // expected
102: }
103: }
104:
105: public void testRequestContentProtocolException() throws Exception {
106: HttpContext context = new BasicHttpContext(null);
107: BasicHttpRequest request1 = new BasicHttpEntityEnclosingRequest(
108: "POST", "/");
109: request1.addHeader(new BasicHeader(HTTP.TRANSFER_ENCODING,
110: "chunked"));
111: BasicHttpRequest request2 = new BasicHttpEntityEnclosingRequest(
112: "POST", "/");
113: request2.addHeader(new BasicHeader(HTTP.CONTENT_LEN, "12"));
114:
115: RequestContent interceptor = new RequestContent();
116: try {
117: interceptor.process(request1, context);
118: fail("ProtocolException should have been thrown");
119: } catch (ProtocolException ex) {
120: // expected
121: }
122: try {
123: interceptor.process(request2, context);
124: fail("ProtocolException should have been thrown");
125: } catch (ProtocolException ex) {
126: // expected
127: }
128: }
129:
130: public void testRequestContentNullEntity() throws Exception {
131: HttpContext context = new BasicHttpContext(null);
132: BasicHttpRequest request = new BasicHttpEntityEnclosingRequest(
133: "POST", "/");
134:
135: RequestContent interceptor = new RequestContent();
136: interceptor.process(request, context);
137: Header header = request.getFirstHeader(HTTP.CONTENT_LEN);
138: assertNotNull(header);
139: assertEquals("0", header.getValue());
140: assertNull(request.getFirstHeader(HTTP.TRANSFER_ENCODING));
141: }
142:
143: public void testRequestContentEntityContentLengthDelimitedHTTP11()
144: throws Exception {
145: HttpContext context = new BasicHttpContext(null);
146: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
147: "POST", "/");
148: String s = "whatever";
149: StringEntity entity = new StringEntity(s, "US-ASCII");
150: request.setEntity(entity);
151:
152: RequestContent interceptor = new RequestContent();
153: interceptor.process(request, context);
154: Header header = request.getFirstHeader(HTTP.CONTENT_LEN);
155: assertNotNull(header);
156: assertEquals(s.length(), Integer.parseInt(header.getValue()));
157: assertNull(request.getFirstHeader(HTTP.TRANSFER_ENCODING));
158: }
159:
160: public void testRequestContentEntityChunkedHTTP11()
161: throws Exception {
162: HttpContext context = new BasicHttpContext(null);
163: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
164: "POST", "/");
165: String s = "whatever";
166: StringEntity entity = new StringEntity(s, "US-ASCII");
167: entity.setChunked(true);
168: request.setEntity(entity);
169:
170: RequestContent interceptor = new RequestContent();
171: interceptor.process(request, context);
172: Header header = request.getFirstHeader(HTTP.TRANSFER_ENCODING);
173: assertNotNull(header);
174: assertEquals("chunked", header.getValue());
175: assertNull(request.getFirstHeader(HTTP.CONTENT_LEN));
176: }
177:
178: public void testRequestContentEntityUnknownLengthHTTP11()
179: throws Exception {
180: HttpContext context = new BasicHttpContext(null);
181: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
182: "POST", "/");
183: BasicHttpEntity entity = new BasicHttpEntity();
184: entity.setContentLength(-1);
185: entity.setChunked(false);
186: request.setEntity(entity);
187:
188: RequestContent interceptor = new RequestContent();
189: interceptor.process(request, context);
190: Header header = request.getFirstHeader(HTTP.TRANSFER_ENCODING);
191: assertNotNull(header);
192: assertEquals("chunked", header.getValue());
193: assertNull(request.getFirstHeader(HTTP.CONTENT_LEN));
194: }
195:
196: public void testRequestContentEntityChunkedHTTP10()
197: throws Exception {
198: HttpContext context = new BasicHttpContext(null);
199: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
200: "POST", "/", HttpVersion.HTTP_1_0);
201: String s = "whatever";
202: StringEntity entity = new StringEntity(s, "US-ASCII");
203: entity.setChunked(true);
204: request.setEntity(entity);
205:
206: RequestContent interceptor = new RequestContent();
207: try {
208: interceptor.process(request, context);
209: fail("ProtocolException should have been thrown");
210: } catch (ProtocolException ex) {
211: // expected
212: }
213: }
214:
215: public void testRequestContentTypeAndEncoding() throws Exception {
216: HttpContext context = new BasicHttpContext(null);
217: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
218: "POST", "/");
219: BasicHttpEntity entity = new BasicHttpEntity();
220: entity.setContentType("whatever");
221: entity.setContentEncoding("whatever");
222: request.setEntity(entity);
223:
224: RequestContent interceptor = new RequestContent();
225: interceptor.process(request, context);
226: Header h1 = request.getFirstHeader(HTTP.CONTENT_TYPE);
227: assertNotNull(h1);
228: assertEquals("whatever", h1.getValue());
229: Header h2 = request.getFirstHeader(HTTP.CONTENT_ENCODING);
230: assertNotNull(h2);
231: assertEquals("whatever", h2.getValue());
232: }
233:
234: public void testRequestContentNullTypeAndEncoding()
235: throws Exception {
236: HttpContext context = new BasicHttpContext(null);
237: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
238: "POST", "/");
239: BasicHttpEntity entity = new BasicHttpEntity();
240: request.setEntity(entity);
241:
242: RequestContent interceptor = new RequestContent();
243: interceptor.process(request, context);
244: assertNull(request.getFirstHeader(HTTP.CONTENT_TYPE));
245: assertNull(request.getFirstHeader(HTTP.CONTENT_ENCODING));
246: }
247:
248: public void testRequestContentEntityUnknownLengthHTTP10()
249: throws Exception {
250: HttpContext context = new BasicHttpContext(null);
251: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
252: "POST", "/", HttpVersion.HTTP_1_0);
253: BasicHttpEntity entity = new BasicHttpEntity();
254: entity.setContentLength(-1);
255: entity.setChunked(false);
256: request.setEntity(entity);
257:
258: RequestContent interceptor = new RequestContent();
259: try {
260: interceptor.process(request, context);
261: fail("ProtocolException should have been thrown");
262: } catch (ProtocolException ex) {
263: // expected
264: }
265: }
266:
267: public void testRequestContentInvalidInput() throws Exception {
268: RequestContent interceptor = new RequestContent();
269: try {
270: interceptor.process(null, null);
271: fail("IllegalArgumentException should have been thrown");
272: } catch (IllegalArgumentException ex) {
273: // expected
274: }
275: }
276:
277: public void testRequestContentIgnoreNonenclosingRequests()
278: throws Exception {
279: HttpContext context = new BasicHttpContext(null);
280: BasicHttpRequest request = new BasicHttpRequest("POST", "/");
281: RequestContent interceptor = new RequestContent();
282: interceptor.process(request, context);
283: assertEquals(0, request.getAllHeaders().length);
284: }
285:
286: public void testRequestExpectContinueGenerated() throws Exception {
287: HttpContext context = new BasicHttpContext(null);
288: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
289: "POST", "/");
290: String s = "whatever";
291: StringEntity entity = new StringEntity(s, "US-ASCII");
292: request.setEntity(entity);
293: request.getParams().setBooleanParameter(
294: CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
295: RequestExpectContinue interceptor = new RequestExpectContinue();
296: interceptor.process(request, context);
297: Header header = request.getFirstHeader(HTTP.EXPECT_DIRECTIVE);
298: assertNotNull(header);
299: assertEquals(HTTP.EXPECT_CONTINUE, header.getValue());
300: }
301:
302: public void testRequestExpectContinueNotGenerated()
303: throws Exception {
304: HttpContext context = new BasicHttpContext(null);
305: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
306: "POST", "/");
307: String s = "whatever";
308: StringEntity entity = new StringEntity(s, "US-ASCII");
309: request.setEntity(entity);
310: request.getParams().setBooleanParameter(
311: CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
312: RequestExpectContinue interceptor = new RequestExpectContinue();
313: interceptor.process(request, context);
314: Header header = request.getFirstHeader(HTTP.EXPECT_DIRECTIVE);
315: assertNull(header);
316: }
317:
318: public void testRequestExpectContinueHTTP10() throws Exception {
319: HttpContext context = new BasicHttpContext(null);
320: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
321: "POST", "/", HttpVersion.HTTP_1_0);
322: String s = "whatever";
323: StringEntity entity = new StringEntity(s, "US-ASCII");
324: request.setEntity(entity);
325: request.getParams().setBooleanParameter(
326: CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
327: RequestExpectContinue interceptor = new RequestExpectContinue();
328: interceptor.process(request, context);
329: Header header = request.getFirstHeader(HTTP.EXPECT_DIRECTIVE);
330: assertNull(header);
331: }
332:
333: public void testRequestExpectContinueZeroContent() throws Exception {
334: HttpContext context = new BasicHttpContext(null);
335: BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(
336: "POST", "/");
337: String s = "";
338: StringEntity entity = new StringEntity(s, "US-ASCII");
339: request.setEntity(entity);
340: request.getParams().setBooleanParameter(
341: CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
342: RequestExpectContinue interceptor = new RequestExpectContinue();
343: interceptor.process(request, context);
344: Header header = request.getFirstHeader(HTTP.EXPECT_DIRECTIVE);
345: assertNull(header);
346: }
347:
348: public void testRequestExpectContinueInvalidInput()
349: throws Exception {
350: RequestExpectContinue interceptor = new RequestExpectContinue();
351: try {
352: interceptor.process(null, null);
353: fail("IllegalArgumentException should have been thrown");
354: } catch (IllegalArgumentException ex) {
355: // expected
356: }
357: }
358:
359: public void testRequestExpectContinueIgnoreNonenclosingRequests()
360: throws Exception {
361: HttpContext context = new BasicHttpContext(null);
362: BasicHttpRequest request = new BasicHttpRequest("POST", "/");
363: RequestExpectContinue interceptor = new RequestExpectContinue();
364: interceptor.process(request, context);
365: assertEquals(0, request.getAllHeaders().length);
366: }
367:
368: public void testRequestTargetHostGenerated() throws Exception {
369: HttpContext context = new BasicHttpContext(null);
370: HttpHost host = new HttpHost("somehost", 8080, "http");
371: context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
372: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
373: RequestTargetHost interceptor = new RequestTargetHost();
374: interceptor.process(request, context);
375: Header header = request.getFirstHeader(HTTP.TARGET_HOST);
376: assertNotNull(header);
377: assertEquals("somehost:8080", header.getValue());
378: }
379:
380: public void testRequestTargetHostNotGenerated() throws Exception {
381: HttpContext context = new BasicHttpContext(null);
382: HttpHost host = new HttpHost("somehost", 8080, "http");
383: context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
384: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
385: request
386: .addHeader(new BasicHeader(HTTP.TARGET_HOST, "whatever"));
387: RequestTargetHost interceptor = new RequestTargetHost();
388: interceptor.process(request, context);
389: Header header = request.getFirstHeader(HTTP.TARGET_HOST);
390: assertNotNull(header);
391: assertEquals("whatever", header.getValue());
392: }
393:
394: public void testRequestTargetHostMissingHostHTTP10()
395: throws Exception {
396: HttpContext context = new BasicHttpContext(null);
397: BasicHttpRequest request = new BasicHttpRequest("GET", "/",
398: HttpVersion.HTTP_1_0);
399: RequestTargetHost interceptor = new RequestTargetHost();
400: interceptor.process(request, context);
401: Header header = request.getFirstHeader(HTTP.TARGET_HOST);
402: assertNull(header);
403: }
404:
405: public void testRequestTargetHostMissingHostHTTP11()
406: throws Exception {
407: HttpContext context = new BasicHttpContext(null);
408: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
409: RequestTargetHost interceptor = new RequestTargetHost();
410: try {
411: interceptor.process(request, context);
412: fail("ProtocolException should have been thrown");
413: } catch (ProtocolException ex) {
414: // expected
415: }
416: }
417:
418: public void testRequestTargetHostInvalidInput() throws Exception {
419: RequestTargetHost interceptor = new RequestTargetHost();
420: try {
421: interceptor.process(null, null);
422: fail("IllegalArgumentException should have been thrown");
423: } catch (IllegalArgumentException ex) {
424: // expected
425: }
426: try {
427: interceptor.process(new BasicHttpRequest("GET", "/"), null);
428: fail("IllegalArgumentException should have been thrown");
429: } catch (IllegalArgumentException ex) {
430: // expected
431: }
432: }
433:
434: public void testRequestUserAgentGenerated() throws Exception {
435: HttpContext context = new BasicHttpContext(null);
436: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
437: request.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
438: "some agent");
439: RequestUserAgent interceptor = new RequestUserAgent();
440: interceptor.process(request, context);
441: Header header = request.getFirstHeader(HTTP.USER_AGENT);
442: assertNotNull(header);
443: assertEquals("some agent", header.getValue());
444: }
445:
446: public void testRequestUserAgentNotGenerated() throws Exception {
447: HttpContext context = new BasicHttpContext(null);
448: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
449: request.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
450: "some agent");
451: request.addHeader(new BasicHeader(HTTP.USER_AGENT, "whatever"));
452: RequestUserAgent interceptor = new RequestUserAgent();
453: interceptor.process(request, context);
454: Header header = request.getFirstHeader(HTTP.USER_AGENT);
455: assertNotNull(header);
456: assertEquals("whatever", header.getValue());
457: }
458:
459: public void testRequestUserAgentMissingUserAgent() throws Exception {
460: HttpContext context = new BasicHttpContext(null);
461: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
462: RequestUserAgent interceptor = new RequestUserAgent();
463: interceptor.process(request, context);
464: Header header = request.getFirstHeader(HTTP.USER_AGENT);
465: assertNull(header);
466: }
467:
468: public void testRequestUserAgentInvalidInput() throws Exception {
469: RequestUserAgent interceptor = new RequestUserAgent();
470: try {
471: interceptor.process(null, null);
472: fail("IllegalArgumentException should have been thrown");
473: } catch (IllegalArgumentException ex) {
474: // expected
475: }
476: }
477:
478: public void testResponseConnControlNoEntity() throws Exception {
479: HttpContext context = new BasicHttpContext(null);
480: HttpResponse response = new BasicHttpResponse(
481: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
482: ResponseConnControl interceptor = new ResponseConnControl();
483: interceptor.process(response, context);
484: Header header = response.getFirstHeader(HTTP.CONN_DIRECTIVE);
485: assertNull(header);
486: }
487:
488: public void testResponseConnControlEntityContentLength()
489: throws Exception {
490: HttpContext context = new BasicHttpContext(null);
491: HttpResponse response = new BasicHttpResponse(
492: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
493: StringEntity entity = new StringEntity("whatever");
494: response.setEntity(entity);
495: ResponseConnControl interceptor = new ResponseConnControl();
496: interceptor.process(response, context);
497: Header header = response.getFirstHeader(HTTP.CONN_DIRECTIVE);
498: assertNull(header);
499: }
500:
501: public void testResponseConnControlEntityUnknownContentLength()
502: throws Exception {
503: HttpContext context = new BasicHttpContext(null);
504: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
505: request.addHeader(new BasicHeader(HTTP.CONN_DIRECTIVE,
506: HTTP.CONN_KEEP_ALIVE));
507: context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
508: HttpResponse response = new BasicHttpResponse(
509: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
510: BasicHttpEntity entity = new BasicHttpEntity();
511: response.setEntity(entity);
512: ResponseConnControl interceptor = new ResponseConnControl();
513: interceptor.process(response, context);
514: Header header = response.getFirstHeader(HTTP.CONN_DIRECTIVE);
515: assertNotNull(header);
516: assertEquals(HTTP.CONN_CLOSE, header.getValue());
517: }
518:
519: public void testResponseConnControlEntityChunked() throws Exception {
520: HttpContext context = new BasicHttpContext(null);
521: HttpResponse response = new BasicHttpResponse(
522: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
523: BasicHttpEntity entity = new BasicHttpEntity();
524: entity.setChunked(true);
525: response.setEntity(entity);
526: ResponseConnControl interceptor = new ResponseConnControl();
527: interceptor.process(response, context);
528: Header header = response.getFirstHeader(HTTP.CONN_DIRECTIVE);
529: assertNull(header);
530: }
531:
532: public void testResponseConnControlEntityUnknownContentLengthHTTP10()
533: throws Exception {
534: HttpContext context = new BasicHttpContext(null);
535: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
536: request.addHeader(new BasicHeader(HTTP.CONN_DIRECTIVE,
537: HTTP.CONN_KEEP_ALIVE));
538: context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
539:
540: BasicHttpResponse response = new BasicHttpResponse(
541: HttpVersion.HTTP_1_0, HttpStatus.SC_OK, "OK");
542: BasicHttpEntity entity = new BasicHttpEntity();
543: response.setEntity(entity);
544: ResponseConnControl interceptor = new ResponseConnControl();
545: interceptor.process(response, context);
546: Header header = response.getFirstHeader(HTTP.CONN_DIRECTIVE);
547: assertNotNull(header);
548: assertEquals(HTTP.CONN_CLOSE, header.getValue());
549: }
550:
551: public void testResponseConnControlClientRequest() throws Exception {
552: HttpContext context = new BasicHttpContext(null);
553: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
554: request.addHeader(new BasicHeader(HTTP.CONN_DIRECTIVE,
555: HTTP.CONN_KEEP_ALIVE));
556: context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
557:
558: HttpResponse response = new BasicHttpResponse(
559: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
560: StringEntity entity = new StringEntity("whatever");
561: response.setEntity(entity);
562: ResponseConnControl interceptor = new ResponseConnControl();
563: interceptor.process(response, context);
564: Header header = response.getFirstHeader(HTTP.CONN_DIRECTIVE);
565: assertNotNull(header);
566: assertEquals(HTTP.CONN_KEEP_ALIVE, header.getValue());
567: }
568:
569: public void testResponseConnControlClientRequest2()
570: throws Exception {
571: HttpContext context = new BasicHttpContext(null);
572: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
573: context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
574:
575: HttpResponse response = new BasicHttpResponse(
576: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
577: StringEntity entity = new StringEntity("whatever");
578: response.setEntity(entity);
579: ResponseConnControl interceptor = new ResponseConnControl();
580: interceptor.process(response, context);
581: Header header = response.getFirstHeader(HTTP.CONN_DIRECTIVE);
582: assertNull(header);
583: }
584:
585: public void testResponseConnControlStatusCode() throws Exception {
586: HttpContext context = new BasicHttpContext(null);
587: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
588: request.addHeader(new BasicHeader(HTTP.CONN_DIRECTIVE,
589: HTTP.CONN_KEEP_ALIVE));
590: context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
591:
592: ResponseConnControl interceptor = new ResponseConnControl();
593:
594: int[] statusCodes = new int[] { HttpStatus.SC_BAD_REQUEST,
595: HttpStatus.SC_REQUEST_TIMEOUT,
596: HttpStatus.SC_LENGTH_REQUIRED,
597: HttpStatus.SC_REQUEST_TOO_LONG,
598: HttpStatus.SC_REQUEST_URI_TOO_LONG,
599: HttpStatus.SC_SERVICE_UNAVAILABLE,
600: HttpStatus.SC_NOT_IMPLEMENTED };
601:
602: for (int i = 0; i < statusCodes.length; i++) {
603: BasicHttpResponse response = new BasicHttpResponse(
604: HttpVersion.HTTP_1_1, statusCodes[i],
605: "Unreasonable");
606: interceptor.process(response, context);
607: Header header = response
608: .getFirstHeader(HTTP.CONN_DIRECTIVE);
609: assertNotNull(header);
610: assertEquals(HTTP.CONN_CLOSE, header.getValue());
611: }
612:
613: }
614:
615: public void testResponseConnControlHostInvalidInput()
616: throws Exception {
617: ResponseConnControl interceptor = new ResponseConnControl();
618: try {
619: interceptor.process(null, null);
620: fail("IllegalArgumentException should have been thrown");
621: } catch (IllegalArgumentException ex) {
622: // expected
623: }
624: try {
625: HttpResponse response = new BasicHttpResponse(
626: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
627: interceptor.process(response, null);
628: fail("IllegalArgumentException should have been thrown");
629: } catch (IllegalArgumentException ex) {
630: // expected
631: }
632: }
633:
634: public void testResponseContentNoEntity() throws Exception {
635: HttpContext context = new BasicHttpContext(null);
636: HttpResponse response = new BasicHttpResponse(
637: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
638: ResponseContent interceptor = new ResponseContent();
639: interceptor.process(response, context);
640: Header header = response.getFirstHeader(HTTP.CONTENT_LEN);
641: assertNotNull(header);
642: assertEquals("0", header.getValue());
643: }
644:
645: public void testResponseContentStatusNoContent() throws Exception {
646: HttpContext context = new BasicHttpContext(null);
647: HttpResponse response = new BasicHttpResponse(
648: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
649: response.setStatusCode(HttpStatus.SC_NO_CONTENT);
650: ResponseContent interceptor = new ResponseContent();
651: interceptor.process(response, context);
652: Header header = response.getFirstHeader(HTTP.CONTENT_LEN);
653: assertNull(header);
654: }
655:
656: public void testResponseContentStatusResetContent()
657: throws Exception {
658: HttpContext context = new BasicHttpContext(null);
659: HttpResponse response = new BasicHttpResponse(
660: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
661: response.setStatusCode(HttpStatus.SC_RESET_CONTENT);
662: ResponseContent interceptor = new ResponseContent();
663: interceptor.process(response, context);
664: Header header = response.getFirstHeader(HTTP.CONTENT_LEN);
665: assertNull(header);
666: }
667:
668: public void testResponseContentStatusNotModified() throws Exception {
669: HttpContext context = new BasicHttpContext(null);
670: HttpResponse response = new BasicHttpResponse(
671: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
672: response.setStatusCode(HttpStatus.SC_NOT_MODIFIED);
673: ResponseContent interceptor = new ResponseContent();
674: interceptor.process(response, context);
675: Header header = response.getFirstHeader(HTTP.CONTENT_LEN);
676: assertNull(header);
677: }
678:
679: public void testResponseContentEntityChunked() throws Exception {
680: HttpContext context = new BasicHttpContext(null);
681: HttpResponse response = new BasicHttpResponse(
682: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
683: BasicHttpEntity entity = new BasicHttpEntity();
684: entity.setChunked(true);
685: response.setEntity(entity);
686: ResponseContent interceptor = new ResponseContent();
687: interceptor.process(response, context);
688: Header h1 = response.getFirstHeader(HTTP.TRANSFER_ENCODING);
689: assertNotNull(h1);
690: assertEquals(HTTP.CHUNK_CODING, h1.getValue());
691: Header h2 = response.getFirstHeader(HTTP.CONTENT_LEN);
692: assertNull(h2);
693: }
694:
695: public void testResponseContentEntityContentLenghtDelimited()
696: throws Exception {
697: HttpContext context = new BasicHttpContext(null);
698: HttpResponse response = new BasicHttpResponse(
699: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
700: BasicHttpEntity entity = new BasicHttpEntity();
701: entity.setContentLength(10);
702: response.setEntity(entity);
703: ResponseContent interceptor = new ResponseContent();
704: interceptor.process(response, context);
705: Header h1 = response.getFirstHeader(HTTP.CONTENT_LEN);
706: assertNotNull(h1);
707: assertEquals("10", h1.getValue());
708: Header h2 = response.getFirstHeader(HTTP.TRANSFER_ENCODING);
709: assertNull(h2);
710: }
711:
712: public void testResponseContentEntityUnknownContentLength()
713: throws Exception {
714: HttpContext context = new BasicHttpContext(null);
715: HttpResponse response = new BasicHttpResponse(
716: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
717: BasicHttpEntity entity = new BasicHttpEntity();
718: response.setEntity(entity);
719: ResponseContent interceptor = new ResponseContent();
720: interceptor.process(response, context);
721: Header h1 = response.getFirstHeader(HTTP.TRANSFER_ENCODING);
722: assertNull(h1);
723: Header h2 = response.getFirstHeader(HTTP.CONTENT_LEN);
724: assertNull(h2);
725: }
726:
727: public void testResponseContentEntityChunkedHTTP10()
728: throws Exception {
729: HttpContext context = new BasicHttpContext(null);
730: BasicHttpResponse response = new BasicHttpResponse(
731: HttpVersion.HTTP_1_0, HttpStatus.SC_OK, "OK");
732: BasicHttpEntity entity = new BasicHttpEntity();
733: entity.setChunked(true);
734: response.setEntity(entity);
735: ResponseContent interceptor = new ResponseContent();
736: interceptor.process(response, context);
737: Header h1 = response.getFirstHeader(HTTP.TRANSFER_ENCODING);
738: assertNull(h1);
739: Header h2 = response.getFirstHeader(HTTP.CONTENT_LEN);
740: assertNull(h2);
741: }
742:
743: public void testResponseContentEntityNoContentTypeAndEncoding()
744: throws Exception {
745: HttpContext context = new BasicHttpContext(null);
746: HttpResponse response = new BasicHttpResponse(
747: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
748: BasicHttpEntity entity = new BasicHttpEntity();
749: response.setEntity(entity);
750: ResponseContent interceptor = new ResponseContent();
751: interceptor.process(response, context);
752: Header h1 = response.getFirstHeader(HTTP.CONTENT_TYPE);
753: assertNull(h1);
754: Header h2 = response.getFirstHeader(HTTP.CONTENT_ENCODING);
755: assertNull(h2);
756: }
757:
758: public void testResponseContentEntityContentTypeAndEncoding()
759: throws Exception {
760: HttpContext context = new BasicHttpContext(null);
761: HttpResponse response = new BasicHttpResponse(
762: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
763: BasicHttpEntity entity = new BasicHttpEntity();
764: entity.setContentEncoding("whatever");
765: entity.setContentType("whatever");
766: response.setEntity(entity);
767: ResponseContent interceptor = new ResponseContent();
768: interceptor.process(response, context);
769: Header h1 = response.getFirstHeader(HTTP.CONTENT_TYPE);
770: assertNotNull(h1);
771: assertEquals("whatever", h1.getValue());
772: Header h2 = response.getFirstHeader(HTTP.CONTENT_ENCODING);
773: assertNotNull(h2);
774: assertEquals("whatever", h2.getValue());
775: }
776:
777: public void testResponseContentInvalidInput() throws Exception {
778: ResponseContent interceptor = new ResponseContent();
779: try {
780: interceptor.process(null, null);
781: fail("IllegalArgumentException should have been thrown");
782: } catch (IllegalArgumentException ex) {
783: // expected
784: }
785: }
786:
787: public void testResponseContentInvalidResponseState()
788: throws Exception {
789: ResponseContent interceptor = new ResponseContent();
790: HttpContext context = new BasicHttpContext(null);
791: try {
792: HttpResponse response = new BasicHttpResponse(
793: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
794: response.addHeader(new BasicHeader(HTTP.CONTENT_LEN, "10"));
795: interceptor.process(response, context);
796: fail("ProtocolException should have been thrown");
797: } catch (ProtocolException ex) {
798: // expected
799: }
800: try {
801: HttpResponse response = new BasicHttpResponse(
802: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
803: response.addHeader(new BasicHeader(HTTP.TRANSFER_ENCODING,
804: "stuff"));
805: interceptor.process(response, context);
806: fail("ProtocolException should have been thrown");
807: } catch (ProtocolException ex) {
808: // expected
809: }
810: }
811:
812: public void testResponseDateGenerated() throws Exception {
813: HttpContext context = new BasicHttpContext(null);
814: HttpResponse response = new BasicHttpResponse(
815: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
816: ResponseDate interceptor = new ResponseDate();
817: interceptor.process(response, context);
818: Header h1 = response.getFirstHeader(HTTP.DATE_HEADER);
819: assertNotNull(h1);
820: interceptor.process(response, context);
821: Header h2 = response.getFirstHeader(HTTP.DATE_HEADER);
822: assertNotNull(h2);
823: }
824:
825: public void testResponseDateNotGenerated() throws Exception {
826: HttpContext context = new BasicHttpContext(null);
827: HttpResponse response = new BasicHttpResponse(
828: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
829: response.setStatusCode(199);
830: ResponseDate interceptor = new ResponseDate();
831: interceptor.process(response, context);
832: Header h1 = response.getFirstHeader(HTTP.DATE_HEADER);
833: assertNull(h1);
834: }
835:
836: public void testResponseDateInvalidInput() throws Exception {
837: ResponseDate interceptor = new ResponseDate();
838: try {
839: interceptor.process(null, null);
840: fail("IllegalArgumentException should have been thrown");
841: } catch (IllegalArgumentException ex) {
842: // expected
843: }
844: }
845:
846: public void testRequestDateGenerated() throws Exception {
847: HttpContext context = new BasicHttpContext(null);
848: BasicHttpRequest request = new BasicHttpEntityEnclosingRequest(
849: "POST", "/");
850: //BasicHttpRequest request = new BasicHttpRequest("GET", "/");
851:
852: RequestDate interceptor = new RequestDate();
853: interceptor.process(request, context);
854: Header h1 = request.getFirstHeader(HTTP.DATE_HEADER);
855: assertNotNull(h1);
856: interceptor.process(request, context);
857: Header h2 = request.getFirstHeader(HTTP.DATE_HEADER);
858: assertNotNull(h2);
859: }
860:
861: public void testRequestDateNotGenerated() throws Exception {
862: HttpContext context = new BasicHttpContext(null);
863: BasicHttpRequest request = new BasicHttpRequest("GET", "/");
864:
865: RequestDate interceptor = new RequestDate();
866: interceptor.process(request, context);
867: Header h1 = request.getFirstHeader(HTTP.DATE_HEADER);
868: assertNull(h1);
869: }
870:
871: public void testRequestDateInvalidInput() throws Exception {
872: RequestDate interceptor = new RequestDate();
873: try {
874: interceptor.process(null, null);
875: fail("IllegalArgumentException should have been thrown");
876: } catch (IllegalArgumentException ex) {
877: // expected
878: }
879: }
880:
881: public void testResponseServerGenerated() throws Exception {
882: HttpContext context = new BasicHttpContext(null);
883: HttpResponse response = new BasicHttpResponse(
884: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
885: response.getParams().setParameter(
886: CoreProtocolPNames.ORIGIN_SERVER, "some server");
887: ResponseServer interceptor = new ResponseServer();
888: interceptor.process(response, context);
889: Header h1 = response.getFirstHeader(HTTP.SERVER_HEADER);
890: assertNotNull(h1);
891: assertEquals("some server", h1.getValue());
892: }
893:
894: public void testResponseServerNotGenerated() throws Exception {
895: HttpContext context = new BasicHttpContext(null);
896: HttpResponse response = new BasicHttpResponse(
897: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
898: response.getParams().setParameter(
899: CoreProtocolPNames.ORIGIN_SERVER, "some server");
900: response.addHeader(new BasicHeader(HTTP.SERVER_HEADER,
901: "whatever"));
902: ResponseServer interceptor = new ResponseServer();
903: interceptor.process(response, context);
904: Header h1 = response.getFirstHeader(HTTP.SERVER_HEADER);
905: assertNotNull(h1);
906: assertEquals("whatever", h1.getValue());
907: }
908:
909: public void testResponseServerMissing() throws Exception {
910: HttpContext context = new BasicHttpContext(null);
911: HttpResponse response = new BasicHttpResponse(
912: HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
913: ResponseServer interceptor = new ResponseServer();
914: interceptor.process(response, context);
915: Header h1 = response.getFirstHeader(HTTP.SERVER_HEADER);
916: assertNull(h1);
917: }
918:
919: public void testResponseServerInvalidInput() throws Exception {
920: ResponseServer interceptor = new ResponseServer();
921: try {
922: interceptor.process(null, null);
923: fail("IllegalArgumentException should have been thrown");
924: } catch (IllegalArgumentException ex) {
925: // expected
926: }
927: }
928:
929: }
|