Polish tests in spring-web

This PR polishes trivial things in tests in spring-web.

Closes gh-27958
This commit is contained in:
shirohoo 2022-01-20 14:06:13 +09:00 committed by Sam Brannen
parent 148eac0200
commit 7211912057
2 changed files with 32 additions and 22 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 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.
@ -16,8 +16,12 @@
package org.springframework.web.util;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
@ -29,16 +33,22 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
*/
public class ContentCachingRequestWrapperTests {
protected static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded";
protected static final String FORM_CONTENT_TYPE = MediaType.APPLICATION_FORM_URLENCODED_VALUE;
protected static final String CHARSET = "UTF-8";
protected static final String CHARSET = StandardCharsets.UTF_8.name();
protected static final String GET = HttpMethod.GET.name();
protected static final String POST = HttpMethod.POST.name();
protected static final int CONTENT_CACHE_LIMIT = 3;
private final MockHttpServletRequest request = new MockHttpServletRequest();
@Test
public void cachedContent() throws Exception {
this.request.setMethod("GET");
void cachedContent() throws Exception {
this.request.setMethod(GET);
this.request.setCharacterEncoding(CHARSET);
this.request.setContent("Hello World".getBytes(CHARSET));
@ -48,24 +58,24 @@ public class ContentCachingRequestWrapperTests {
}
@Test
public void cachedContentWithLimit() throws Exception {
this.request.setMethod("GET");
void cachedContentWithLimit() throws Exception {
this.request.setMethod(GET);
this.request.setCharacterEncoding(CHARSET);
this.request.setContent("Hello World".getBytes(CHARSET));
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, 3);
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, CONTENT_CACHE_LIMIT);
byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream());
assertThat(response).isEqualTo("Hello World".getBytes(CHARSET));
assertThat(wrapper.getContentAsByteArray()).isEqualTo("Hel".getBytes(CHARSET));
}
@Test
public void cachedContentWithOverflow() throws Exception {
this.request.setMethod("GET");
void cachedContentWithOverflow() throws Exception {
this.request.setMethod(GET);
this.request.setCharacterEncoding(CHARSET);
this.request.setContent("Hello World".getBytes(CHARSET));
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, 3) {
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, CONTENT_CACHE_LIMIT) {
@Override
protected void handleContentOverflow(int contentCacheLimit) {
throw new IllegalStateException(String.valueOf(contentCacheLimit));
@ -78,8 +88,8 @@ public class ContentCachingRequestWrapperTests {
}
@Test
public void requestParams() throws Exception {
this.request.setMethod("POST");
void requestParams() throws Exception {
this.request.setMethod(POST);
this.request.setContentType(FORM_CONTENT_TYPE);
this.request.setCharacterEncoding(CHARSET);
this.request.setParameter("first", "value");
@ -94,8 +104,8 @@ public class ContentCachingRequestWrapperTests {
}
@Test // SPR-12810
public void inputStreamFormPostRequest() throws Exception {
this.request.setMethod("POST");
void inputStreamFormPostRequest() throws Exception {
this.request.setMethod(POST);
this.request.setContentType(FORM_CONTENT_TYPE);
this.request.setCharacterEncoding(CHARSET);
this.request.setParameter("first", "value");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 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.
@ -45,7 +45,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class WebUtilsTests {
@Test
public void findParameterValue() {
void findParameterValue() {
Map<String, Object> params = new HashMap<>();
params.put("myKey1", "myValue1");
params.put("myKey2_myValue2", "xxx");
@ -60,7 +60,7 @@ public class WebUtilsTests {
}
@Test
public void parseMatrixVariablesString() {
void parseMatrixVariablesString() {
MultiValueMap<String, String> variables;
variables = WebUtils.parseMatrixVariables(null);
@ -103,7 +103,7 @@ public class WebUtilsTests {
}
@Test
public void isValidOrigin() {
void isValidOrigin() {
List<String> allowed = Collections.emptyList();
assertThat(checkValidOrigin("mydomain1.example", -1, "http://mydomain1.example", allowed)).isTrue();
assertThat(checkValidOrigin("mydomain1.example", -1, "http://mydomain2.example", allowed)).isFalse();
@ -117,7 +117,7 @@ public class WebUtilsTests {
}
@Test
public void isSameOrigin() {
void isSameOrigin() {
assertThat(checkSameOrigin("http", "mydomain1.example", -1, "http://mydomain1.example")).isTrue();
assertThat(checkSameOrigin("http", "mydomain1.example", -1, "http://mydomain1.example:80")).isTrue();
assertThat(checkSameOrigin("https", "mydomain1.example", 443, "https://mydomain1.example")).isTrue();
@ -156,7 +156,7 @@ public class WebUtilsTests {
}
@Test // SPR-16262
public void isSameOriginWithXForwardedHeaders() throws Exception {
void isSameOriginWithXForwardedHeaders() throws Exception {
String server = "mydomain1.example";
testWithXForwardedHeaders(server, -1, "https", null, -1, "https://mydomain1.example");
testWithXForwardedHeaders(server, 123, "https", null, -1, "https://mydomain1.example");
@ -167,7 +167,7 @@ public class WebUtilsTests {
}
@Test // SPR-16262
public void isSameOriginWithForwardedHeader() throws Exception {
void isSameOriginWithForwardedHeader() throws Exception {
String server = "mydomain1.example";
testWithForwardedHeader(server, -1, "proto=https", "https://mydomain1.example");
testWithForwardedHeader(server, 123, "proto=https", "https://mydomain1.example");