1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.http.functional;
12
13 import static org.junit.Assert.assertEquals;
14
15 import org.apache.commons.httpclient.HttpClient;
16 import org.apache.commons.httpclient.methods.GetMethod;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.mule.tck.junit4.FunctionalTestCase;
20 import org.mule.tck.junit4.rule.DynamicPort;
21 import org.mule.transport.http.HttpConstants;
22 import org.mule.util.ClassUtils;
23
24 public class StaticResourcesMPFunctionalTestCase extends FunctionalTestCase
25 {
26 @Rule
27 public DynamicPort port1 = new DynamicPort("port1");
28
29 @Rule
30 public DynamicPort port2 = new DynamicPort("port2");
31
32 public StaticResourcesMPFunctionalTestCase()
33 {
34 System.setProperty("test.root",
35 ClassUtils.getClassPathRoot(StaticResourcesMPFunctionalTestCase.class).getPath());
36 }
37
38 @Override
39 protected void doSetUp() throws Exception
40 {
41 super.doSetUp();
42 }
43
44 protected String getConfigResources()
45 {
46 return "http-static-resource-test.xml";
47 }
48
49 @Test
50 public void testHttpStaticResource() throws Exception
51 {
52 String url = String.format("http://localhost:%1d/static", port1.getNumber());
53
54 GetMethod method = new GetMethod(url);
55 HttpClient client = new HttpClient();
56
57
58 int responseCode = client.executeMethod(method);
59 assertEquals(HttpConstants.SC_OK, responseCode);
60
61 String result = method.getResponseBodyAsString();
62 assertEquals(result, "Test index.html");
63
64
65 method = new GetMethod(url + "/main.html");
66 responseCode = client.executeMethod(method);
67 assertEquals(HttpConstants.SC_OK, responseCode);
68
69 result = method.getResponseBodyAsString();
70 assertEquals(result, "Test main.html");
71
72
73 method = new GetMethod(url + "/foo.html");
74 responseCode = client.executeMethod(method);
75 assertEquals(HttpConstants.SC_NOT_FOUND, responseCode);
76
77 }
78
79 @Test
80 public void testHttpStaticResourceMimeTypes() throws Exception
81 {
82 String url = String.format("http://localhost:%1d/static", port1.getNumber());
83
84 GetMethod method = new GetMethod(url);
85 HttpClient client = new HttpClient();
86
87
88 int responseCode = client.executeMethod(method);
89 assertEquals(HttpConstants.SC_OK, responseCode);
90
91 String result = method.getResponseBodyAsString();
92 assertEquals(result, "Test index.html");
93 assertEquals("text/html", method.getResponseHeader("Content-Type").getValue());
94
95
96 method = new GetMethod(url + "/image.gif");
97 responseCode = client.executeMethod(method);
98 assertEquals(HttpConstants.SC_OK, responseCode);
99 assertEquals("image/gif", method.getResponseHeader("Content-Type").getValue());
100
101
102 method = new GetMethod(url + "/image.png");
103 responseCode = client.executeMethod(method);
104 assertEquals(HttpConstants.SC_OK, responseCode);
105 assertEquals("image/png", method.getResponseHeader("Content-Type").getValue());
106 }
107
108 @Test
109 public void testHttpsStaticResource() throws Exception
110 {
111 String url = String.format("https://localhost:%2d/static", port2.getNumber());
112
113 GetMethod method = new GetMethod(url);
114 HttpClient client = new HttpClient();
115
116
117 int responseCode = client.executeMethod(method);
118 assertEquals(HttpConstants.SC_OK, responseCode);
119
120 String result = method.getResponseBodyAsString();
121 assertEquals(result, "Test index.html");
122
123
124 method = new GetMethod(url + "/main.html");
125 responseCode = client.executeMethod(method);
126 assertEquals(HttpConstants.SC_OK, responseCode);
127
128 result = method.getResponseBodyAsString();
129 assertEquals(result, "Test main.html");
130
131
132 method = new GetMethod(url + "/foo.html");
133 responseCode = client.executeMethod(method);
134 assertEquals(HttpConstants.SC_NOT_FOUND, responseCode);
135
136 }
137
138
139
140
141
142
143
144 @Test
145 public void testFlowBindingOnSamePort() throws Exception
146 {
147 String url = String.format("http://localhost:%1d/echo", port1.getNumber());
148
149 GetMethod method = new GetMethod(url);
150 HttpClient client = new HttpClient();
151
152
153 int responseCode = client.executeMethod(method);
154 assertEquals(HttpConstants.SC_OK, responseCode);
155
156 assertEquals(method.getResponseBodyAsString(), "/echo");
157
158 url = String.format("https://localhost:%2d/echo", port2.getNumber());
159 method = new GetMethod(url);
160 responseCode = client.executeMethod(method);
161 assertEquals(HttpConstants.SC_OK, responseCode);
162
163 assertEquals(method.getResponseBodyAsString(), "/echo");
164 }
165
166 }