View Javadoc

1   /*
2    * $Id: HttpResponseTestCase.java 22431 2011-07-18 07:40:35Z dirk.olmes $
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.test.usecases.http;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.module.client.MuleClient;
16  import org.mule.tck.AbstractServiceAndFlowTestCase;
17  import org.mule.transport.NullPayload;
18  import org.mule.transport.http.HttpConnector;
19  import org.mule.transport.http.HttpConstants;
20  
21  import java.util.Arrays;
22  import java.util.Collection;
23  
24  import org.junit.Test;
25  import org.junit.runners.Parameterized.Parameters;
26  
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertFalse;
29  import static org.junit.Assert.assertNotNull;
30  import static org.junit.Assert.assertNotSame;
31  import static org.junit.Assert.assertNull;
32  
33  public class HttpResponseTestCase extends AbstractServiceAndFlowTestCase
34  {
35      @Parameters
36      public static Collection<Object[]> parameters()
37      {
38          return Arrays.asList(new Object[][]{
39              {ConfigVariant.SERVICE, "org/mule/test/usecases/http/http-response-service.xml"},
40              {ConfigVariant.FLOW, "org/mule/test/usecases/http/http-response-flow.xml"}
41          });
42      }
43  
44      public HttpResponseTestCase(ConfigVariant variant, String configResources)
45      {
46          super(variant, configResources);
47      }
48  
49      @Test
50      public void testNullPayloadUsingAsync() throws Exception
51      {
52          MuleClient client = new MuleClient(muleContext);
53          MuleMessage reply = client.send("http://localhost:8990", new DefaultMuleMessage("test", muleContext));
54  
55          //TODO RM: What should really be returned when doing an async request?
56          assertNotNull(reply.getPayload());
57          int status = reply.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0);
58          assertEquals(status, 200);
59          assertEquals(0, reply.getPayloadAsString().length());
60      }
61  
62      @Test
63      public void testPayloadIsNotEmptyNoRemoteSynch() throws Exception
64      {
65          MuleClient client = new MuleClient(muleContext);
66          MuleMessage reply = client.send("http://localhost:8999", new DefaultMuleMessage("test", muleContext));
67          assertNotNull(reply.getPayload());
68          assertFalse(reply.getPayload() instanceof NullPayload);
69          assertEquals("test", reply.getPayloadAsString());
70      }
71  
72      @Test
73      public void testPayloadIsNotEmptyWithRemoteSynch() throws Exception
74      {
75          MuleClient client = new MuleClient(muleContext);
76          MuleMessage reply = client.send("http://localhost:8989", new DefaultMuleMessage("test", muleContext));
77          assertNotNull(reply.getPayload());
78          assertFalse(reply.getPayload() instanceof NullPayload);
79          assertEquals("test", reply.getPayloadAsString());
80      }
81  
82      /**
83       * See MULE-4522
84       * @throws Exception
85       */
86      @Test
87      public void testChunkingContentLength() throws Exception
88      {
89          MuleClient client = new MuleClient(muleContext);
90          MuleMessage reply = client.send("http://localhost:8988", new DefaultMuleMessage("test", muleContext));
91          assertNotNull(reply.getPayload());
92          assertFalse(reply.getPayload() instanceof NullPayload);
93          assertEquals("chunked", reply.getInboundProperty(HttpConstants.HEADER_TRANSFER_ENCODING));
94          assertNull(reply.getInboundProperty(HttpConstants.HEADER_CONTENT_LENGTH));
95      }
96  
97      @Test
98      public void testNoChunkingContentLength() throws Exception
99      {
100         MuleClient client = new MuleClient(muleContext);
101         MuleMessage reply = client.send("http://localhost:8987", new DefaultMuleMessage("test", muleContext));
102         assertNotNull(reply.getPayload());
103         assertFalse(reply.getPayload() instanceof NullPayload);
104         assertNotSame("chunked", reply.getInboundProperty(HttpConstants.HEADER_TRANSFER_ENCODING));
105         assertNotNull(reply.getInboundProperty(HttpConstants.HEADER_CONTENT_LENGTH));
106     }
107 
108 }