001: /*
002: * Created on 9/01/2004
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.mortbay.jetty;
008:
009: import junit.framework.TestCase;
010:
011: /**
012: * @author gregw
013: *
014: */
015: public class HttpConnectionTest extends TestCase {
016: Server server = new Server();
017: LocalConnector connector = new LocalConnector();
018:
019: /**
020: * Constructor
021: * @param arg0
022: */
023: public HttpConnectionTest(String arg0) {
024: super (arg0);
025: server.setConnectors(new Connector[] { connector });
026: server.setHandler(new DumpHandler());
027: }
028:
029: /*
030: * @see TestCase#setUp()
031: */
032: protected void setUp() throws Exception {
033: super .setUp();
034:
035: server.start();
036: }
037:
038: /*
039: * @see TestCase#tearDown()
040: */
041: protected void tearDown() throws Exception {
042: super .tearDown();
043: server.stop();
044: }
045:
046: /* --------------------------------------------------------------- */
047: public void testFragmentedChunk() {
048:
049: String response = null;
050: try {
051: int offset = 0;
052:
053: // Chunk last
054: offset = 0;
055: connector.reopen();
056: response = connector.getResponses("GET /R1 HTTP/1.1\n"
057: + "Host: localhost\n"
058: + "Transfer-Encoding: chunked\n"
059: + "Content-Type: text/plain\n" + "\015\012"
060: + "5;\015\012" + "12345\015\012"
061: + "0;\015\012\015\012");
062: offset = checkContains(response, offset, "HTTP/1.1 200");
063: offset = checkContains(response, offset, "/R1");
064: offset = checkContains(response, offset, "12345");
065:
066: response = connector.getResponses("GET /R2 HTTP/1.1\n"
067: + "Host: localhost\n"
068: + "Transfer-Encoding: chunked\n"
069: + "Content-Type: text/plain\n" + "\015\012"
070: + "5;\015\012", true);
071: response = connector.getResponses("ABCDE\015\012"
072: + "0;\015\012\015\012");
073: offset = checkContains(response, offset, "HTTP/1.1 200");
074: offset = checkContains(response, offset, "/R2");
075: offset = checkContains(response, offset, "ABCDE");
076:
077: } catch (Exception e) {
078: e.printStackTrace();
079: assertTrue(false);
080: if (response != null)
081: System.err.println(response);
082: }
083: }
084:
085: /* --------------------------------------------------------------- */
086: public void testAutoFlush() throws Exception {
087:
088: String response = null;
089: int offset = 0;
090:
091: offset = 0;
092: connector.reopen();
093: response = connector
094: .getResponses("GET /R1 HTTP/1.1\n"
095: + "Host: localhost\n"
096: + "Transfer-Encoding: chunked\n"
097: + "Content-Type: text/plain\n" + "\015\012"
098: + "5;\015\012" + "12345\015\012"
099: + "0;\015\012\015\012");
100: offset = checkContains(response, offset, "HTTP/1.1 200");
101: checkNotContained(response, offset, "IgnoreMe");
102: offset = checkContains(response, offset, "/R1");
103: offset = checkContains(response, offset, "12345");
104: }
105:
106: /* --------------------------------------------------------------- */
107: public void testCharset() {
108:
109: String response = null;
110: try {
111: int offset = 0;
112:
113: offset = 0;
114: connector.reopen();
115: response = connector.getResponses("GET /R1 HTTP/1.1\n"
116: + "Host: localhost\n"
117: + "Transfer-Encoding: chunked\n"
118: + "Content-Type: text/plain; charset=utf-8\n"
119: + "\015\012" + "5;\015\012" + "12345\015\012"
120: + "0;\015\012\015\012");
121: offset = checkContains(response, offset, "HTTP/1.1 200");
122: offset = checkContains(response, offset, "/R1");
123: offset = checkContains(response, offset, "encoding=utf-8");
124: offset = checkContains(response, offset, "12345");
125:
126: offset = 0;
127: connector.reopen();
128: response = connector
129: .getResponses("GET /R1 HTTP/1.1\n"
130: + "Host: localhost\n"
131: + "Transfer-Encoding: chunked\n"
132: + "Content-Type: text/plain; charset = iso-8859-1 ; other=value\n"
133: + "\015\012" + "5;\015\012"
134: + "12345\015\012" + "0;\015\012\015\012");
135: offset = checkContains(response, offset, "HTTP/1.1 200");
136: offset = checkContains(response, offset,
137: "encoding=iso-8859-1");
138: offset = checkContains(response, offset, "/R1");
139: offset = checkContains(response, offset, "12345");
140:
141: offset = 0;
142: connector.reopen();
143: response = connector.getResponses("GET /R1 HTTP/1.1\n"
144: + "Host: localhost\n"
145: + "Transfer-Encoding: chunked\n"
146: + "Content-Type: text/plain; charset=unknown\n"
147: + "\015\012" + "5;\015\012" + "12345\015\012"
148: + "0;\015\012\015\012");
149: offset = checkContains(response, offset, "HTTP/1.1 200");
150: offset = checkContains(response, offset, "encoding=unknown");
151: offset = checkContains(response, offset, "/R1");
152: offset = checkContains(response, offset, "12345");
153:
154: } catch (Exception e) {
155: e.printStackTrace();
156: assertTrue(false);
157: if (response != null)
158: System.err.println(response);
159: }
160: }
161:
162: private int checkContains(String s, int offset, String c) {
163: int o = s.indexOf(c, offset);
164: if (o < offset) {
165: System.err.println("FAILED");
166: System.err.println("'" + c + "' not in:");
167: System.err.println(s.substring(offset));
168: System.err.flush();
169: System.out.println("--\n" + s);
170: System.out.flush();
171: assertTrue(false);
172: }
173: return o;
174: }
175:
176: private void checkNotContained(String s, int offset, String c) {
177: int o = s.indexOf(c, offset);
178: if (o >= offset) {
179: System.err.println("FAILED");
180: System.err.println("'" + c + "' IS in:");
181: System.err.println(s.substring(offset));
182: System.err.flush();
183: System.out.println("--\n" + s);
184: System.out.flush();
185: assertTrue(false);
186: }
187: }
188:
189: }
|