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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
//
// ========================================================================
// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.test.support.rawhttp;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
public class HttpResponseTesterTest
{
@Test
public void testHttp11Response() throws IOException
{
StringBuffer rawResponse = new StringBuffer();
rawResponse.append("HTTP/1.1 200 OK\n");
rawResponse.append("Date: Mon, 08 Jun 2009 22:56:04 GMT\n");
rawResponse.append("Content-Type: text/plain\n");
rawResponse.append("Content-Length: 28\n");
rawResponse.append("Last-Modified: Mon, 08 Jun 2009 17:06:22 GMT\n");
rawResponse.append("Connection: close\n");
rawResponse.append("Server: Jetty(7.0.y.z-SNAPSHOT\n");
rawResponse.append("\n");
rawResponse.append("ABCDEFGHIJKLMNOPQRSTTUVWXYZ\n");
rawResponse.append("\n");
HttpResponseTester response = new HttpResponseTester();
response.parse(rawResponse);
Assert.assertEquals("Response.version","HTTP/1.1",response.getVersion());
Assert.assertEquals("Response.status",200,response.getStatus());
Assert.assertEquals("Response.reason","OK",response.getReason());
Assert.assertEquals("Response[Content-Type]","text/plain",response.getContentType());
Assert.assertEquals("Response[Content-Length]",28,response.getLongHeader("Content-Length"));
Assert.assertEquals("Response[Connection]","close",response.getHeader("Connection"));
String expected = "ABCDEFGHIJKLMNOPQRSTTUVWXYZ\n";
Assert.assertEquals("Response.content",expected,response.getContent().toString());
}
@Test
public void testMultiHttp11Response() throws IOException
{
StringBuffer rawResponse = new StringBuffer();
rawResponse.append("HTTP/1.1 200 OK\n");
rawResponse.append("Date: Mon, 08 Jun 2009 23:05:26 GMT\n");
rawResponse.append("Content-Type: text/plain\n");
rawResponse.append("Content-Length: 28\n");
rawResponse.append("Last-Modified: Mon, 08 Jun 2009 17:06:22 GMT\n");
rawResponse.append("Server: Jetty(7.0.y.z-SNAPSHOT)\n");
rawResponse.append("\n");
rawResponse.append("ABCDEFGHIJKLMNOPQRSTTUVWXYZ\n");
rawResponse.append("HTTP/1.1 200 OK\n");
rawResponse.append("Date: Mon, 08 Jun 2009 23:05:26 GMT\n");
rawResponse.append("Content-Type: text/plain\n");
rawResponse.append("Content-Length: 25\n");
rawResponse.append("Last-Modified: Mon, 08 Jun 2009 17:06:22 GMT\n");
rawResponse.append("Server: Jetty(7.0.y.z-SNAPSHOT)\n");
rawResponse.append("\n");
rawResponse.append("Host=Default\n");
rawResponse.append("Resource=R1\n");
rawResponse.append("HTTP/1.1 200 OK\n");
rawResponse.append("Date: Mon, 08 Jun 2009 23:05:26 GMT\n");
rawResponse.append("Content-Type: text/plain\n");
rawResponse.append("Content-Length: 25\n");
rawResponse.append("Last-Modified: Mon, 08 Jun 2009 17:06:22 GMT\n");
rawResponse.append("Connection: close\n");
rawResponse.append("Server: Jetty(7.0.y.z-SNAPSHOT)\n");
rawResponse.append("\n");
rawResponse.append("Host=Default\n");
rawResponse.append("Resource=R2\n");
rawResponse.append("\n");
List<HttpResponseTester> responses = HttpResponseTester.parseMulti(rawResponse);
Assert.assertNotNull("Responses should not be null",responses);
Assert.assertEquals("Responses.size",3,responses.size());
HttpResponseTester resp1 = responses.get(0);
resp1.assertStatusOK();
resp1.assertContentType("text/plain");
resp1.assertBody("ABCDEFGHIJKLMNOPQRSTTUVWXYZ\n");
assertThat(resp1.getHeader("Connection"),is(not("close")));
HttpResponseTester resp2 = responses.get(1);
resp2.assertStatusOK();
resp2.assertContentType("text/plain");
resp2.assertBody("Host=Default\nResource=R1\n");
assertThat(resp2.getHeader("Connection"),is(not("close")));
HttpResponseTester resp3 = responses.get(2);
resp3.assertStatusOK();
resp3.assertContentType("text/plain");
resp3.assertBody("Host=Default\nResource=R2\n");
assertThat(resp3.getHeader("Connection"),is("close"));
}
}
|