19
19
import java .io .IOException ;
20
20
import java .nio .charset .StandardCharsets ;
21
21
22
- import org .junit .jupiter .api .BeforeEach ;
23
22
import org .junit .jupiter .api .Test ;
24
23
25
24
import org .springframework .http .HttpHeaders ;
30
29
import static org .assertj .core .api .Assertions .assertThat ;
31
30
32
31
/**
32
+ * Tests for {@link StringHttpMessageConverter}.
33
+ *
33
34
* @author Arjen Poutsma
34
35
* @author Rossen Stoyanchev
35
36
*/
36
- public class StringHttpMessageConverterTests {
37
-
38
- public static final MediaType TEXT_PLAIN_UTF_8 = new MediaType ("text" , "plain" , StandardCharsets .UTF_8 );
37
+ class StringHttpMessageConverterTests {
39
38
40
- private StringHttpMessageConverter converter ;
39
+ private static final MediaType TEXT_PLAIN_UTF_8 = new MediaType ( "text" , "plain" , StandardCharsets . UTF_8 ) ;
41
40
42
- private MockHttpOutputMessage outputMessage ;
41
+ private final StringHttpMessageConverter converter = new StringHttpMessageConverter () ;
43
42
44
-
45
- @ BeforeEach
46
- public void setUp () {
47
- this .converter = new StringHttpMessageConverter ();
48
- this .outputMessage = new MockHttpOutputMessage ();
49
- }
43
+ private final MockHttpOutputMessage outputMessage = new MockHttpOutputMessage ();
50
44
51
45
52
46
@ Test
53
- public void canRead () {
47
+ void canRead () {
54
48
assertThat (this .converter .canRead (String .class , MediaType .TEXT_PLAIN )).isTrue ();
55
49
}
56
50
57
51
@ Test
58
- public void canWrite () {
52
+ void canWrite () {
59
53
assertThat (this .converter .canWrite (String .class , MediaType .TEXT_PLAIN )).isTrue ();
60
54
assertThat (this .converter .canWrite (String .class , MediaType .ALL )).isTrue ();
61
55
}
62
56
63
57
@ Test
64
- public void read () throws IOException {
58
+ void read () throws IOException {
65
59
String body = "Hello World" ;
66
60
MockHttpInputMessage inputMessage = new MockHttpInputMessage (body .getBytes (StandardCharsets .UTF_8 ));
67
61
inputMessage .getHeaders ().setContentType (TEXT_PLAIN_UTF_8 );
@@ -71,7 +65,7 @@ public void read() throws IOException {
71
65
}
72
66
73
67
@ Test
74
- public void readWithContentLengthHeader () throws IOException {
68
+ void readWithContentLengthHeader () throws IOException {
75
69
String body = "Hello World" ;
76
70
MockHttpInputMessage inputMessage = new MockHttpInputMessage (body .getBytes (StandardCharsets .UTF_8 ));
77
71
inputMessage .getHeaders ().setContentLength (body .length ());
@@ -82,7 +76,7 @@ public void readWithContentLengthHeader() throws IOException {
82
76
}
83
77
84
78
@ Test // gh-24123
85
- public void readJson () throws IOException {
79
+ void readJson () throws IOException {
86
80
String body = "{\" result\" :\" \u0414 \u0410 \" }" ;
87
81
MockHttpInputMessage inputMessage = new MockHttpInputMessage (body .getBytes (StandardCharsets .UTF_8 ));
88
82
inputMessage .getHeaders ().setContentType (MediaType .APPLICATION_JSON );
@@ -92,7 +86,7 @@ public void readJson() throws IOException {
92
86
}
93
87
94
88
@ Test // gh-25328
95
- public void readJsonApi () throws IOException {
89
+ void readJsonApi () throws IOException {
96
90
String body = "{\" result\" :\" \u0414 \u0410 \" }" ;
97
91
MockHttpInputMessage inputMessage = new MockHttpInputMessage (body .getBytes (StandardCharsets .UTF_8 ));
98
92
inputMessage .getHeaders ().setContentType (new MediaType ("application" , "vnd.api.v1+json" ));
@@ -102,31 +96,31 @@ public void readJsonApi() throws IOException {
102
96
}
103
97
104
98
@ Test
105
- public void writeDefaultCharset () throws IOException {
99
+ void writeDefaultCharset () throws IOException {
106
100
String body = "H\u00e9 llo W\u00f6 rld" ;
107
101
this .converter .write (body , null , this .outputMessage );
108
102
109
103
HttpHeaders headers = this .outputMessage .getHeaders ();
110
104
assertThat (this .outputMessage .getBodyAsString (StandardCharsets .ISO_8859_1 )).isEqualTo (body );
111
105
assertThat (headers .getContentType ()).isEqualTo (new MediaType ("text" , "plain" , StandardCharsets .ISO_8859_1 ));
112
106
assertThat (headers .getContentLength ()).isEqualTo (body .getBytes (StandardCharsets .ISO_8859_1 ).length );
113
- assertThat (headers .getAcceptCharset (). isEmpty ()). isTrue ();
107
+ assertThat (headers .getAcceptCharset ()). isEmpty ();
114
108
}
115
109
116
110
@ Test // gh-24123
117
- public void writeJson () throws IOException {
111
+ void writeJson () throws IOException {
118
112
String body = "{\" føø\" :\" bår\" }" ;
119
113
this .converter .write (body , MediaType .APPLICATION_JSON , this .outputMessage );
120
114
121
115
HttpHeaders headers = this .outputMessage .getHeaders ();
122
116
assertThat (this .outputMessage .getBodyAsString (StandardCharsets .UTF_8 )).isEqualTo (body );
123
117
assertThat (headers .getContentType ()).isEqualTo (MediaType .APPLICATION_JSON );
124
118
assertThat (headers .getContentLength ()).isEqualTo (body .getBytes (StandardCharsets .UTF_8 ).length );
125
- assertThat (headers .getAcceptCharset (). isEmpty ()). isTrue ();
119
+ assertThat (headers .getAcceptCharset ()). isEmpty ();
126
120
}
127
121
128
122
@ Test // gh-25328
129
- public void writeJsonApi () throws IOException {
123
+ void writeJsonApi () throws IOException {
130
124
String body = "{\" føø\" :\" bår\" }" ;
131
125
MediaType contentType = new MediaType ("application" , "vnd.api.v1+json" );
132
126
this .converter .write (body , contentType , this .outputMessage );
@@ -135,23 +129,23 @@ public void writeJsonApi() throws IOException {
135
129
assertThat (this .outputMessage .getBodyAsString (StandardCharsets .UTF_8 )).isEqualTo (body );
136
130
assertThat (headers .getContentType ()).isEqualTo (contentType );
137
131
assertThat (headers .getContentLength ()).isEqualTo (body .getBytes (StandardCharsets .UTF_8 ).length );
138
- assertThat (headers .getAcceptCharset (). isEmpty ()). isTrue ();
132
+ assertThat (headers .getAcceptCharset ()). isEmpty ();
139
133
}
140
134
141
135
@ Test
142
- public void writeUTF8 () throws IOException {
136
+ void writeUTF8 () throws IOException {
143
137
String body = "H\u00e9 llo W\u00f6 rld" ;
144
138
this .converter .write (body , TEXT_PLAIN_UTF_8 , this .outputMessage );
145
139
146
140
HttpHeaders headers = this .outputMessage .getHeaders ();
147
141
assertThat (this .outputMessage .getBodyAsString (StandardCharsets .UTF_8 )).isEqualTo (body );
148
142
assertThat (headers .getContentType ()).isEqualTo (TEXT_PLAIN_UTF_8 );
149
143
assertThat (headers .getContentLength ()).isEqualTo (body .getBytes (StandardCharsets .UTF_8 ).length );
150
- assertThat (headers .getAcceptCharset (). isEmpty ()). isTrue ();
144
+ assertThat (headers .getAcceptCharset ()). isEmpty ();
151
145
}
152
146
153
147
@ Test // SPR-8867
154
- public void writeOverrideRequestedContentType () throws IOException {
148
+ void writeOverrideRequestedContentType () throws IOException {
155
149
String body = "H\u00e9 llo W\u00f6 rld" ;
156
150
MediaType requestedContentType = new MediaType ("text" , "html" );
157
151
@@ -162,19 +156,19 @@ public void writeOverrideRequestedContentType() throws IOException {
162
156
assertThat (this .outputMessage .getBodyAsString (StandardCharsets .UTF_8 )).isEqualTo (body );
163
157
assertThat (headers .getContentType ()).isEqualTo (TEXT_PLAIN_UTF_8 );
164
158
assertThat (headers .getContentLength ()).isEqualTo (body .getBytes (StandardCharsets .UTF_8 ).length );
165
- assertThat (headers .getAcceptCharset (). isEmpty ()). isTrue ();
159
+ assertThat (headers .getAcceptCharset ()). isEmpty ();
166
160
}
167
161
168
162
@ Test // gh-24283
169
- public void writeWithWildCardMediaType () throws IOException {
163
+ void writeWithWildCardMediaType () throws IOException {
170
164
String body = "Hello World" ;
171
165
this .converter .write (body , MediaType .ALL , this .outputMessage );
172
166
173
167
HttpHeaders headers = this .outputMessage .getHeaders ();
174
168
assertThat (this .outputMessage .getBodyAsString (StandardCharsets .US_ASCII )).isEqualTo (body );
175
169
assertThat (headers .getContentType ()).isEqualTo (new MediaType ("text" , "plain" , StandardCharsets .ISO_8859_1 ));
176
170
assertThat (headers .getContentLength ()).isEqualTo (body .getBytes ().length );
177
- assertThat (headers .getAcceptCharset (). isEmpty ()). isTrue ();
171
+ assertThat (headers .getAcceptCharset ()). isEmpty ();
178
172
}
179
173
180
174
}
0 commit comments