1   /*
2    * $Id: HttpResponseToStringTestCase.java 11186 2008-03-05 23:11:10Z dandiep $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.AbstractMuleTestCase;
16  import org.mule.transport.http.HttpConstants;
17  import org.mule.transport.http.HttpResponse;
18  import org.mule.transport.http.ResponseWriter;
19  
20  import org.apache.commons.httpclient.Header;
21  import org.apache.commons.httpclient.HttpVersion;
22  
23  public class HttpResponseToStringTestCase extends AbstractMuleTestCase
24  {
25      private final String _statusLine = "HTTP/1.1 200 OK";
26      private final String _headerCT = "Content-Type: text/plain";
27      private final String _headerTE = "Transfer-Encoding: chunked";
28      private final String _contentLength = "Content-Length: ";
29      private final String _body = "<html><head></head><body><p>WOW</p></body></html>";
30  
31      private String _resultChunked = _statusLine + ResponseWriter.CRLF + _headerCT + ResponseWriter.CRLF
32                                      + _contentLength + _body.length() + ResponseWriter.CRLF + _headerTE
33                                      + ResponseWriter.CRLF + ResponseWriter.CRLF;
34      private String _resultNotChunked = _statusLine + ResponseWriter.CRLF + _headerCT + ResponseWriter.CRLF
35                                         + _contentLength + _body.length() + ResponseWriter.CRLF
36                                         + ResponseWriter.CRLF;
37  
38      private HttpResponse _resp = null;
39  
40      // @Override
41      protected void doSetUp() throws Exception
42      {
43          super.doSetUp();
44  
45          _resp = new HttpResponse();
46          _resp.setStatusLine(new HttpVersion(1, 1), 200);
47          _resp.setHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, HttpConstants.DEFAULT_CONTENT_TYPE));
48          _resp.setBody(new DefaultMuleMessage(_body));
49      }
50  
51      /**
52       * Check consistency of the transformed {@link HttpResponse} string when HTTP
53       * transfer encoding is chunked
54       * 
55       * @throws Exception
56       */
57      public void testTransformChunked() throws Exception
58      {
59          HttpResponseToString trasf = new HttpResponseToString();
60          trasf.setReturnClass(String.class);
61  
62          _resp.setHeader(new Header(HttpConstants.HEADER_TRANSFER_ENCODING,
63              HttpConstants.TRANSFER_ENCODING_CHUNKED));
64          _resultChunked += "31\r\n" + _body + "\r\n0\r\n\r\n";
65  
66          String trasfRes = (String)trasf.doTransform(_resp, "ISO-8859-1");
67  
68          assertEquals(_resultChunked, trasfRes);
69      }
70  
71      /**
72       * Check consistency of the transformed {@link HttpResponse} string when HTTP
73       * transfer encoding is chunked
74       * 
75       * @throws Exception
76       */
77      public void testTransformNotChunked() throws Exception
78      {
79          HttpResponseToString trasf = new HttpResponseToString();
80          trasf.setReturnClass(String.class);
81  
82          _resultNotChunked += _body;
83  
84          String trasfRes = (String)trasf.doTransform(_resp, "ISO-8859-1");
85  
86          assertEquals(_resultNotChunked, trasfRes);
87      }
88  
89      /**
90       * Expect a {@link TransformerException} when the encoding is not supported.
91       */
92      public void testTransformException()
93      {
94          try
95          {
96              HttpResponseToString trasf = new HttpResponseToString();
97              trasf.doTransform(_resp, "ISO-8859-20");
98  
99              fail();
100         }
101         catch (TransformerException tfe)
102         {
103             // Expected
104         }
105     }
106 
107 }