1
2
3
4
5
6
7 package org.mule.transport.http.transformers;
8
9 import org.mule.DefaultMuleMessage;
10 import org.mule.api.transformer.TransformerException;
11 import org.mule.tck.junit4.AbstractMuleContextTestCase;
12 import org.mule.transformer.types.DataTypeFactory;
13 import org.mule.transport.http.HttpConstants;
14 import org.mule.transport.http.HttpResponse;
15 import org.mule.transport.http.ResponseWriter;
16
17 import org.apache.commons.httpclient.Header;
18 import org.apache.commons.httpclient.HttpVersion;
19 import org.junit.Test;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.fail;
23
24 public class HttpResponseToStringTestCase extends AbstractMuleContextTestCase
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 @Test
59 public void testTransformChunked() throws Exception
60 {
61 HttpResponseToString trasf = new HttpResponseToString();
62 trasf.setReturnDataType(DataTypeFactory.STRING);
63
64 _resp.setHeader(new Header(HttpConstants.HEADER_TRANSFER_ENCODING,
65 HttpConstants.TRANSFER_ENCODING_CHUNKED));
66 _resultChunked += "31\r\n" + _body + "\r\n0\r\n\r\n";
67
68 String trasfRes = (String)trasf.doTransform(_resp, "ISO-8859-1");
69
70 assertEquals(_resultChunked, trasfRes);
71 }
72
73
74
75
76
77
78
79 @Test
80 public void testTransformNotChunked() throws Exception
81 {
82 HttpResponseToString trasf = new HttpResponseToString();
83 trasf.setReturnDataType(DataTypeFactory.STRING);
84
85 _resultNotChunked += _body;
86
87 String trasfRes = (String)trasf.doTransform(_resp, "ISO-8859-1");
88
89 assertEquals(_resultNotChunked, trasfRes);
90 }
91
92
93
94
95 @Test
96 public void testTransformException()
97 {
98 try
99 {
100 HttpResponseToString trasf = new HttpResponseToString();
101 trasf.doTransform(_resp, "ISO-8859-20");
102
103 fail();
104 }
105 catch (TransformerException tfe)
106 {
107
108 }
109 }
110
111 }