@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2019 the original author or authors .
* Copyright 2002 - 2020 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 .
@ -17,7 +17,6 @@
@@ -17,7 +17,6 @@
package org.springframework.http.server ;
import java.nio.charset.StandardCharsets ;
import java.util.Collections ;
import java.util.List ;
import org.junit.jupiter.api.BeforeEach ;
@ -44,20 +43,20 @@ public class ServletServerHttpResponseTests {
@@ -44,20 +43,20 @@ public class ServletServerHttpResponseTests {
@BeforeEach
public void create ( ) throws Exception {
void create ( ) {
mockResponse = new MockHttpServletResponse ( ) ;
response = new ServletServerHttpResponse ( mockResponse ) ;
}
@Test
public void setStatusCode ( ) throws Exception {
void setStatusCode ( ) {
response . setStatusCode ( HttpStatus . NOT_FOUND ) ;
assertThat ( mockResponse . getStatus ( ) ) . as ( "Invalid status code" ) . isEqualTo ( 404 ) ;
}
@Test
public void getHeaders ( ) throws Exception {
void getHeaders ( ) {
HttpHeaders headers = response . getHeaders ( ) ;
String headerName = "MyHeader" ;
String headerValue1 = "value1" ;
@ -77,23 +76,32 @@ public class ServletServerHttpResponseTests {
@@ -77,23 +76,32 @@ public class ServletServerHttpResponseTests {
}
@Test
public void preExistingHeadersFromHttpServletResponse ( ) {
void preExistingHeadersFromHttpServletResponse ( ) {
String headerName = "Access-Control-Allow-Origin" ;
String headerValue = "localhost:8080" ;
this . mockResponse . addHeader ( headerName , headerValue ) ;
this . mockResponse . setContentType ( "text/csv" ) ;
this . response = new ServletServerHttpResponse ( this . mockResponse ) ;
assertThat ( this . response . getHeaders ( ) . getFirst ( headerName ) ) . isEqualTo ( headerValue ) ;
assertThat ( this . response . getHeaders ( ) . get ( headerName ) ) . isEqualTo ( Collections . singletonList ( headerValue ) ) ;
assertThat ( this . response . getHeaders ( ) . containsKey ( headerName ) ) . isTrue ( ) ;
assertThat ( this . response . getHeaders ( ) . getFirst ( headerName ) ) . isEqualTo ( headerValue ) ;
assertThat ( this . response . getHeaders ( ) . get ( headerName ) ) . containsExactly ( headerValue ) ;
assertThat ( this . response . getHeaders ( ) ) . containsKey ( headerName ) ;
assertThat ( this . response . getHeaders ( ) . getAccessControlAllowOrigin ( ) ) . isEqualTo ( headerValue ) ;
}
@Test // gh-25490
void preExistingContentTypeIsOverriddenImmediately ( ) {
this . mockResponse . setContentType ( "text/csv" ) ;
this . response = new ServletServerHttpResponse ( this . mockResponse ) ;
this . response . getHeaders ( ) . setContentType ( MediaType . APPLICATION_JSON ) ;
assertThat ( response . getHeaders ( ) . getContentType ( ) ) . isEqualTo ( MediaType . APPLICATION_JSON ) ;
}
@Test
public void getBody ( ) throws Exception {
byte [ ] content = "Hello World" . getBytes ( "UTF-8" ) ;
void getBody ( ) throws Exception {
byte [ ] content = "Hello World" . getBytes ( StandardCharsets . UTF_8 ) ;
FileCopyUtils . copy ( content , response . getBody ( ) ) ;
assertThat ( mockResponse . getContentAsByteArray ( ) ) . as ( "Invalid content written" ) . isEqualTo ( content ) ;