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.test;
17  
18  import java.io.IOException;
19  import java.util.Properties;
20  
21  import org.apache.http.HttpHost;
22  import org.apache.http.HttpResponse;
23  import org.apache.http.HttpVersion;
24  import org.apache.http.RequestLine;
25  import org.apache.http.client.methods.CloseableHttpResponse;
26  import org.apache.http.conn.HttpClientConnectionManager;
27  import org.apache.http.message.BasicRequestLine;
28  import org.esigate.Driver;
29  import org.esigate.DriverFactory;
30  import org.esigate.HttpErrorPage;
31  import org.esigate.Renderer;
32  import org.esigate.api.ContainerRequestContext;
33  import org.esigate.http.Http;
34  import org.esigate.http.HttpClientRequestExecutor;
35  import org.esigate.http.IncomingRequest;
36  import org.esigate.impl.DriverRequest;
37  import org.esigate.test.conn.IResponseHandler;
38  import org.esigate.test.conn.MockConnectionManager;
39  import org.esigate.test.http.HttpResponseBuilder;
40  import org.esigate.util.UriUtils;
41  
42  /**
43   * Helper class for unit or integration tests.
44   * 
45   * @author Francois-Xavier Bonnet
46   * @author Nicolas Richeton
47   * 
48   */
49  public final class TestUtils {
50  
51      private TestUtils() {
52  
53      }
54  
55      /**
56       * Creates a mock {@link IncomingRequest}.
57       * 
58       * @param uri
59       *            the uri
60       * @return the {@link IncomingRequest}
61       */
62      public static IncomingRequest.Builder createIncomingRequest(String uri) {
63          HttpHost httpHost = UriUtils.extractHost(uri);
64          String scheme = httpHost.getSchemeName();
65          String host = httpHost.getHostName();
66          int port = httpHost.getPort();
67          RequestLine requestLine = new BasicRequestLine("GET", uri, HttpVersion.HTTP_1_1);
68          IncomingRequest.Builder builder = IncomingRequest.builder(requestLine);
69          builder.setContext(new ContainerRequestContext() {
70          });
71          // Remove default ports
72          if (port == -1 || (port == Http.DEFAULT_HTTP_PORT && "http".equals(scheme))
73                  || (port == Http.DEFAULT_HTTPS_PORT && "https".equals(scheme))) {
74              builder.addHeader("Host", host);
75          } else {
76              builder.addHeader("Host", host + ":" + port);
77          }
78          builder.setSession(new MockSession());
79          return builder;
80      }
81  
82      /**
83       * Creates a mock {@link IncomingRequest}.
84       * 
85       * @return the {@link IncomingRequest}
86       */
87      public static IncomingRequest.Builder createIncomingRequest() {
88          return createIncomingRequest("http://localhost:8080");
89      }
90  
91      /**
92       * Creates a mock {@link DriverRequest}.
93       * 
94       * @param uri
95       *            the uri
96       * @param driver
97       *            the {@link Driver}
98       * @return the {@link DriverRequest}
99       * @throws HttpErrorPage
100      *             if an error occurs
101      */
102     public static DriverRequest createDriverRequest(String uri, Driver driver) throws HttpErrorPage {
103         IncomingRequest request = createIncomingRequest(uri).build();
104         return new DriverRequest(request, driver, "/");
105     }
106 
107     /**
108      * Creates a mock {@link DriverRequest}.
109      * 
110      * @param driver
111      *            the {@link Driver}
112      * @return the {@link DriverRequest}
113      * @throws HttpErrorPage
114      *             if an error occurs
115      */
116     public static DriverRequest createDriverRequest(Driver driver) throws HttpErrorPage {
117         IncomingRequest request = createIncomingRequest().build();
118         return new DriverRequest(request, driver, "/");
119     }
120 
121     /**
122      * Creates a {@link Driver} instance with a custom response handler.
123      * 
124      * @param properties
125      *            the {@link Properties}
126      * @param responseHandler
127      *            the {@link IResponseHandler}
128      * @return new {@link Driver} object
129      */
130     public static Driver createMockDriver(Properties properties, IResponseHandler responseHandler) {
131         MockConnectionManager connManager = new MockConnectionManager();
132         connManager.setResponseHandler(responseHandler);
133         return createMockDriver(properties, connManager);
134     }
135 
136     /**
137      * Creates a {@link Driver} instance with a mock {@link HttpResponse}.
138      * 
139      * @param properties
140      *            the {@link Properties}
141      * @param response
142      *            the {@link HttpResponse}
143      * @return new {@link Driver} object
144      */
145     public static Driver createMockDriver(Properties properties, HttpResponse response) {
146         MockConnectionManager connManager = new MockConnectionManager();
147         connManager.setResponse(response);
148         return createMockDriver(properties, connManager);
149     }
150 
151     /**
152      * Create a Driver instance with a custom connection Manager.
153      * 
154      * @param properties
155      *            the {@link Properties}
156      * @param connectionManager
157      *            the {@link HttpClientConnectionManager}
158      * @return new {@link Driver} object
159      */
160     public static Driver createMockDriver(Properties properties, HttpClientConnectionManager connectionManager) {
161         return createMockDriver(properties, connectionManager, "tested");
162     }
163 
164     /**
165      * Create a Driver instance with a custom connection Manager.
166      * 
167      * @param properties
168      *            the {@link Properties}
169      * @param connectionManager
170      *            the {@link HttpClientConnectionManager}
171      * @param name
172      *            name of the Driver instance
173      * @return new {@link Driver} object
174      */
175     public static Driver createMockDriver(Properties properties, HttpClientConnectionManager connectionManager,
176             String name) {
177         Driver driver =
178                 Driver.builder()
179                         .setName(name)
180                         .setProperties(properties)
181                         .setRequestExecutorBuilder(
182                                 HttpClientRequestExecutor.builder().setConnectionManager(connectionManager)).build();
183         DriverFactory.put(name, driver);
184         return driver;
185     }
186 
187     /**
188      * Create a fluent-style builder for {@link HttpResponse}.
189      * 
190      * @return a {@link HttpResponseBuilder}
191      */
192     public static HttpResponseBuilder createHttpResponse() {
193         return new HttpResponseBuilder();
194     }
195 
196     /**
197      * Create a fluent-style builder for {@link IncomingRequest}.
198      * 
199      * @param uri
200      *            the request uri
201      * @return the IncomingRequest.Builder
202      */
203     public static IncomingRequest.Builder createRequest(String uri) {
204         return createIncomingRequest(uri);
205     }
206 
207     /**
208      * Execute {@link Driver#proxy(String, org.esigate.http.IncomingRequest, Renderer...)} on an HttpRequest.
209      * 
210      * 
211      * @param d
212      *            {@link Driver}
213      * @param request
214      *            Request
215      * @param renderers
216      *            {@link Renderer} which will be applied on response entity.
217      * @return the {@link HttpResponse}
218      * @throws IOException
219      *             if an {@link IOException} occurs
220      * @throws HttpErrorPage
221      *             if any other Exception
222      */
223     public static CloseableHttpResponse driverProxy(Driver d, IncomingRequest request, Renderer... renderers)
224             throws IOException, HttpErrorPage {
225         String uri = request.getRequestLine().getUri();
226         return d.proxy(UriUtils.getPath(uri), request, renderers);
227     }
228 
229 }