View Javadoc

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