View Javadoc

1   /*
2    * $Id: HttpRequestWildcardFilterTestCase.java 22518 2011-07-22 07:00:22Z claude.mamo $
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.filters;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  
16  import org.mule.api.MuleMessage;
17  import org.mule.api.endpoint.InboundEndpoint;
18  import org.mule.module.client.MuleClient;
19  import org.mule.tck.AbstractServiceAndFlowTestCase;
20  import org.mule.tck.AbstractServiceAndFlowTestCase.ConfigVariant;
21  import org.mule.tck.junit4.rule.DynamicPort;
22  import org.mule.transport.http.HttpConnector;
23  import org.mule.transport.http.HttpConstants;
24  
25  import java.util.Arrays;
26  import java.util.Collection;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import org.junit.Rule;
31  import org.junit.Test;
32  import org.junit.runners.Parameterized.Parameters;
33  
34  public class HttpRequestWildcardFilterTestCase extends AbstractServiceAndFlowTestCase
35  {
36      
37      private static final String TEST_HTTP_MESSAGE = "Hello=World";
38      private static final String TEST_BAD_MESSAGE = "xyz";
39  
40      @Rule
41      public DynamicPort dynamicPort1 = new DynamicPort("port1");
42  
43      @Rule
44      public DynamicPort dynamicPort2 = new DynamicPort("port2");
45  
46      public HttpRequestWildcardFilterTestCase(ConfigVariant variant, String configResources)
47      {
48          super(variant, configResources);
49      }
50  
51      @Parameters
52      public static Collection<Object[]> parameters()
53      {
54          return Arrays.asList(new Object[][]{
55              {ConfigVariant.SERVICE, "http-wildcard-filter-test-flow.xml"},
56              {ConfigVariant.FLOW, "http-wildcard-filter-test-service.xml"}
57          });
58      }      
59      
60      @Test
61      public void testReference() throws Exception
62      {
63          MuleClient client = new MuleClient(muleContext);
64          MuleMessage result = client.send(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inReference")).getAddress(), TEST_HTTP_MESSAGE, null);
65  
66          assertEquals(TEST_HTTP_MESSAGE, result.getPayloadAsString());
67      }
68  
69      @Test
70      public void testHttpPost() throws Exception
71      {
72          MuleClient client = new MuleClient(muleContext);
73          MuleMessage result = client.send(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress(), TEST_HTTP_MESSAGE, null);
74  
75          assertEquals(TEST_HTTP_MESSAGE, result.getPayloadAsString());
76      }
77  
78      @Test
79      public void testHttpGetNotFiltered() throws Exception
80      {
81          Map<String, Object> props = new HashMap<String, Object>();
82          props.put(HttpConstants.METHOD_GET, "true");
83  
84          MuleClient client = new MuleClient(muleContext);
85          MuleMessage result = client.send(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress() + "/" + "mulerulez", TEST_HTTP_MESSAGE, props);
86  
87          assertEquals(TEST_HTTP_MESSAGE, result.getPayloadAsString());
88      }
89  
90      @Test
91      public void testHttpGetFiltered() throws Exception
92      {
93          Map<String, Object> props = new HashMap<String, Object>();
94          props.put(HttpConnector.HTTP_METHOD_PROPERTY, HttpConstants.METHOD_GET);
95          //props.put(HttpConstants.METHOD_GET, "true");
96  
97          MuleClient client = new MuleClient(muleContext);
98          MuleMessage result = client.send(((InboundEndpoint) client.getMuleContext().getRegistry().lookupObject("inHttpIn")).getAddress() + "/" + TEST_BAD_MESSAGE, "mule", props);
99  
100         final int status = result.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0);
101         assertEquals(HttpConstants.SC_NOT_ACCEPTABLE, status);
102         assertNotNull(result.getExceptionPayload());
103     }
104 
105 }