1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 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.
16
16
17
17
package org .springframework .http .client ;
18
18
19
- import java .io .IOException ;
20
- import java .io .OutputStream ;
21
19
import java .net .URI ;
22
20
import java .util .Arrays ;
23
21
import java .util .Locale ;
@@ -69,6 +67,7 @@ public void status() throws Exception {
69
67
ClientHttpRequest request = factory .createRequest (uri , HttpMethod .GET );
70
68
assertEquals ("Invalid HTTP method" , HttpMethod .GET , request .getMethod ());
71
69
assertEquals ("Invalid HTTP URI" , uri , request .getURI ());
70
+
72
71
ClientHttpResponse response = request .execute ();
73
72
try {
74
73
assertEquals ("Invalid status code" , HttpStatus .NOT_FOUND , response .getStatusCode ());
@@ -82,26 +81,23 @@ public void status() throws Exception {
82
81
public void echo () throws Exception {
83
82
ClientHttpRequest request = factory .createRequest (new URI (baseUrl + "/echo" ), HttpMethod .PUT );
84
83
assertEquals ("Invalid HTTP method" , HttpMethod .PUT , request .getMethod ());
84
+
85
85
String headerName = "MyHeader" ;
86
86
String headerValue1 = "value1" ;
87
87
request .getHeaders ().add (headerName , headerValue1 );
88
88
String headerValue2 = "value2" ;
89
89
request .getHeaders ().add (headerName , headerValue2 );
90
90
final byte [] body = "Hello World" .getBytes ("UTF-8" );
91
91
request .getHeaders ().setContentLength (body .length );
92
+
92
93
if (request instanceof StreamingHttpOutputMessage ) {
93
- StreamingHttpOutputMessage streamingRequest =
94
- (StreamingHttpOutputMessage ) request ;
95
- streamingRequest .setBody (new StreamingHttpOutputMessage .Body () {
96
- @ Override
97
- public void writeTo (OutputStream outputStream ) throws IOException {
98
- StreamUtils .copy (body , outputStream );
99
- }
100
- });
94
+ StreamingHttpOutputMessage streamingRequest = (StreamingHttpOutputMessage ) request ;
95
+ streamingRequest .setBody (outputStream -> StreamUtils .copy (body , outputStream ));
101
96
}
102
97
else {
103
98
StreamUtils .copy (body , request .getBody ());
104
99
}
100
+
105
101
ClientHttpResponse response = request .execute ();
106
102
try {
107
103
assertEquals ("Invalid status code" , HttpStatus .OK , response .getStatusCode ());
@@ -119,17 +115,14 @@ public void writeTo(OutputStream outputStream) throws IOException {
119
115
@ Test (expected = IllegalStateException .class )
120
116
public void multipleWrites () throws Exception {
121
117
ClientHttpRequest request = factory .createRequest (new URI (baseUrl + "/echo" ), HttpMethod .POST );
118
+
122
119
final byte [] body = "Hello World" .getBytes ("UTF-8" );
123
120
if (request instanceof StreamingHttpOutputMessage ) {
124
- StreamingHttpOutputMessage streamingRequest =
125
- (StreamingHttpOutputMessage ) request ;
126
- streamingRequest .setBody (new StreamingHttpOutputMessage .Body () {
127
- @ Override
128
- public void writeTo (OutputStream outputStream ) throws IOException {
129
- StreamUtils .copy (body , outputStream );
130
- outputStream .flush ();
131
- outputStream .close ();
132
- }
121
+ StreamingHttpOutputMessage streamingRequest = (StreamingHttpOutputMessage ) request ;
122
+ streamingRequest .setBody (outputStream -> {
123
+ StreamUtils .copy (body , outputStream );
124
+ outputStream .flush ();
125
+ outputStream .close ();
133
126
});
134
127
}
135
128
else {
@@ -143,9 +136,11 @@ public void writeTo(OutputStream outputStream) throws IOException {
143
136
@ Test (expected = UnsupportedOperationException .class )
144
137
public void headersAfterExecute () throws Exception {
145
138
ClientHttpRequest request = factory .createRequest (new URI (baseUrl + "/echo" ), HttpMethod .POST );
139
+
146
140
request .getHeaders ().add ("MyHeader" , "value" );
147
141
byte [] body = "Hello World" .getBytes ("UTF-8" );
148
142
FileCopyUtils .copy (body , request .getBody ());
143
+
149
144
ClientHttpResponse response = request .execute ();
150
145
try {
151
146
request .getHeaders ().add ("MyHeader" , "value" );
0 commit comments