This project has retired. For details please refer to its Attic page.
ProxyRequestTest xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.impl;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  import static org.mockito.Mockito.when;
24  
25  import java.net.URI;
26  import java.net.URISyntaxException;
27  
28  import javax.servlet.http.HttpServletRequest;
29  
30  import org.apache.chemistry.opencmis.server.filter.ProxyHttpServletRequestWrapper;
31  import org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.mockito.Mock;
35  import org.mockito.MockitoAnnotations;
36  
37  public class ProxyRequestTest {
38      private static final int FORWARDED_SERVER_PORT = 2443;
39      private static final String FORWARDED_SERVER_NAME = "www.frontend.org";
40      private static final String FORWARDED_HOST = FORWARDED_SERVER_NAME + ":" + FORWARDED_SERVER_PORT;
41      private static final String FORWARDED_HTTPS_PROTO = ProxyHttpServletRequestWrapper.HTTPS_SCHEME;
42      private static final String FORWARDED_HTTP_PROTO = ProxyHttpServletRequestWrapper.HTTP_SCHEME;
43      private static final String CONTEXT_PATH = "/context";
44      private static final String SERVLET_PATH = "cmisatom";
45      private static final String REPOSITORY_ID = "22d2880a- bae5-4cfc-a5a9-3b2618e6e11c";
46      private static final String EXPECTED_PATH = CONTEXT_PATH + "/" + SERVLET_PATH + "/" + REPOSITORY_ID;
47      private static final int BACKEND_SERVER_PORT = 8080;
48      private static final String BACKEND_SERVER_NAME = "www.backend.be";
49      private static final String BACKEND_SERVER_PROTO = ProxyHttpServletRequestWrapper.HTTP_SCHEME;
50  
51      @Mock
52      private HttpServletRequest request;
53  
54      @Before
55      public void setUp() throws Exception {
56          MockitoAnnotations.initMocks(this);
57          when(this.request.getScheme()).thenReturn(BACKEND_SERVER_PROTO);
58          when(this.request.getServerName()).thenReturn(BACKEND_SERVER_NAME);
59          when(this.request.getServerPort()).thenReturn(BACKEND_SERVER_PORT);
60          when(this.request.getContextPath()).thenReturn(CONTEXT_PATH);
61          when(this.request.getServletPath()).thenReturn(SERVLET_PATH);
62      }
63  
64      @Test
65      public void testGetProxiedProtoBaseAddress() throws URISyntaxException {
66          when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_PROTO_HEADER)).thenReturn(
67                  FORWARDED_HTTPS_PROTO);
68  
69          ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
70  
71          assertEquals(FORWARDED_HTTPS_PROTO, proxyRequest.getScheme());
72  
73          URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
74  
75          assertEquals(FORWARDED_HTTPS_PROTO, baseUri.getScheme());
76          assertEquals(BACKEND_SERVER_NAME, baseUri.getHost());
77          assertEquals(BACKEND_SERVER_PORT, baseUri.getPort());
78          assertEquals(EXPECTED_PATH, baseUri.getPath());
79      }
80  
81      @Test
82      public void testGetProxiedHostBaseAddressAndHttpsProto() throws URISyntaxException {
83          when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_PROTO_HEADER)).thenReturn(
84                  FORWARDED_HTTPS_PROTO);
85          when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(
86                  FORWARDED_SERVER_NAME);
87  
88          ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
89  
90          assertEquals(FORWARDED_HTTPS_PROTO, proxyRequest.getScheme());
91          assertEquals(FORWARDED_SERVER_NAME, proxyRequest.getServerName());
92  
93          URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
94  
95          assertEquals(FORWARDED_HTTPS_PROTO, baseUri.getScheme());
96          assertEquals(FORWARDED_SERVER_NAME, baseUri.getHost());
97          assertEquals(-1, baseUri.getPort());
98          assertEquals(EXPECTED_PATH, baseUri.getPath());
99      }
100 
101     @Test
102     public void testGetProxiedHostBaseAddress() throws URISyntaxException {
103         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(
104                 FORWARDED_SERVER_NAME);
105 
106         ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
107 
108         assertEquals(FORWARDED_SERVER_NAME, proxyRequest.getServerName());
109 
110         URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
111 
112         assertEquals(BACKEND_SERVER_PROTO, baseUri.getScheme());
113         assertEquals(FORWARDED_SERVER_NAME, baseUri.getHost());
114         assertEquals(-1, baseUri.getPort());
115         assertEquals(EXPECTED_PATH, baseUri.getPath());
116     }
117 
118     @Test
119     public void testGetProxiedHostAndPortBaseAddress() throws URISyntaxException {
120         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(FORWARDED_HOST);
121 
122         ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
123 
124         assertTrue(FORWARDED_HOST.startsWith(proxyRequest.getServerName()));
125 
126         URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
127 
128         assertEquals(BACKEND_SERVER_PROTO, baseUri.getScheme());
129         assertEquals(FORWARDED_SERVER_NAME, baseUri.getHost());
130         assertEquals(FORWARDED_SERVER_PORT, baseUri.getPort());
131         assertEquals(EXPECTED_PATH, baseUri.getPath());
132     }
133 
134     @Test
135     public void testGetProxiedHostPortAndProtoBaseAddress() throws URISyntaxException {
136         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_PROTO_HEADER)).thenReturn(
137                 FORWARDED_HTTPS_PROTO);
138         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(FORWARDED_HOST);
139 
140         ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
141 
142         assertEquals(FORWARDED_HTTPS_PROTO, proxyRequest.getScheme());
143         assertTrue(FORWARDED_HOST.startsWith(proxyRequest.getServerName()));
144 
145         URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
146 
147         assertEquals(FORWARDED_HTTPS_PROTO, baseUri.getScheme());
148         assertEquals(FORWARDED_SERVER_NAME, baseUri.getHost());
149         assertEquals(FORWARDED_SERVER_PORT, baseUri.getPort());
150         assertEquals(EXPECTED_PATH, baseUri.getPath());
151     }
152 
153     @Test
154     public void testGetProxiedHostBadHttpPortAndProtoBaseAddress() throws URISyntaxException {
155         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_PROTO_HEADER)).thenReturn(
156                 FORWARDED_HTTP_PROTO);
157         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(
158                 FORWARDED_SERVER_NAME + ":noportnumber");
159 
160         ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
161 
162         assertEquals(FORWARDED_HTTP_PROTO, proxyRequest.getScheme());
163         assertTrue(FORWARDED_HOST.startsWith(proxyRequest.getServerName()));
164 
165         URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
166 
167         assertEquals(FORWARDED_HTTP_PROTO, baseUri.getScheme());
168         assertEquals(FORWARDED_SERVER_NAME, baseUri.getHost());
169         assertEquals(-1, baseUri.getPort());
170         assertEquals(EXPECTED_PATH, baseUri.getPath());
171     }
172 
173     @Test
174     public void testGetProxiedHostBadHttpsPortAndProtoBaseAddress() throws URISyntaxException {
175         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_PROTO_HEADER)).thenReturn(
176                 FORWARDED_HTTPS_PROTO);
177         when(this.request.getHeader(ProxyHttpServletRequestWrapper.FORWARDED_HOST_HEADER)).thenReturn(
178                 FORWARDED_SERVER_NAME + ":noportnumber");
179 
180         ProxyHttpServletRequestWrapper proxyRequest = new ProxyHttpServletRequestWrapper(request);
181 
182         assertEquals(FORWARDED_HTTPS_PROTO, proxyRequest.getScheme());
183         assertTrue(FORWARDED_HOST.startsWith(proxyRequest.getServerName()));
184 
185         URI baseUri = new URI(AtomPubUtils.compileBaseUrl(proxyRequest, REPOSITORY_ID).toString());
186 
187         assertEquals(FORWARDED_HTTPS_PROTO, baseUri.getScheme());
188         assertEquals(FORWARDED_SERVER_NAME, baseUri.getHost());
189         assertEquals(-1, baseUri.getPort());
190         assertEquals(EXPECTED_PATH, baseUri.getPath());
191     }
192 
193     @Test
194     public void testCompileBaseUrl() throws URISyntaxException {
195         URI baseUri = new URI(AtomPubUtils.compileBaseUrl(request, REPOSITORY_ID).toString());
196 
197         assertEquals(BACKEND_SERVER_PROTO, baseUri.getScheme());
198         assertEquals(BACKEND_SERVER_NAME, baseUri.getHost());
199         assertEquals(BACKEND_SERVER_PORT, baseUri.getPort());
200         assertEquals(EXPECTED_PATH, baseUri.getPath());
201     }
202 }