@@ -62,6 +62,8 @@ class HttpClientTransportTests {
62
62
63
63
private static final String APPLICATION_JSON = "application/json" ;
64
64
65
+ private static final String APPLICATION_X_TAR = "application/x-tar" ;
66
+
65
67
@ Mock
66
68
private CloseableHttpClient client ;
67
69
@@ -124,42 +126,86 @@ void postShouldExecuteHttpPost() throws Exception {
124
126
}
125
127
126
128
@ Test
127
- void postWithContentShouldExecuteHttpPost () throws Exception {
129
+ void postWithJsonContentShouldExecuteHttpPost () throws Exception {
130
+ String content = "test" ;
128
131
given (this .entity .getContent ()).willReturn (this .content );
129
132
given (this .statusLine .getStatusCode ()).willReturn (200 );
130
133
Response response = this .http .post (this .uri , APPLICATION_JSON ,
131
- (out ) -> StreamUtils .copy ("test" , StandardCharsets .UTF_8 , out ));
134
+ (out ) -> StreamUtils .copy (content , StandardCharsets .UTF_8 , out ));
135
+ verify (this .client ).execute (this .hostCaptor .capture (), this .requestCaptor .capture ());
136
+ HttpUriRequest request = this .requestCaptor .getValue ();
137
+ HttpEntity entity = ((HttpEntityEnclosingRequest ) request ).getEntity ();
138
+ assertThat (request ).isInstanceOf (HttpPost .class );
139
+ assertThat (request .getURI ()).isEqualTo (this .uri );
140
+ assertThat (entity .isRepeatable ()).isFalse ();
141
+ assertThat (entity .getContentLength ()).isEqualTo (content .length ());
142
+ assertThat (entity .getContentType ().getValue ()).isEqualTo (APPLICATION_JSON );
143
+ assertThat (entity .isStreaming ()).isTrue ();
144
+ assertThatExceptionOfType (UnsupportedOperationException .class ).isThrownBy (entity ::getContent );
145
+ assertThat (writeToString (entity )).isEqualTo (content );
146
+ assertThat (response .getContent ()).isSameAs (this .content );
147
+ }
148
+
149
+ @ Test
150
+ void postWithArchiveContentShouldExecuteHttpPost () throws Exception {
151
+ String content = "test" ;
152
+ given (this .entity .getContent ()).willReturn (this .content );
153
+ given (this .statusLine .getStatusCode ()).willReturn (200 );
154
+ Response response = this .http .post (this .uri , APPLICATION_X_TAR ,
155
+ (out ) -> StreamUtils .copy (content , StandardCharsets .UTF_8 , out ));
132
156
verify (this .client ).execute (this .hostCaptor .capture (), this .requestCaptor .capture ());
133
157
HttpUriRequest request = this .requestCaptor .getValue ();
134
158
HttpEntity entity = ((HttpEntityEnclosingRequest ) request ).getEntity ();
135
159
assertThat (request ).isInstanceOf (HttpPost .class );
136
160
assertThat (request .getURI ()).isEqualTo (this .uri );
137
- assertThat (request .getFirstHeader (HttpHeaders .CONTENT_TYPE ).getValue ()).isEqualTo (APPLICATION_JSON );
138
161
assertThat (entity .isRepeatable ()).isFalse ();
139
162
assertThat (entity .getContentLength ()).isEqualTo (-1 );
163
+ assertThat (entity .getContentType ().getValue ()).isEqualTo (APPLICATION_X_TAR );
140
164
assertThat (entity .isStreaming ()).isTrue ();
141
165
assertThatExceptionOfType (UnsupportedOperationException .class ).isThrownBy (entity ::getContent );
142
- assertThat (writeToString (entity )).isEqualTo ("test" );
166
+ assertThat (writeToString (entity )).isEqualTo (content );
143
167
assertThat (response .getContent ()).isSameAs (this .content );
144
168
}
145
169
146
170
@ Test
147
- void putWithContentShouldExecuteHttpPut () throws Exception {
171
+ void putWithJsonContentShouldExecuteHttpPut () throws Exception {
172
+ String content = "test" ;
148
173
given (this .entity .getContent ()).willReturn (this .content );
149
174
given (this .statusLine .getStatusCode ()).willReturn (200 );
150
175
Response response = this .http .put (this .uri , APPLICATION_JSON ,
151
- (out ) -> StreamUtils .copy ("test" , StandardCharsets .UTF_8 , out ));
176
+ (out ) -> StreamUtils .copy (content , StandardCharsets .UTF_8 , out ));
177
+ verify (this .client ).execute (this .hostCaptor .capture (), this .requestCaptor .capture ());
178
+ HttpUriRequest request = this .requestCaptor .getValue ();
179
+ HttpEntity entity = ((HttpEntityEnclosingRequest ) request ).getEntity ();
180
+ assertThat (request ).isInstanceOf (HttpPut .class );
181
+ assertThat (request .getURI ()).isEqualTo (this .uri );
182
+ assertThat (entity .isRepeatable ()).isFalse ();
183
+ assertThat (entity .getContentLength ()).isEqualTo (content .length ());
184
+ assertThat (entity .getContentType ().getValue ()).isEqualTo (APPLICATION_JSON );
185
+ assertThat (entity .isStreaming ()).isTrue ();
186
+ assertThatExceptionOfType (UnsupportedOperationException .class ).isThrownBy (entity ::getContent );
187
+ assertThat (writeToString (entity )).isEqualTo (content );
188
+ assertThat (response .getContent ()).isSameAs (this .content );
189
+ }
190
+
191
+ @ Test
192
+ void putWithArchiveContentShouldExecuteHttpPut () throws Exception {
193
+ String content = "test" ;
194
+ given (this .entity .getContent ()).willReturn (this .content );
195
+ given (this .statusLine .getStatusCode ()).willReturn (200 );
196
+ Response response = this .http .put (this .uri , APPLICATION_X_TAR ,
197
+ (out ) -> StreamUtils .copy (content , StandardCharsets .UTF_8 , out ));
152
198
verify (this .client ).execute (this .hostCaptor .capture (), this .requestCaptor .capture ());
153
199
HttpUriRequest request = this .requestCaptor .getValue ();
154
200
HttpEntity entity = ((HttpEntityEnclosingRequest ) request ).getEntity ();
155
201
assertThat (request ).isInstanceOf (HttpPut .class );
156
202
assertThat (request .getURI ()).isEqualTo (this .uri );
157
- assertThat (request .getFirstHeader (HttpHeaders .CONTENT_TYPE ).getValue ()).isEqualTo (APPLICATION_JSON );
158
203
assertThat (entity .isRepeatable ()).isFalse ();
159
204
assertThat (entity .getContentLength ()).isEqualTo (-1 );
205
+ assertThat (entity .getContentType ().getValue ()).isEqualTo (APPLICATION_X_TAR );
160
206
assertThat (entity .isStreaming ()).isTrue ();
161
207
assertThatExceptionOfType (UnsupportedOperationException .class ).isThrownBy (entity ::getContent );
162
- assertThat (writeToString (entity )).isEqualTo ("test" );
208
+ assertThat (writeToString (entity )).isEqualTo (content );
163
209
assertThat (response .getContent ()).isSameAs (this .content );
164
210
}
165
211
0 commit comments