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.transport.servlet;
8   
9   import org.mule.DefaultMuleMessage;
10  import org.mule.api.MuleContext;
11  import org.mule.tck.junit4.AbstractMuleTestCase;
12  import org.mule.transport.http.HttpConstants;
13  import org.mule.transport.http.HttpResponse;
14  
15  import javax.servlet.http.HttpServletResponse;
16  
17  import org.apache.commons.httpclient.Header;
18  import org.junit.Test;
19  
20  import static org.mockito.Mockito.mock;
21  import static org.mockito.Mockito.verify;
22  
23  public class MuleReceiverServletTestCase extends AbstractMuleTestCase
24  {
25      private static final String KEY = "key";
26      private MuleContext mockContext = mock(MuleContext.class);
27  
28      @Test
29      public void responseWithSingleValueForHeaderShouldWriteSingleValueToServletResponse() throws Exception
30      {
31          String headerValue = "value";
32  
33          HttpResponse httpResponse = new HttpResponse();
34          httpResponse.addHeader(new Header(KEY, headerValue));
35  
36          HttpServletResponse servletResponse = createServletResponseAndWriteResponse(httpResponse);
37          verify(servletResponse).addHeader(KEY, headerValue);
38      }
39  
40      @Test
41      public void responseWithMultipleValuesForHeaderShouldWriteMultipleValuesToServletResponse() throws Exception
42      {
43          String firstValue = "value1";
44          String secondValue = "value2";
45  
46          HttpResponse httpResponse = new HttpResponse();
47          httpResponse.addHeader(new Header(KEY, firstValue));
48          httpResponse.addHeader(new Header(KEY, secondValue));
49  
50          HttpServletResponse servletResponse = createServletResponseAndWriteResponse(httpResponse);
51          verify(servletResponse).addHeader(KEY, firstValue);
52          verify(servletResponse).addHeader(KEY, secondValue);
53      }
54  
55      @Test
56      public void responseWithoutContentTypeHeaderShouldGetDefaultContentType() throws Exception
57      {
58          HttpResponse httpResponse = new HttpResponse();
59          httpResponse.removeHeaders(HttpConstants.HEADER_CONTENT_TYPE);
60  
61          HttpServletResponse servletResponse = createServletResponseAndWriteResponse(httpResponse);
62          verify(servletResponse).setContentType(HttpConstants.DEFAULT_CONTENT_TYPE);
63      }
64  
65      @Test
66      public void responseWithExistingContentTypeHeaderShouldPreserve() throws Exception
67      {
68          String contentType = "foo/bar";
69  
70          HttpResponse httpResponse = new HttpResponse();
71          httpResponse.addHeader(new Header(HttpConstants.HEADER_CONTENT_TYPE, contentType));
72  
73          HttpServletResponse servletResponse = createServletResponseAndWriteResponse(httpResponse);
74          verify(servletResponse).setContentType(contentType);
75      }
76  
77      private HttpServletResponse createServletResponseAndWriteResponse(HttpResponse httpResponse) throws Exception
78      {
79          HttpServletResponse servletResponse = mock(HttpServletResponse.class);
80  
81          TestReceiverServlet testServlet = new TestReceiverServlet();
82          testServlet.writeResponse(servletResponse, new DefaultMuleMessage(httpResponse, mockContext));
83  
84          return servletResponse;
85      }
86  
87      private static class TestReceiverServlet extends AbstractReceiverServlet
88      {
89          // no custom methods
90      }
91  }