001: //========================================================================
002: //$Id: HttpGeneratorTest.java,v 1.1 2005/10/05 14:09:41 janb Exp $
003: //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.jetty;
017:
018: import java.io.IOException;
019:
020: import junit.framework.TestCase;
021:
022: import org.mortbay.io.Buffer;
023: import org.mortbay.io.ByteArrayBuffer;
024: import org.mortbay.io.ByteArrayEndPoint;
025: import org.mortbay.io.SimpleBuffers;
026: import org.mortbay.io.View;
027:
028: /**
029: * @author gregw
030: *
031: * To change the template for this generated type comment go to
032: * Window - Preferences - Java - Code Generation - Code and Comments
033: */
034: public class HttpGeneratorTest extends TestCase {
035: 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";
036: public final static String[] connect = { null, "keep-alive",
037: "close" };
038:
039: public HttpGeneratorTest(String arg0) {
040: super (arg0);
041: }
042:
043: public static void main(String[] args) {
044: junit.textui.TestRunner.run(HttpGeneratorTest.class);
045: }
046:
047: public void testHTTP() throws Exception {
048: Buffer bb = new ByteArrayBuffer(8096);
049: Buffer sb = new ByteArrayBuffer(1500);
050: HttpFields fields = new HttpFields();
051: ByteArrayEndPoint endp = new ByteArrayEndPoint(new byte[0],
052: 4096);
053: HttpGenerator hb = new HttpGenerator(new SimpleBuffers(
054: new Buffer[] { sb, bb }), endp, sb.capacity(), bb
055: .capacity());
056: Handler handler = new Handler();
057: HttpParser parser = null;
058:
059: // For HTTP version
060: for (int v = 9; v <= 11; v++) {
061: // For each test result
062: for (int r = 0; r < tr.length; r++) {
063: // chunks = 1 to 3
064: for (int chunks = 1; chunks <= 6; chunks++) {
065: // For none, keep-alive, close
066: for (int c = 0; c < connect.length; c++) {
067: String t = "v=" + v + ",r=" + r + ",chunks="
068: + chunks + ",connect=" + connect[c]
069: + ",tr=" + tr[r];
070: // System.err.println(t);
071:
072: hb.reset(true);
073: endp.reset();
074: fields.clear();
075:
076: tr[r].build(v, hb, null, connect[c], null,
077: chunks, fields);
078: String response = endp.getOut().toString();
079: // System.out.println("RESPONSE: "+t+"\n"+response+(hb.isPersistent()?"...\n":"---\n"));
080:
081: if (v == 9) {
082: assertFalse(t, hb.isPersistent());
083: if (tr[r].body != null)
084: assertEquals(t, tr[r].body, response);
085: continue;
086: }
087:
088: parser = new HttpParser(new ByteArrayBuffer(
089: response.getBytes()), handler);
090: try {
091: parser.parse();
092: } catch (IOException e) {
093: if (tr[r].body != null)
094: throw e;
095: continue;
096: }
097:
098: if (tr[r].body != null)
099: assertEquals(t, tr[r].body, this .content);
100: if (v == 10)
101: assertTrue(t, hb.isPersistent()
102: || tr[r].values[1] == null
103: || c == 2 || c == 0);
104: else
105: assertTrue(t, hb.isPersistent() || c == 2);
106:
107: assertTrue(t, tr[r].values[1] == null
108: || content.length() == Integer
109: .parseInt(tr[r].values[1]));
110: }
111: }
112: }
113: }
114: }
115:
116: static final String[] headers = { "Content-Type", "Content-Length",
117: "Connection", "Transfer-Encoding", "Other" };
118:
119: class TR {
120: int code;
121: String[] values = new String[headers.length];
122: String body;
123:
124: TR(int code, String ct, String cl, String content) {
125: this .code = code;
126: values[0] = ct;
127: values[1] = cl;
128: values[4] = "value";
129: this .body = content;
130: }
131:
132: void build(int version, HttpGenerator hb, String reason,
133: String connection, String te, int chunks,
134: HttpFields fields) throws Exception {
135: values[2] = connection;
136: values[3] = te;
137: hb.setVersion(version);
138: hb.setResponse(code, reason);
139:
140: for (int i = 0; i < headers.length; i++) {
141: if (values[i] == null)
142: continue;
143: fields.put(new ByteArrayBuffer(headers[i]),
144: new ByteArrayBuffer(values[i]));
145: }
146:
147: if (body != null) {
148: int inc = 1 + body.length() / chunks;
149: Buffer buf = new ByteArrayBuffer(body);
150: View view = new View(buf);
151: for (int i = 1; i < chunks; i++) {
152: view.setPutIndex(i * inc);
153: view.setGetIndex((i - 1) * inc);
154: hb.addContent(view, HttpGenerator.MORE);
155: if (hb.isBufferFull()
156: && hb.isState(HttpGenerator.STATE_HEADER))
157: hb.completeHeader(fields, HttpGenerator.MORE);
158: if (i % 2 == 0) {
159: if (hb.isState(HttpGenerator.STATE_HEADER))
160: hb.completeHeader(fields,
161: HttpGenerator.MORE);
162: hb.flush();
163: }
164: }
165: view.setPutIndex(buf.putIndex());
166: view.setGetIndex((chunks - 1) * inc);
167: hb.addContent(view, HttpGenerator.LAST);
168: if (hb.isState(HttpGenerator.STATE_HEADER))
169: hb.completeHeader(fields, HttpGenerator.LAST);
170: } else {
171: hb.completeHeader(fields, HttpGenerator.LAST);
172: }
173: hb.complete();
174: }
175:
176: public String toString() {
177: return "[" + code + "," + values[0] + "," + values[1] + ","
178: + (body == null ? "none" : "_content") + "]";
179: }
180: }
181:
182: private TR[] tr = {
183: /* 0 */new TR(200, null, null, null),
184: /* 1 */new TR(200, null, null, CONTENT),
185: /* 2 */new TR(200, null, "" + CONTENT.length(), null),
186: /* 3 */new TR(200, null, "" + CONTENT.length(), CONTENT),
187: /* 4 */new TR(200, "text/html", null, null),
188: /* 5 */new TR(200, "text/html", null, CONTENT),
189: /* 6 */new TR(200, "text/html", "" + CONTENT.length(), null),
190: /* 7 */new TR(200, "text/html", "" + CONTENT.length(), CONTENT), };
191:
192: String content;
193: String f0;
194: String f1;
195: String f2;
196: String[] hdr;
197: String[] val;
198: int h;
199:
200: class Handler extends HttpParser.EventHandler {
201: int index = 0;
202:
203: public void content(Buffer ref) {
204: if (index == 0)
205: content = "";
206: content = content.substring(0, index) + ref;
207: index += ref.length();
208: }
209:
210: public void startRequest(Buffer tok0, Buffer tok1, Buffer tok2) {
211: h = -1;
212: hdr = new String[9];
213: val = new String[9];
214: f0 = tok0.toString();
215: f1 = tok1.toString();
216: if (tok2 != null)
217: f2 = tok2.toString();
218: else
219: f2 = null;
220: index = 0;
221: // System.out.println(f0+" "+f1+" "+f2);
222: }
223:
224: /* (non-Javadoc)
225: * @see org.mortbay.jetty.EventHandler#startResponse(org.mortbay.io.Buffer, int, org.mortbay.io.Buffer)
226: */
227: public void startResponse(Buffer version, int status,
228: Buffer reason) {
229: h = -1;
230: hdr = new String[9];
231: val = new String[9];
232: f0 = version.toString();
233: f1 = "" + status;
234: if (reason != null)
235: f2 = reason.toString();
236: else
237: f2 = null;
238: index = 0;
239: }
240:
241: public void parsedHeader(Buffer name, Buffer value) {
242: hdr[++h] = name.toString();
243: val[h] = value.toString();
244: }
245:
246: public void headerComplete() {
247: content = null;
248: }
249:
250: public void messageComplete(long contentLength) {
251: }
252:
253: }
254:
255: }
|