1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .http .server ;
18
18
19
19
import java .nio .charset .StandardCharsets ;
20
- import java .util .Collections ;
21
20
import java .util .List ;
22
21
23
22
import org .junit .jupiter .api .BeforeEach ;
@@ -44,20 +43,20 @@ public class ServletServerHttpResponseTests {
44
43
45
44
46
45
@ BeforeEach
47
- public void create () throws Exception {
46
+ void create () {
48
47
mockResponse = new MockHttpServletResponse ();
49
48
response = new ServletServerHttpResponse (mockResponse );
50
49
}
51
50
52
51
53
52
@ Test
54
- public void setStatusCode () throws Exception {
53
+ void setStatusCode () {
55
54
response .setStatusCode (HttpStatus .NOT_FOUND );
56
55
assertThat (mockResponse .getStatus ()).as ("Invalid status code" ).isEqualTo (404 );
57
56
}
58
57
59
58
@ Test
60
- public void getHeaders () throws Exception {
59
+ void getHeaders () {
61
60
HttpHeaders headers = response .getHeaders ();
62
61
String headerName = "MyHeader" ;
63
62
String headerValue1 = "value1" ;
@@ -77,23 +76,32 @@ public void getHeaders() throws Exception {
77
76
}
78
77
79
78
@ Test
80
- public void preExistingHeadersFromHttpServletResponse () {
79
+ void preExistingHeadersFromHttpServletResponse () {
81
80
String headerName = "Access-Control-Allow-Origin" ;
82
81
String headerValue = "localhost:8080" ;
83
82
84
83
this .mockResponse .addHeader (headerName , headerValue );
84
+ this .mockResponse .setContentType ("text/csv" );
85
85
this .response = new ServletServerHttpResponse (this .mockResponse );
86
86
87
87
assertThat (this .response .getHeaders ().getFirst (headerName )).isEqualTo (headerValue );
88
- assertThat (this .response .getHeaders ().get (headerName )).isEqualTo (Collections .singletonList (headerValue ));
89
- assertThat (this .response .getHeaders ().containsKey (headerName )).isTrue ();
90
- assertThat (this .response .getHeaders ().getFirst (headerName )).isEqualTo (headerValue );
88
+ assertThat (this .response .getHeaders ().get (headerName )).containsExactly (headerValue );
89
+ assertThat (this .response .getHeaders ()).containsKey (headerName );
91
90
assertThat (this .response .getHeaders ().getAccessControlAllowOrigin ()).isEqualTo (headerValue );
92
91
}
93
92
93
+ @ Test // gh-25490
94
+ void preExistingContentTypeIsOverriddenImmediately () {
95
+ this .mockResponse .setContentType ("text/csv" );
96
+ this .response = new ServletServerHttpResponse (this .mockResponse );
97
+ this .response .getHeaders ().setContentType (MediaType .APPLICATION_JSON );
98
+
99
+ assertThat (response .getHeaders ().getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
100
+ }
101
+
94
102
@ Test
95
- public void getBody () throws Exception {
96
- byte [] content = "Hello World" .getBytes ("UTF-8" );
103
+ void getBody () throws Exception {
104
+ byte [] content = "Hello World" .getBytes (StandardCharsets . UTF_8 );
97
105
FileCopyUtils .copy (content , response .getBody ());
98
106
99
107
assertThat (mockResponse .getContentAsByteArray ()).as ("Invalid content written" ).isEqualTo (content );
0 commit comments