1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.impl.io;
29
30 import java.io.InterruptedIOException;
31
32 import org.apache.http.Consts;
33 import org.apache.http.Header;
34 import org.apache.http.HttpResponse;
35 import org.apache.http.HttpVersion;
36 import org.apache.http.NoHttpResponseException;
37 import org.apache.http.StatusLine;
38 import org.apache.http.impl.SessionInputBufferMock;
39 import org.apache.http.io.SessionInputBuffer;
40 import org.junit.Assert;
41 import org.junit.Test;
42
43
44
45
46 public class TestResponseParser {
47
48 @Test(expected=IllegalArgumentException.class)
49 public void testInvalidConstructorInput() throws Exception {
50 new DefaultHttpResponseParser(null);
51 }
52
53 @Test
54 public void testBasicMessageParsing() throws Exception {
55 final String s =
56 "HTTP/1.1 200 OK\r\n" +
57 "Server: whatever\r\n" +
58 "Date: some date\r\n" +
59 "Set-Cookie: c1=stuff\r\n" +
60 "\r\n";
61 final SessionInputBuffer inBuffer = new SessionInputBufferMock(s, Consts.ASCII);
62
63 final DefaultHttpResponseParser parser = new DefaultHttpResponseParser(inBuffer);
64 final HttpResponse httpresponse = parser.parse();
65
66 final StatusLine statusline = httpresponse.getStatusLine();
67 Assert.assertNotNull(statusline);
68 Assert.assertEquals(200, statusline.getStatusCode());
69 Assert.assertEquals("OK", statusline.getReasonPhrase());
70 Assert.assertEquals(HttpVersion.HTTP_1_1, statusline.getProtocolVersion());
71 final Header[] headers = httpresponse.getAllHeaders();
72 Assert.assertEquals(3, headers.length);
73 }
74
75 @Test
76 public void testConnectionClosedException() throws Exception {
77 final SessionInputBuffer inBuffer = new SessionInputBufferMock(new byte[] {});
78
79 final DefaultHttpResponseParser parser = new DefaultHttpResponseParser(inBuffer);
80 try {
81 parser.parse();
82 Assert.fail("NoHttpResponseException should have been thrown");
83 } catch (final NoHttpResponseException expected) {
84 }
85 }
86
87 @Test
88 public void testMessageParsingTimeout() throws Exception {
89 final String s =
90 "HTTP\000/1.1 200\000 OK\r\n" +
91 "Server: wha\000tever\r\n" +
92 "Date: some date\r\n" +
93 "Set-Coo\000kie: c1=stuff\r\n" +
94 "\000\r\n";
95 final SessionInputBuffer inBuffer = new SessionInputBufferMock(
96 new TimeoutByteArrayInputStream(s.getBytes(Consts.ASCII)), 16);
97
98 final DefaultHttpResponseParser parser = new DefaultHttpResponseParser(inBuffer);
99
100 int timeoutCount = 0;
101
102 HttpResponse httpresponse = null;
103 for (int i = 0; i < 10; i++) {
104 try {
105 httpresponse = parser.parse();
106 break;
107 } catch (final InterruptedIOException ex) {
108 timeoutCount++;
109 }
110
111 }
112 Assert.assertNotNull(httpresponse);
113 Assert.assertEquals(5, timeoutCount);
114
115 final StatusLine statusline = httpresponse.getStatusLine();
116 Assert.assertNotNull(statusline);
117 Assert.assertEquals(200, statusline.getStatusCode());
118 Assert.assertEquals("OK", statusline.getReasonPhrase());
119 Assert.assertEquals(HttpVersion.HTTP_1_1, statusline.getProtocolVersion());
120 final Header[] headers = httpresponse.getAllHeaders();
121 Assert.assertEquals(3, headers.length);
122 }
123
124 }
125