001: package org.mortbay.jetty;
002:
003: import java.io.IOException;
004:
005: import org.mortbay.io.Buffer;
006: import org.mortbay.io.ByteArrayBuffer;
007: import org.mortbay.io.ByteArrayEndPoint;
008: import org.mortbay.io.SimpleBuffers;
009: import org.mortbay.io.View;
010:
011: import junit.framework.TestCase;
012:
013: public class HttpGeneratorClientTest extends TestCase {
014: public final static String CONTENT = "The quick brown fox jumped over the lazy dog.\nNow is the time for all good men to come to the aid of the party\nThe moon is blue to a fish in love.\n";
015: public final static String[] connect = { null, "keep-alive",
016: "close" };
017:
018: public HttpGeneratorClientTest(String arg0) {
019: super (arg0);
020: }
021:
022: public static void main(String[] args) {
023: junit.textui.TestRunner.run(HttpGeneratorTest.class);
024: }
025:
026: public void testHTTP() throws Exception {
027: Buffer bb = new ByteArrayBuffer(8096);
028: Buffer sb = new ByteArrayBuffer(1500);
029: HttpFields fields = new HttpFields();
030: ByteArrayEndPoint endp = new ByteArrayEndPoint(new byte[0],
031: 4096);
032: HttpGenerator hb = new HttpGenerator(new SimpleBuffers(
033: new Buffer[] { sb, bb }), endp, sb.capacity(), bb
034: .capacity());
035: Handler handler = new Handler();
036: HttpParser parser = null;
037:
038: // For HTTP version
039: for (int v = 9; v <= 11; v++) {
040: // For each test result
041: for (int r = 0; r < tr.length; r++) {
042: // chunks = 1 to 3
043: for (int chunks = 1; chunks <= 6; chunks++) {
044: // For none, keep-alive, close
045: for (int c = 0; c < connect.length; c++) {
046: String t = "v=" + v + ",r=" + r + ",chunks="
047: + chunks + ",connect=" + connect[c]
048: + ",tr=" + tr[r];
049: // System.err.println(t);
050:
051: hb.reset(true);
052: endp.reset();
053: fields.clear();
054:
055: // System.out.println("TEST: "+t);
056:
057: try {
058: tr[r].build(v, hb, connect[c], null,
059: chunks, fields);
060: } catch (IllegalStateException e) {
061: if (v == 10 && chunks > 2)
062: continue;
063: throw e;
064: }
065: String request = endp.getOut().toString();
066: // System.out.println(request+(hb.isPersistent()?"...\n":"---\n"));
067:
068: assertTrue(t, hb.isPersistent());
069:
070: if (v == 9) {
071: assertEquals(t,
072: "GET /context/path/info\r\n",
073: request);
074: continue;
075: }
076:
077: parser = new HttpParser(new ByteArrayBuffer(
078: request.getBytes()), handler);
079: try {
080: parser.parse();
081: } catch (IOException e) {
082: if (tr[r].body != null)
083: throw e;
084: continue;
085: }
086:
087: if (tr[r].body != null)
088: assertEquals(t, tr[r].body, this .content);
089: if (v == 10)
090: assertTrue(t, hb.isPersistent()
091: || tr[r].values[1] == null
092: || c == 2 || c == 0);
093: else
094: assertTrue(t, hb.isPersistent() || c == 2);
095:
096: assertTrue(t, tr[r].values[1] == null
097: || content.length() == Integer
098: .parseInt(tr[r].values[1]));
099: }
100: }
101: }
102: }
103: }
104:
105: static final String[] headers = { "Content-Type", "Content-Length",
106: "Connection", "Transfer-Encoding", "Other" };
107:
108: class TR {
109: String[] values = new String[headers.length];
110: String body;
111:
112: TR(String ct, String cl, String content) {
113: values[0] = ct;
114: values[1] = cl;
115: values[4] = "value";
116: this .body = content;
117: }
118:
119: void build(int version, HttpGenerator hb, String connection,
120: String te, int chunks, HttpFields fields)
121: throws Exception {
122: values[2] = connection;
123: values[3] = te;
124:
125: hb.setRequest(HttpMethods.GET, "/context/path/info");
126: hb.setVersion(version);
127:
128: for (int i = 0; i < headers.length; i++) {
129: if (values[i] == null)
130: continue;
131: fields.put(new ByteArrayBuffer(headers[i]),
132: new ByteArrayBuffer(values[i]));
133: }
134:
135: if (body != null) {
136: int inc = 1 + body.length() / chunks;
137: Buffer buf = new ByteArrayBuffer(body);
138: View view = new View(buf);
139: for (int i = 1; i < chunks; i++) {
140: view.setPutIndex(i * inc);
141: view.setGetIndex((i - 1) * inc);
142: hb.addContent(view, HttpGenerator.MORE);
143: if (hb.isBufferFull()
144: && hb.isState(HttpGenerator.STATE_HEADER))
145: hb.completeHeader(fields, HttpGenerator.MORE);
146: if (i % 2 == 0) {
147: if (hb.isState(HttpGenerator.STATE_HEADER))
148: hb.completeHeader(fields,
149: HttpGenerator.MORE);
150: hb.flush();
151: }
152: }
153: view.setPutIndex(buf.putIndex());
154: view.setGetIndex((chunks - 1) * inc);
155: hb.addContent(view, HttpGenerator.LAST);
156: if (hb.isState(HttpGenerator.STATE_HEADER))
157: hb.completeHeader(fields, HttpGenerator.LAST);
158: } else {
159: hb.completeHeader(fields, HttpGenerator.LAST);
160: }
161: hb.complete();
162: }
163:
164: public String toString() {
165: return "[" + values[0] + "," + values[1] + ","
166: + (body == null ? "none" : "_content") + "]";
167: }
168: }
169:
170: private TR[] tr = {
171: /* 0 */new TR(null, null, null),
172: /* 1 */new TR(null, null, CONTENT),
173: /* 3 */new TR(null, "" + CONTENT.length(), CONTENT),
174: /* 4 */new TR("text/html", null, null),
175: /* 5 */new TR("text/html", null, CONTENT),
176: /* 7 */new TR("text/html", "" + CONTENT.length(), CONTENT), };
177:
178: String content;
179: String f0;
180: String f1;
181: String f2;
182: String[] hdr;
183: String[] val;
184: int h;
185:
186: class Handler extends HttpParser.EventHandler {
187: int index = 0;
188:
189: public void content(Buffer ref) {
190: if (index == 0)
191: content = "";
192: content = content.substring(0, index) + ref;
193: index += ref.length();
194: }
195:
196: public void startRequest(Buffer tok0, Buffer tok1, Buffer tok2) {
197: h = -1;
198: hdr = new String[9];
199: val = new String[9];
200: f0 = tok0.toString();
201: f1 = tok1.toString();
202: if (tok2 != null)
203: f2 = tok2.toString();
204: else
205: f2 = null;
206: index = 0;
207: // System.out.println(f0+" "+f1+" "+f2);
208: }
209:
210: /* (non-Javadoc)
211: * @see org.mortbay.jetty.EventHandler#startResponse(org.mortbay.io.Buffer, int, org.mortbay.io.Buffer)
212: */
213: public void startResponse(Buffer version, int status,
214: Buffer reason) {
215: h = -1;
216: hdr = new String[9];
217: val = new String[9];
218: f0 = version.toString();
219: f1 = "" + status;
220: if (reason != null)
221: f2 = reason.toString();
222: else
223: f2 = null;
224: index = 0;
225: }
226:
227: public void parsedHeader(Buffer name, Buffer value) {
228: hdr[++h] = name.toString();
229: val[h] = value.toString();
230: }
231:
232: public void headerComplete() {
233: content = null;
234: }
235:
236: public void messageComplete(long contentLength) {
237: }
238:
239: }
240:
241: }
|