View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.test.usecases.http;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleMessage;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.junit4.FunctionalTestCase;
13  import org.mule.transport.NullPayload;
14  import org.mule.transport.http.HttpConnector;
15  import org.mule.transport.http.HttpConstants;
16  
17  import org.junit.Test;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertNotSame;
23  import static org.junit.Assert.assertNull;
24  
25  public class HttpResponseTestCase extends FunctionalTestCase
26  {
27  
28      @Override
29      protected String getConfigResources()
30      {
31          return "org/mule/test/usecases/http/http-response.xml";
32      }
33  
34      @Test
35      public void testNullPayloadUsingAsync() throws Exception
36      {
37          MuleClient client = new MuleClient(muleContext);
38          MuleMessage reply = client.send("http://localhost:8990", new DefaultMuleMessage("test", muleContext));
39  
40          //TODO RM: What should really be returned when doing an async request?
41          assertNotNull(reply.getPayload());
42          int status = reply.getInboundProperty(HttpConnector.HTTP_STATUS_PROPERTY, 0);
43          assertEquals(status, 200);
44          assertEquals(0, reply.getPayloadAsString().length());
45      }
46  
47      @Test
48      public void testPayloadIsNotEmptyNoRemoteSynch() throws Exception
49      {
50          MuleClient client = new MuleClient(muleContext);
51          MuleMessage reply = client.send("http://localhost:8999", new DefaultMuleMessage("test", muleContext));
52          assertNotNull(reply.getPayload());
53          assertFalse(reply.getPayload() instanceof NullPayload);
54          assertEquals("test", reply.getPayloadAsString());
55      }
56  
57      @Test
58      public void testPayloadIsNotEmptyWithRemoteSynch() throws Exception
59      {
60          MuleClient client = new MuleClient(muleContext);
61          MuleMessage reply = client.send("http://localhost:8989", new DefaultMuleMessage("test", muleContext));
62          assertNotNull(reply.getPayload());
63          assertFalse(reply.getPayload() instanceof NullPayload);
64          assertEquals("test", reply.getPayloadAsString());
65      }
66      
67      /**
68       * See MULE-4522
69       * @throws Exception
70       */
71      @Test
72      public void testChunkingContentLength() throws Exception
73      {
74          MuleClient client = new MuleClient(muleContext);
75          MuleMessage reply = client.send("http://localhost:8988", new DefaultMuleMessage("test", muleContext));
76          assertNotNull(reply.getPayload());
77          assertFalse(reply.getPayload() instanceof NullPayload);
78          assertEquals("chunked", reply.getInboundProperty(HttpConstants.HEADER_TRANSFER_ENCODING));
79          assertNull(reply.getInboundProperty(HttpConstants.HEADER_CONTENT_LENGTH));
80      }
81  
82      @Test
83      public void testNoChunkingContentLength() throws Exception
84      {
85          MuleClient client = new MuleClient(muleContext);
86          MuleMessage reply = client.send("http://localhost:8987", new DefaultMuleMessage("test", muleContext));
87          assertNotNull(reply.getPayload());
88          assertFalse(reply.getPayload() instanceof NullPayload);
89          assertNotSame("chunked", reply.getInboundProperty(HttpConstants.HEADER_TRANSFER_ENCODING));
90          assertNotNull(reply.getInboundProperty(HttpConstants.HEADER_CONTENT_LENGTH));
91      }
92      
93  }