Added ServerHttpRequest/Response to web.http, and Servlet-based implementations.
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.http.server;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.http.HttpHeaders;
|
||||
import org.springframework.web.http.HttpMethod;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class ServletHttpRequestTests {
|
||||
|
||||
private ServletServerHttpRequest request;
|
||||
|
||||
private MockHttpServletRequest mockRequest;
|
||||
|
||||
@Before
|
||||
public void create() throws Exception {
|
||||
mockRequest = new MockHttpServletRequest();
|
||||
request = new ServletServerHttpRequest(mockRequest);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMethod() throws Exception {
|
||||
mockRequest.setMethod("POST");
|
||||
assertEquals("Invalid method", HttpMethod.POST, request.getMethod());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHeaders() throws Exception {
|
||||
String headerName = "MyHeader";
|
||||
String headerValue1 = "value1";
|
||||
mockRequest.addHeader(headerName, headerValue1);
|
||||
String headerValue2 = "value2";
|
||||
mockRequest.addHeader(headerName, headerValue2);
|
||||
|
||||
HttpHeaders headers = request.getHeaders();
|
||||
assertNotNull("No HttpHeaders returned", headers);
|
||||
assertTrue("Invalid headers returned", headers.containsKey(headerName));
|
||||
List<String> headerValues = headers.get(headerName);
|
||||
assertEquals("Invalid header values returned", 2, headerValues.size());
|
||||
assertTrue("Invalid header values returned", headerValues.contains(headerValue1));
|
||||
assertTrue("Invalid header values returned", headerValues.contains(headerValue2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBody() throws Exception {
|
||||
byte[] content = "Hello World".getBytes("UTF-8");
|
||||
mockRequest.setContent(content);
|
||||
|
||||
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
|
||||
assertArrayEquals("Invalid content returned", content, result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.http.server;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.http.HttpHeaders;
|
||||
import org.springframework.web.http.HttpStatus;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class ServletHttpResponseTest {
|
||||
|
||||
private ServletServerHttpResponse response;
|
||||
|
||||
private MockHttpServletResponse mockResponse;
|
||||
|
||||
@Before
|
||||
public void create() throws Exception {
|
||||
mockResponse = new MockHttpServletResponse();
|
||||
response = new ServletServerHttpResponse(mockResponse);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setStatusCode() throws Exception {
|
||||
response.setStatusCode(HttpStatus.NOT_FOUND);
|
||||
assertEquals("Invalid status code", 404, mockResponse.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHeaders() throws Exception {
|
||||
HttpHeaders headers = response.getHeaders();
|
||||
String headerName = "MyHeader";
|
||||
String headerValue1 = "value1";
|
||||
headers.add(headerName, headerValue1);
|
||||
String headerValue2 = "value2";
|
||||
headers.add(headerName, headerValue2);
|
||||
|
||||
response.close();
|
||||
assertTrue("Header not set", mockResponse.getHeaderNames().contains(headerName));
|
||||
List headerValues = mockResponse.getHeaders(headerName);
|
||||
assertTrue("Header not set", headerValues.contains(headerValue1));
|
||||
assertTrue("Header not set", headerValues.contains(headerValue2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBody() throws Exception {
|
||||
byte[] content = "Hello World".getBytes("UTF-8");
|
||||
FileCopyUtils.copy(content, response.getBody());
|
||||
|
||||
assertArrayEquals("Invalid content written", content, mockResponse.getContentAsByteArray());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user