ServletServerHttpRequestTests.java 5.0 KB
Newer Older
1
/*
2
 * Copyright 2002-2016 the original author or authors.
3 4 5 6 7
 *
 * 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
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15 16
 *
 * 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.
 */

17
package org.springframework.http.server;
18

19
import java.net.URI;
20
import java.nio.charset.Charset;
21 22 23 24
import java.util.List;

import org.junit.Before;
import org.junit.Test;
25

26 27
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
28
import org.springframework.http.MediaType;
R
Rob Winch 已提交
29
import org.springframework.mock.web.test.MockHttpServletRequest;
30
import org.springframework.util.FileCopyUtils;
31

32 33
import static org.junit.Assert.*;

34 35 36
/**
 * @author Arjen Poutsma
 */
37
public class ServletServerHttpRequestTests {
38 39 40 41 42

	private ServletServerHttpRequest request;

	private MockHttpServletRequest mockRequest;

43

44 45 46 47 48 49
	@Before
	public void create() throws Exception {
		mockRequest = new MockHttpServletRequest();
		request = new ServletServerHttpRequest(mockRequest);
	}

50

51 52 53 54 55 56
	@Test
	public void getMethod() throws Exception {
		mockRequest.setMethod("POST");
		assertEquals("Invalid method", HttpMethod.POST, request.getMethod());
	}

57 58
	@Test
	public void getURI() throws Exception {
59 60 61 62 63 64 65 66 67 68 69 70 71 72
		URI uri = new URI("http://example.com/path?query");
		mockRequest.setServerName(uri.getHost());
		mockRequest.setServerPort(uri.getPort());
		mockRequest.setRequestURI(uri.getPath());
		mockRequest.setQueryString(uri.getQuery());
		assertEquals("Invalid uri", uri, request.getURI());
	}

	// SPR-13876

	@Test
	public void getUriWithEncoding() throws Exception {
        URI uri = new URI("https://example.com/%E4%B8%AD%E6%96%87" +
				"?redirect=https%3A%2F%2Fgithub.com%2Fspring-projects%2Fspring-framework");
73 74 75 76 77 78 79
        mockRequest.setScheme(uri.getScheme());
        mockRequest.setServerName(uri.getHost());
        mockRequest.setServerPort(uri.getPort());
        mockRequest.setRequestURI(uri.getRawPath());
        mockRequest.setQueryString(uri.getRawQuery());
        assertEquals("Invalid uri", uri, request.getURI());
    }
80

81 82 83 84 85
	@Test
	public void getHeaders() throws Exception {
		String headerName = "MyHeader";
		String headerValue1 = "value1";
		String headerValue2 = "value2";
86
		mockRequest.addHeader(headerName, headerValue1);
87
		mockRequest.addHeader(headerName, headerValue2);
88 89
		mockRequest.setContentType("text/plain");
		mockRequest.setCharacterEncoding("UTF-8");
90 91 92 93 94 95 96 97

		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));
98 99
		assertEquals("Invalid Content-Type", new MediaType("text", "plain", Charset.forName("UTF-8")),
				headers.getContentType());
100 101
	}

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
	@Test
	public void getHeadersWithEmptyContentTypeAndEncoding() throws Exception {
		String headerName = "MyHeader";
		String headerValue1 = "value1";
		String headerValue2 = "value2";
		mockRequest.addHeader(headerName, headerValue1);
		mockRequest.addHeader(headerName, headerValue2);
		mockRequest.setContentType("");
		mockRequest.setCharacterEncoding("");

		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));
		assertNull(headers.getContentType());
	}

122 123 124 125 126 127 128 129
	@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);
	}
130 131 132

	@Test
	public void getFormBody() throws Exception {
133 134
		// Charset (SPR-8676)
		mockRequest.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
135 136 137 138 139 140 141 142 143
		mockRequest.setMethod("POST");
		mockRequest.addParameter("name 1", "value 1");
		mockRequest.addParameter("name 2", new String[] {"value 2+1", "value 2+2"});
		mockRequest.addParameter("name 3", (String) null);

		byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
		byte[] content = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes("UTF-8");
		assertArrayEquals("Invalid content returned", content, result);
	}
144

145
}