View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.rest.mock2;
18  
19  import static org.apache.juneau.TestUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import org.apache.juneau.*;
23  import org.apache.juneau.rest.mock.*;
24  import org.junit.jupiter.api.*;
25  
26  class MockServletRequest_Test extends TestBase {
27  
28  	//-----------------------------------------------------------------------------------------------------------------
29  	// URIs
30  	//-----------------------------------------------------------------------------------------------------------------
31  
32  	@Test void a01_uris_basic() {
33  		var req = MockServletRequest.create("GET", "/foo");
34  		assertBean(
35  			req,
36  			"contextPath,pathInfo,pathTranslated,queryString,requestURI,requestURL,servletPath",
37  			",/foo,/mock-path/foo,<null>,/foo,/foo,"
38  		);
39  	}
40  
41  	@Test void a02_uris_full() {
42  		var req = MockServletRequest.create("GET", "http://localhost:8080/foo?bar=baz#quz");
43  		assertBean(
44  			req,
45  			"contextPath,pathInfo,pathTranslated,queryString,requestURI,requestURL,servletPath",
46  			",/foo,/mock-path/foo,bar=baz,/foo,http://localhost:8080/foo,"
47  		);
48  	}
49  
50  	@Test void a03_uris_full2() {
51  		var req = MockServletRequest.create("GET", "http://localhost:8080/foo/bar/baz?bar=baz#quz");
52  		assertBean(
53  			req,
54  			"contextPath,pathInfo,pathTranslated,queryString,requestURI,requestURL,servletPath",
55  			",/foo/bar/baz,/mock-path/foo/bar/baz,bar=baz,/foo/bar/baz,http://localhost:8080/foo/bar/baz,"
56  		);
57  	}
58  
59  	@Test void a04_uris_contextPath() {
60  		var req = MockServletRequest.create("GET", "http://localhost:8080/foo/bar/baz?bar=baz#quz").contextPath("/foo");
61  		assertBean(
62  			req,
63  			"contextPath,pathInfo,pathTranslated,queryString,requestURI,requestURL,servletPath",
64  			"/foo,/bar/baz,/mock-path/bar/baz,bar=baz,/foo/bar/baz,http://localhost:8080/foo/bar/baz,"
65  		);
66  	}
67  
68  	@Test void a05_uris_servletPath() {
69  		assertBean(
70  			MockServletRequest.create("GET", "http://localhost:8080/foo/bar/baz?bar=baz#quz").servletPath("/foo"),
71  			"contextPath,pathInfo,pathTranslated,queryString,requestURI,requestURL,servletPath",
72  			",/bar/baz,/mock-path/bar/baz,bar=baz,/foo/bar/baz,http://localhost:8080/foo/bar/baz,/foo"
73  		);
74  	}
75  
76  	//-----------------------------------------------------------------------------------------------------------------
77  	// Query strings
78  	//-----------------------------------------------------------------------------------------------------------------
79  
80  	@Test void b01_query_basic() {
81  		var req = MockServletRequest.create("GET", "/foo?bar=baz&bing=qux");
82  		assertBean(
83  			req,
84  			"queryString,parameterMap,parameterNames",
85  			"bar=baz&bing=qux,{bar=[baz],bing=[qux]},[bar,bing]"
86  		);
87  		assertEquals("baz", req.getParameter("bar"));
88  		assertList(req.getParameterValues("bar"), "baz");
89  	}
90  
91  	@Test void b02_query_multivalues() {
92  		var req = MockServletRequest.create("GET", "/foo?bar=baz&bar=bing");
93  		assertBean(
94  			req,
95  			"queryString,parameterMap,parameterNames",
96  			"bar=baz&bar=bing,{bar=[baz,bing]},[bar]"
97  		);
98  		assertEquals("baz", req.getParameter("bar"));
99  		assertList(req.getParameterValues("bar"), "baz", "bing");
100 	}
101 }