From 72119120570c7607d34e77973726a5346184b083 Mon Sep 17 00:00:00 2001 From: shirohoo Date: Thu, 20 Jan 2022 14:06:13 +0900 Subject: [PATCH] Polish tests in spring-web This PR polishes trivial things in tests in spring-web. Closes gh-27958 --- .../ContentCachingRequestWrapperTests.java | 40 ++++++++++++------- .../web/util/WebUtilsTests.java | 14 +++---- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/spring-web/src/test/java/org/springframework/web/util/ContentCachingRequestWrapperTests.java b/spring-web/src/test/java/org/springframework/web/util/ContentCachingRequestWrapperTests.java index 8f8ac90629bd..d9132561a26c 100644 --- a/spring-web/src/test/java/org/springframework/web/util/ContentCachingRequestWrapperTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/ContentCachingRequestWrapperTests.java @@ -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 @@ */ 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)); @@ -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)); @@ -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"); @@ -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"); diff --git a/spring-web/src/test/java/org/springframework/web/util/WebUtilsTests.java b/spring-web/src/test/java/org/springframework/web/util/WebUtilsTests.java index 998af1c9eb16..074b9b5c5c8d 100644 --- a/spring-web/src/test/java/org/springframework/web/util/WebUtilsTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/WebUtilsTests.java @@ -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 @@ public class WebUtilsTests { @Test - public void findParameterValue() { + void findParameterValue() { Map params = new HashMap<>(); params.put("myKey1", "myValue1"); params.put("myKey2_myValue2", "xxx"); @@ -60,7 +60,7 @@ public void findParameterValue() { } @Test - public void parseMatrixVariablesString() { + void parseMatrixVariablesString() { MultiValueMap variables; variables = WebUtils.parseMatrixVariables(null); @@ -103,7 +103,7 @@ public void parseMatrixVariablesString() { } @Test - public void isValidOrigin() { + void isValidOrigin() { List 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 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(); @@ -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"); @@ -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");