001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/impl/entity/TestEntityDeserializer.java $
003: * $Revision: 576073 $
004: * $Date: 2007-09-16 12:53:13 +0200 (Sun, 16 Sep 2007) $
005: * ====================================================================
006: * Licensed to the Apache Software Foundation (ASF) under one
007: * or more contributor license agreements. See the NOTICE file
008: * distributed with this work for additional information
009: * regarding copyright ownership. The ASF licenses this file
010: * to you under the Apache License, Version 2.0 (the
011: * "License"); you may not use this file except in compliance
012: * with the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing,
017: * software distributed under the License is distributed on an
018: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
019: * KIND, either express or implied. See the License for the
020: * specific language governing permissions and limitations
021: * under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.http.impl.entity;
032:
033: import java.io.InputStream;
034:
035: import junit.framework.Test;
036: import junit.framework.TestCase;
037: import junit.framework.TestSuite;
038:
039: import org.apache.http.HttpEntity;
040: import org.apache.http.HttpMessage;
041: import org.apache.http.ProtocolException;
042: import org.apache.http.impl.io.ChunkedInputStream;
043: import org.apache.http.impl.io.ContentLengthInputStream;
044: import org.apache.http.impl.io.IdentityInputStream;
045: import org.apache.http.io.SessionInputBuffer;
046: import org.apache.http.mockup.SessionInputBufferMockup;
047: import org.apache.http.mockup.HttpMessageMockup;
048: import org.apache.http.params.CoreProtocolPNames;
049:
050: public class TestEntityDeserializer extends TestCase {
051:
052: public TestEntityDeserializer(String testName) {
053: super (testName);
054: }
055:
056: // ------------------------------------------------------- TestCase Methods
057:
058: public static Test suite() {
059: return new TestSuite(TestEntityDeserializer.class);
060: }
061:
062: // ------------------------------------------------------------------- Main
063: public static void main(String args[]) {
064: String[] testCaseName = { TestEntityDeserializer.class
065: .getName() };
066: junit.textui.TestRunner.main(testCaseName);
067: }
068:
069: public void testIllegalGenerateArg() throws Exception {
070: EntityDeserializer entitygen = new EntityDeserializer(
071: new LaxContentLengthStrategy());
072: try {
073: entitygen.deserialize(null, null);
074: fail("IllegalArgumentException should have been thrown");
075: } catch (IllegalArgumentException ex) {
076: // expected
077: }
078: try {
079: entitygen.deserialize(new SessionInputBufferMockup(
080: new byte[] {}), null);
081: fail("IllegalArgumentException should have been thrown");
082: } catch (IllegalArgumentException ex) {
083: // expected
084: }
085: }
086:
087: public void testEntityWithTransferEncoding() throws Exception {
088: SessionInputBuffer datareceiver = new SessionInputBufferMockup(
089: "0\r\n", "US-ASCII");
090: HttpMessage message = new HttpMessageMockup();
091:
092: // lenient mode
093: message.getParams().setBooleanParameter(
094: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
095: message.addHeader("Content-Type", "unknown");
096: message.addHeader("Transfer-Encoding", "identity, chunked");
097: message.addHeader("Content-Length", "plain wrong");
098: EntityDeserializer entitygen = new EntityDeserializer(
099: new LaxContentLengthStrategy());
100: HttpEntity entity = entitygen
101: .deserialize(datareceiver, message);
102: assertNotNull(entity);
103: assertEquals(-1, entity.getContentLength());
104: assertTrue(entity.isChunked());
105: assertTrue(entity.getContent() instanceof ChunkedInputStream);
106:
107: // strict mode
108: message.getParams().setBooleanParameter(
109: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
110: entity = entitygen.deserialize(datareceiver, message);
111: assertNotNull(entity);
112: assertEquals(-1, entity.getContentLength());
113: assertTrue(entity.isChunked());
114: assertTrue(entity.getContent() instanceof ChunkedInputStream);
115: }
116:
117: public void testEntityWithIdentityTransferEncoding()
118: throws Exception {
119: SessionInputBuffer datareceiver = new SessionInputBufferMockup(
120: new byte[] {});
121: HttpMessage message = new HttpMessageMockup();
122:
123: // lenient mode
124: message.getParams().setBooleanParameter(
125: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
126: message.addHeader("Content-Type", "unknown");
127: message.addHeader("Transfer-Encoding", "identity");
128: message.addHeader("Content-Length", "plain wrong");
129: EntityDeserializer entitygen = new EntityDeserializer(
130: new LaxContentLengthStrategy());
131: HttpEntity entity = entitygen
132: .deserialize(datareceiver, message);
133: assertNotNull(entity);
134: assertEquals(-1, entity.getContentLength());
135: assertFalse(entity.isChunked());
136: }
137:
138: public void testEntityWithUnsupportedTransferEncoding()
139: throws Exception {
140: SessionInputBuffer datareceiver = new SessionInputBufferMockup(
141: "0\r\n", "US-ASCII");
142: HttpMessage message = new HttpMessageMockup();
143:
144: // lenient mode
145: message.getParams().setBooleanParameter(
146: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
147: message.addHeader("Content-Type", "unknown");
148: message.addHeader("Transfer-Encoding",
149: "whatever; param=value, chunked");
150: message.addHeader("Content-Length", "plain wrong");
151: EntityDeserializer entitygen = new EntityDeserializer(
152: new LaxContentLengthStrategy());
153: HttpEntity entity = entitygen
154: .deserialize(datareceiver, message);
155: assertNotNull(entity);
156: assertEquals(-1, entity.getContentLength());
157: assertTrue(entity.isChunked());
158: assertTrue(entity.getContent() instanceof ChunkedInputStream);
159:
160: // strict mode
161: message.getParams().setBooleanParameter(
162: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
163: try {
164: entitygen.deserialize(datareceiver, message);
165: fail("ProtocolException should have been thrown");
166: } catch (ProtocolException ex) {
167: // expected
168: }
169: }
170:
171: public void testChunkedTransferEncodingMustBeLast()
172: throws Exception {
173: SessionInputBuffer datareceiver = new SessionInputBufferMockup(
174: "0\r\n", "US-ASCII");
175: HttpMessage message = new HttpMessageMockup();
176:
177: // lenient mode
178: message.getParams().setBooleanParameter(
179: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
180: message.addHeader("Content-Type", "unknown");
181: message.addHeader("Transfer-Encoding", "chunked, identity");
182: message.addHeader("Content-Length", "plain wrong");
183: EntityDeserializer entitygen = new EntityDeserializer(
184: new LaxContentLengthStrategy());
185: HttpEntity entity = entitygen
186: .deserialize(datareceiver, message);
187: assertNotNull(entity);
188: assertEquals(-1, entity.getContentLength());
189: assertFalse(entity.isChunked());
190: assertFalse(entity.getContent() instanceof ChunkedInputStream);
191:
192: // strict mode
193: message.getParams().setBooleanParameter(
194: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
195: try {
196: entitygen.deserialize(datareceiver, message);
197: fail("ProtocolException should have been thrown");
198: } catch (ProtocolException ex) {
199: // expected
200: }
201: }
202:
203: public void testEntityWithContentLength() throws Exception {
204: SessionInputBuffer datareceiver = new SessionInputBufferMockup(
205: new byte[] {});
206: HttpMessage message = new HttpMessageMockup();
207:
208: // lenient mode
209: message.getParams().setBooleanParameter(
210: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
211: message.addHeader("Content-Type", "unknown");
212: message.addHeader("Content-Length", "0");
213: EntityDeserializer entitygen = new EntityDeserializer(
214: new LaxContentLengthStrategy());
215: HttpEntity entity = entitygen
216: .deserialize(datareceiver, message);
217: assertNotNull(entity);
218: assertEquals(0, entity.getContentLength());
219: assertFalse(entity.isChunked());
220: assertTrue(entity.getContent() instanceof ContentLengthInputStream);
221: }
222:
223: public void testEntityWithMultipleContentLength() throws Exception {
224: SessionInputBuffer datareceiver = new SessionInputBufferMockup(
225: new byte[] { '0' });
226: HttpMessage message = new HttpMessageMockup();
227:
228: // lenient mode
229: message.getParams().setBooleanParameter(
230: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
231: message.addHeader("Content-Type", "unknown");
232: message.addHeader("Content-Length", "0");
233: message.addHeader("Content-Length", "0");
234: message.addHeader("Content-Length", "1");
235: EntityDeserializer entitygen = new EntityDeserializer(
236: new LaxContentLengthStrategy());
237: HttpEntity entity = entitygen
238: .deserialize(datareceiver, message);
239: assertNotNull(entity);
240: assertEquals(1, entity.getContentLength());
241: assertFalse(entity.isChunked());
242: InputStream instream = entity.getContent();
243: assertNotNull(instream);
244: assertTrue(instream instanceof ContentLengthInputStream);
245:
246: // strict mode
247: message.getParams().setBooleanParameter(
248: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
249: try {
250: entitygen.deserialize(datareceiver, message);
251: fail("ProtocolException should have been thrown");
252: } catch (ProtocolException ex) {
253: // expected
254: }
255: }
256:
257: public void testEntityWithMultipleContentLengthSomeWrong()
258: throws Exception {
259: SessionInputBuffer datareceiver = new SessionInputBufferMockup(
260: new byte[] { '0' });
261: HttpMessage message = new HttpMessageMockup();
262:
263: // lenient mode
264: message.getParams().setBooleanParameter(
265: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
266: message.addHeader("Content-Type", "unknown");
267: message.addHeader("Content-Length", "1");
268: message.addHeader("Content-Length", "yyy");
269: message.addHeader("Content-Length", "xxx");
270: EntityDeserializer entitygen = new EntityDeserializer(
271: new LaxContentLengthStrategy());
272: HttpEntity entity = entitygen
273: .deserialize(datareceiver, message);
274: assertNotNull(entity);
275: assertEquals(1, entity.getContentLength());
276: assertFalse(entity.isChunked());
277: InputStream instream = entity.getContent();
278: assertNotNull(instream);
279: assertTrue(instream instanceof ContentLengthInputStream);
280:
281: // strict mode
282: message.getParams().setBooleanParameter(
283: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
284: try {
285: entitygen.deserialize(datareceiver, message);
286: fail("ProtocolException should have been thrown");
287: } catch (ProtocolException ex) {
288: // expected
289: }
290: }
291:
292: public void testEntityWithMultipleContentLengthAllWrong()
293: throws Exception {
294: SessionInputBuffer datareceiver = new SessionInputBufferMockup(
295: new byte[] { '0' });
296: HttpMessage message = new HttpMessageMockup();
297:
298: // lenient mode
299: message.getParams().setBooleanParameter(
300: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
301: message.addHeader("Content-Type", "unknown");
302: message.addHeader("Content-Length", "yyy");
303: message.addHeader("Content-Length", "xxx");
304: EntityDeserializer entitygen = new EntityDeserializer(
305: new LaxContentLengthStrategy());
306: HttpEntity entity = entitygen
307: .deserialize(datareceiver, message);
308: assertNotNull(entity);
309: assertEquals(-1, entity.getContentLength());
310: assertFalse(entity.isChunked());
311: InputStream instream = entity.getContent();
312: assertNotNull(instream);
313: assertFalse(instream instanceof ContentLengthInputStream);
314: assertTrue(instream instanceof IdentityInputStream);
315:
316: // strict mode
317: message.getParams().setBooleanParameter(
318: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
319: try {
320: entitygen.deserialize(datareceiver, message);
321: fail("ProtocolException should have been thrown");
322: } catch (ProtocolException ex) {
323: // expected
324: }
325: }
326:
327: public void testEntityWithInvalidContentLength() throws Exception {
328: SessionInputBuffer datareceiver = new SessionInputBufferMockup(
329: new byte[] { '0' });
330: HttpMessage message = new HttpMessageMockup();
331:
332: // lenient mode
333: message.getParams().setBooleanParameter(
334: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
335: message.addHeader("Content-Type", "unknown");
336: message.addHeader("Content-Length", "xxx");
337: EntityDeserializer entitygen = new EntityDeserializer(
338: new LaxContentLengthStrategy());
339: HttpEntity entity = entitygen
340: .deserialize(datareceiver, message);
341: assertNotNull(entity);
342: assertEquals(-1, entity.getContentLength());
343: assertFalse(entity.isChunked());
344: InputStream instream = entity.getContent();
345: assertNotNull(instream);
346: assertFalse(instream instanceof ContentLengthInputStream);
347: assertTrue(instream instanceof IdentityInputStream);
348:
349: // strict mode
350: message.getParams().setBooleanParameter(
351: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, true);
352: try {
353: entitygen.deserialize(datareceiver, message);
354: fail("ProtocolException should have been thrown");
355: } catch (ProtocolException ex) {
356: // expected
357: }
358: }
359:
360: public void testEntityNeitherContentLengthNorTransferEncoding()
361: throws Exception {
362: SessionInputBuffer datareceiver = new SessionInputBufferMockup(
363: new byte[] { '0' });
364: HttpMessage message = new HttpMessageMockup();
365:
366: // lenient mode
367: message.getParams().setBooleanParameter(
368: CoreProtocolPNames.STRICT_TRANSFER_ENCODING, false);
369: EntityDeserializer entitygen = new EntityDeserializer(
370: new LaxContentLengthStrategy());
371: HttpEntity entity = entitygen
372: .deserialize(datareceiver, message);
373: assertNotNull(entity);
374: assertEquals(-1, entity.getContentLength());
375: assertFalse(entity.isChunked());
376: InputStream instream = entity.getContent();
377: assertNotNull(instream);
378: assertFalse(instream instanceof ContentLengthInputStream);
379: assertFalse(instream instanceof ChunkedInputStream);
380: assertTrue(instream instanceof IdentityInputStream);
381: }
382:
383: public void testEntityContentType() throws Exception {
384: SessionInputBuffer datareceiver = new SessionInputBufferMockup(
385: new byte[] { '0' });
386: HttpMessage message = new HttpMessageMockup();
387:
388: message.addHeader("Content-Type", "stuff");
389: EntityDeserializer entitygen = new EntityDeserializer(
390: new LaxContentLengthStrategy());
391: HttpEntity entity = entitygen
392: .deserialize(datareceiver, message);
393: assertNotNull(entity);
394: assertNotNull(entity.getContentType());
395: assertEquals("stuff", entity.getContentType().getValue());
396: }
397:
398: public void testEntityContentEncoding() throws Exception {
399: SessionInputBuffer datareceiver = new SessionInputBufferMockup(
400: new byte[] { '0' });
401: HttpMessage message = new HttpMessageMockup();
402:
403: message.addHeader("Content-Encoding", "what not");
404: EntityDeserializer entitygen = new EntityDeserializer(
405: new LaxContentLengthStrategy());
406: HttpEntity entity = entitygen
407: .deserialize(datareceiver, message);
408: assertNotNull(entity);
409: assertNotNull(entity.getContentEncoding());
410: assertEquals("what not", entity.getContentEncoding().getValue());
411: }
412:
413: }
|