View Javadoc

1   /*
2    * $Id: MockHttpServletRequestBuilder.java 20875 2011-01-04 16:09:25Z aperepel $
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.servlet;
12  
13  import org.mule.transport.http.HttpConstants;
14  
15  import java.util.Enumeration;
16  import java.util.HashMap;
17  import java.util.Hashtable;
18  import java.util.Map;
19  import java.util.Set;
20  
21  import javax.servlet.ServletInputStream;
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpSession;
24  
25  import org.apache.commons.collections.iterators.IteratorEnumeration;
26  
27  import static org.mockito.Matchers.anyBoolean;
28  import static org.mockito.Matchers.eq;
29  import static org.mockito.Mockito.mock;
30  import static org.mockito.Mockito.when;
31  
32  public class MockHttpServletRequestBuilder
33  {
34      public static final String REQUEST_URI = "/services/Echo";
35  
36      public String method = HttpConstants.METHOD_GET;
37      public String requestUri = REQUEST_URI;
38      public ServletInputStream inputStream = null;
39      public String payload = null;
40      public String queryString = null;
41      public Map<String, String[]> parameters = null;
42      public String contentType = null;
43      public HttpSession session = null;
44      public String characterEncoding = null;
45      public Map<String, String> attributes = new HashMap<String, String>();
46      public Map<String, Object> headers = new HashMap<String, Object>();
47      public String host = "localhost";
48      public int localPort = 8080;
49  
50      public HttpServletRequest buildRequest() throws Exception
51      {
52          HttpServletRequest mockRequest = mock(HttpServletRequest.class);
53          when(mockRequest.getMethod()).thenReturn(method);
54  
55          Enumeration<?> emptyEnumeration = new Hashtable<Object, Object>().elements();
56          when(mockRequest.getParameterNames()).thenReturn(emptyEnumeration);
57  
58          when(mockRequest.getRequestURI()).thenReturn(requestUri);
59          when(mockRequest.getQueryString()).thenReturn(queryString);
60          when(mockRequest.getInputStream()).thenReturn(inputStream);
61          when(mockRequest.getSession(anyBoolean())).thenReturn(session);
62          when(mockRequest.getCharacterEncoding()).thenReturn(characterEncoding);
63          when(mockRequest.getLocalPort()).thenReturn(localPort);
64          when(mockRequest.getContentType()).thenReturn(contentType);
65          when(mockRequest.getRemoteAddr()).thenReturn(host);
66          when(mockRequest.getHeader(eq(HttpConstants.HEADER_HOST))).thenReturn(host);
67  
68          addParameterExpectations(mockRequest);
69          addAttributeExpectations(mockRequest);
70          addHeaderExpectations(mockRequest);
71  
72          return mockRequest;
73      }
74  
75      private void addParameterExpectations(HttpServletRequest mockRequest)
76      {
77          Enumeration<?> nameEnum = null;
78  
79          if (parameters != null)
80          {
81              Set<String> keys = parameters.keySet();
82              nameEnum = new IteratorEnumeration(keys.iterator());
83  
84              for (Map.Entry<String, String[]> entry : parameters.entrySet())
85              {
86                  String key = entry.getKey();
87                  String[] value = entry.getValue();
88                  when(mockRequest.getParameterValues(eq(key))).thenReturn(value);
89              }
90          }
91  
92          when(mockRequest.getParameterNames()).thenReturn(nameEnum);
93          when(mockRequest.getParameterMap()).thenReturn(parameters);
94      }
95  
96      private void addAttributeExpectations(HttpServletRequest mockRequest)
97      {
98          Enumeration<?> nameEnum = null;
99  
100         if (attributes != null)
101         {
102             nameEnum = keyEnumeration(attributes);
103 
104             for (Map.Entry<String, String> entry : attributes.entrySet())
105             {
106                 String key = entry.getKey();
107                 String value = entry.getValue();
108 
109                 when(mockRequest.getAttribute(eq(key))).thenReturn(value);
110             }
111         }
112 
113         when(mockRequest.getAttributeNames()).thenReturn(nameEnum);
114     }
115 
116     private void addHeaderExpectations(HttpServletRequest mockRequest)
117     {
118         Enumeration<?> nameEnum = null;
119         if (headers != null)
120         {
121             nameEnum = keyEnumeration(headers);
122 
123             for (Map.Entry<String, Object> entry : headers.entrySet())
124             {
125                 String key = entry.getKey();
126                 Object value = entry.getValue();
127 
128                 Enumeration<?> valueAsEnumeration = null;
129                 if ((value instanceof Enumeration<?>) == false)
130                 {
131                     valueAsEnumeration = new SingleElementEnumeration(value);
132                 }
133                 else
134                 {
135                     valueAsEnumeration = (Enumeration<?>) value;
136                 }
137 
138                 when(mockRequest.getHeaders(eq(key))).thenReturn(valueAsEnumeration);
139             }
140         }
141 
142         when(mockRequest.getHeaderNames()).thenReturn(nameEnum);
143     }
144 
145     private Enumeration<?> keyEnumeration(Map<?, ?> map)
146     {
147         Set<?> keys = map.keySet();
148         return new IteratorEnumeration(keys.iterator());
149     }
150 
151     private static class SingleElementEnumeration implements Enumeration<Object>
152     {
153         private Object element;
154 
155         public SingleElementEnumeration(Object singleElement)
156         {
157             super();
158             element = singleElement;
159         }
160 
161         public boolean hasMoreElements()
162         {
163             return (element != null);
164         }
165 
166         public Object nextElement()
167         {
168             Object retValue = element;
169             if (element != null)
170             {
171                 element = null;
172             }
173             return retValue;
174         }
175     }
176 }