View Javadoc

1   /*
2    * $Id: StaticResourcesMPFunctionalTestCase.java 22547 2011-07-24 03:15:16Z 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.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          // Test default resource
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          // Test explicit resource
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          // Test not found
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          // Test default resource
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          // Test built in content type
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         // Test configured content type (in META-INF/mime.types)
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         // Test default resource
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         // Test explicit resource
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         // Test not found
132         method = new GetMethod(url + "/foo.html");
133         responseCode = client.executeMethod(method);
134         assertEquals(HttpConstants.SC_NOT_FOUND, responseCode);
135 
136     }
137 
138     /**
139      * Test that endpoints bound to the same http port but different path work with
140      * the static resource MP
141      * 
142      * @throws Exception
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         // Test default resource
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 }