View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  
16  package org.esigate.servlet;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpSession;
25  
26  import org.apache.http.Header;
27  import org.apache.http.message.BasicHeader;
28  import org.mockito.Mockito;
29  
30  /**
31   * Fluent-style builder for mocked HttpServletRequest.
32   * 
33   * <p>
34   * Default request is
35   * 
36   * <pre>
37   * GET http://localhost/ HTTP/1.1
38   * </pre>
39   * 
40   * @author Nicolas Richeton
41   * 
42   */
43  public class MockHttpServletRequestBuilder {
44      private String protocolVersion = "HTTP/1.1";
45      private String uriString = "http://localhost/";
46      private List<Header> headers = new ArrayList<>();
47      private String method = "GET";
48      private Map<String, Object> session = null;
49  
50      public MockHttpServletRequestBuilder uri(String uri) {
51          this.uriString = uri;
52          return this;
53      }
54  
55      /**
56       * Duplicate headers are not supported currently.
57       * 
58       * @param name
59       * @param value
60       * @return this
61       */
62      public MockHttpServletRequestBuilder header(String name, String value) {
63          this.headers.add(new BasicHeader(name, value));
64          return this;
65      }
66  
67      public MockHttpServletRequestBuilder method(String paramMethod) {
68          this.method = paramMethod;
69          return this;
70      }
71  
72      public MockHttpServletRequestBuilder protocolVersion(String paramProtocolVersion) {
73          this.protocolVersion = paramProtocolVersion;
74          return this;
75      }
76  
77      /**
78       * Session values are not yet supported. Null or existing object are honored.
79       * 
80       * @param paramSession
81       * @return this
82       */
83      public MockHttpServletRequestBuilder session(Map<String, Object> paramSession) {
84          this.session = paramSession;
85          return this;
86      }
87  
88      /**
89       * Build the request as defined in the current builder.
90       * 
91       * @return the request
92       */
93      public HttpServletRequest build() {
94          HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
95  
96          Mockito.when(request.getMethod()).thenReturn(this.method);
97          Mockito.when(request.getProtocol()).thenReturn(this.protocolVersion);
98          Mockito.when(request.getRequestURI()).thenReturn(this.uriString);
99  
100         Mockito.when(request.getHeaderNames()).thenReturn(Collections.enumeration(headers));
101         for (Header h : headers) {
102             List<String> hresult = new ArrayList<>();
103             hresult.add(h.getValue());
104             Mockito.when(request.getHeaders(h.getName())).thenReturn(Collections.enumeration(hresult));
105             Mockito.when(request.getHeader(h.getName())).thenReturn(h.getValue());
106         }
107 
108         if (session == null) {
109             Mockito.when(request.getSession()).thenReturn(null);
110         } else {
111             HttpSession sessionMock = Mockito.mock(HttpSession.class);
112             Mockito.when(request.getSession()).thenReturn(sessionMock);
113         }
114         return request;
115     }
116 }