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