Skip to content

Commit

Permalink
Polish tests in spring-web
Browse files Browse the repository at this point in the history
This PR polishes trivial things in tests in spring-web.

Closes gh-27958
  • Loading branch information
1chz authored and sbrannen committed Jan 20, 2022
1 parent 148eac0 commit 7211912
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;

Expand All @@ -29,16 +33,22 @@
*/
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 = StandardCharsets.UTF_8.name();

protected static final String GET = HttpMethod.GET.name();

protected static final String POST = HttpMethod.POST.name();

protected static final String CHARSET = "UTF-8";
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));

Expand All @@ -48,24 +58,24 @@ public void cachedContent() throws Exception {
}

@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));
Expand All @@ -78,8 +88,8 @@ protected void handleContentOverflow(int contentCacheLimit) {
}

@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");
Expand All @@ -94,8 +104,8 @@ public void requestParams() throws Exception {
}

@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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -45,7 +45,7 @@
public class WebUtilsTests {

@Test
public void findParameterValue() {
void findParameterValue() {
Map<String, Object> params = new HashMap<>();
params.put("myKey1", "myValue1");
params.put("myKey2_myValue2", "xxx");
Expand All @@ -60,7 +60,7 @@ public void findParameterValue() {
}

@Test
public void parseMatrixVariablesString() {
void parseMatrixVariablesString() {
MultiValueMap<String, String> variables;

variables = WebUtils.parseMatrixVariables(null);
Expand Down Expand Up @@ -103,7 +103,7 @@ public void parseMatrixVariablesString() {
}

@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();
Expand All @@ -117,7 +117,7 @@ public void isValidOrigin() {
}

@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();
Expand Down Expand Up @@ -156,7 +156,7 @@ public void isSameOrigin() {
}

@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");
Expand All @@ -167,7 +167,7 @@ public void isSameOriginWithXForwardedHeaders() throws Exception {
}

@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");
Expand Down

0 comments on commit 7211912

Please sign in to comment.