1
2
3
4
5
6
7
8
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
54
55
56
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
74
75
76
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
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
105 }
106 }
107
108 }