View Javadoc
1   /*
2    * Copyright 2002-2015 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.web.socket;
18  
19  import org.junit.Before;
20  
21  import org.springframework.http.HttpHeaders;
22  import org.springframework.http.server.ServerHttpAsyncRequestControl;
23  import org.springframework.http.server.ServerHttpRequest;
24  import org.springframework.http.server.ServerHttpResponse;
25  import org.springframework.http.server.ServletServerHttpRequest;
26  import org.springframework.http.server.ServletServerHttpResponse;
27  import org.springframework.mock.web.test.MockHttpServletRequest;
28  import org.springframework.mock.web.test.MockHttpServletResponse;
29  
30  /**
31   * Base class for tests using {@link ServerHttpRequest} and {@link ServerHttpResponse}.
32   *
33   * @author Rossen Stoyanchev
34   */
35  public abstract class AbstractHttpRequestTests {
36  
37  	protected ServerHttpRequest request;
38  
39  	protected ServerHttpResponse response;
40  
41  	protected MockHttpServletRequest servletRequest;
42  
43  	protected MockHttpServletResponse servletResponse;
44  
45  	protected ServerHttpAsyncRequestControl asyncControl;
46  
47  
48  	@Before
49  	public void setUp() {
50  		resetRequestAndResponse();
51  	}
52  
53  	protected void setRequest(String method, String requestUri) {
54  		this.servletRequest.setMethod(method);
55  		this.servletRequest.setRequestURI(requestUri);
56  	}
57  
58  	protected void setOrigin(String origin) {
59  		this.request.getHeaders().add(HttpHeaders.ORIGIN, origin);
60  	}
61  
62  	protected void resetRequestAndResponse() {
63  		resetRequest();
64  		resetResponse();
65  		this.asyncControl = this.request.getAsyncRequestControl(this.response);
66  	}
67  
68  	protected void resetRequest() {
69  		this.servletRequest = new MockHttpServletRequest();
70  		this.servletRequest.setAsyncSupported(true);
71  		this.request = new ServletServerHttpRequest(this.servletRequest);
72  	}
73  
74  	protected void resetResponse() {
75  		this.servletResponse = new MockHttpServletResponse();
76  		this.response = new ServletServerHttpResponse(this.servletResponse);
77  	}
78  
79  }