View Javadoc

1   /*
2    * $Id: HttpResponseToStringTestCase.java 22387 2011-07-12 03:53:36Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.http.transformers;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.transformer.TransformerException;
15  import org.mule.tck.junit4.AbstractMuleContextTestCase;
16  import org.mule.transformer.types.DataTypeFactory;
17  import org.mule.transport.http.HttpConstants;
18  import org.mule.transport.http.HttpResponse;
19  import org.mule.transport.http.ResponseWriter;
20  
21  import org.apache.commons.httpclient.Header;
22  import org.apache.commons.httpclient.HttpVersion;
23  import org.junit.Test;
24  
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.fail;
27  
28  public class HttpResponseToStringTestCase extends AbstractMuleContextTestCase
29  {
30      private final String _statusLine = "HTTP/1.1 200 OK";
31      private final String _headerCT = "Content-Type: text/plain";
32      private final String _headerTE = "Transfer-Encoding: chunked";
33      private final String _contentLength = "Content-Length: ";
34      private final String _body = "<html><head></head><body><p>WOW</p></body></html>";
35  
36      private String _resultChunked = _statusLine + ResponseWriter.CRLF + _headerCT + ResponseWriter.CRLF
37                                      + _contentLength + _body.length() + ResponseWriter.CRLF + _headerTE
38                                      + ResponseWriter.CRLF + ResponseWriter.CRLF;
39      private String _resultNotChunked = _statusLine + ResponseWriter.CRLF + _headerCT + ResponseWriter.CRLF
40                                         + _contentLength + _body.length() + ResponseWriter.CRLF
41                                         + ResponseWriter.CRLF;
42  
43      private HttpResponse _resp = null;
44  
45      @Override
46      protected void doSetUp() throws Exception
47      {
48          super.doSetUp();
49  
50          _resp = new HttpResponse();
51          _resp.setStatusLine(new HttpVersion(1, 1), 200);
52          _resp.setHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, HttpConstants.DEFAULT_CONTENT_TYPE));
53          _resp.setBody(new DefaultMuleMessage(_body, muleContext));
54      }
55  
56      /**
57       * Check consistency of the transformed {@link HttpResponse} string when HTTP
58       * transfer encoding is chunked
59       * 
60       * @throws Exception
61       */
62      @Test
63      public void testTransformChunked() throws Exception
64      {
65          HttpResponseToString trasf = new HttpResponseToString();
66          trasf.setReturnDataType(DataTypeFactory.STRING);
67  
68          _resp.setHeader(new Header(HttpConstants.HEADER_TRANSFER_ENCODING,
69              HttpConstants.TRANSFER_ENCODING_CHUNKED));
70          _resultChunked += "31\r\n" + _body + "\r\n0\r\n\r\n";
71  
72          String trasfRes = (String)trasf.doTransform(_resp, "ISO-8859-1");
73  
74          assertEquals(_resultChunked, trasfRes);
75      }
76  
77      /**
78       * Check consistency of the transformed {@link HttpResponse} string when HTTP
79       * transfer encoding is chunked
80       * 
81       * @throws Exception
82       */
83      @Test
84      public void testTransformNotChunked() throws Exception
85      {
86          HttpResponseToString trasf = new HttpResponseToString();
87          trasf.setReturnDataType(DataTypeFactory.STRING);
88  
89          _resultNotChunked += _body;
90  
91          String trasfRes = (String)trasf.doTransform(_resp, "ISO-8859-1");
92  
93          assertEquals(_resultNotChunked, trasfRes);
94      }
95  
96      /**
97       * Expect a {@link TransformerException} when the encoding is not supported.
98       */
99      @Test
100     public void testTransformException()
101     {
102         try
103         {
104             HttpResponseToString trasf = new HttpResponseToString();
105             trasf.doTransform(_resp, "ISO-8859-20");
106 
107             fail();
108         }
109         catch (TransformerException tfe)
110         {
111             // Expected
112         }
113     }
114 
115 }