44package com .azure .core .http .okhttp ;
55
66import com .azure .core .http .HttpClient ;
7+ import com .azure .core .http .HttpHeader ;
8+ import com .azure .core .http .HttpHeaders ;
79import com .azure .core .http .HttpMethod ;
810import com .azure .core .http .HttpRequest ;
911import com .azure .core .http .HttpResponse ;
1012import com .github .tomakehurst .wiremock .WireMockServer ;
11- import com .github .tomakehurst .wiremock .client .WireMock ;
1213import com .github .tomakehurst .wiremock .core .WireMockConfiguration ;
1314import org .junit .jupiter .api .AfterAll ;
1415import org .junit .jupiter .api .Assertions ;
3233import java .security .MessageDigest ;
3334import java .security .NoSuchAlgorithmException ;
3435import java .time .Duration ;
36+ import java .util .Arrays ;
37+ import java .util .List ;
3538import java .util .concurrent .CountDownLatch ;
3639
40+ import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
41+ import static com .github .tomakehurst .wiremock .client .WireMock .get ;
42+ import static com .github .tomakehurst .wiremock .client .WireMock .post ;
3743import static org .junit .jupiter .api .Assertions .assertEquals ;
44+ import static org .junit .jupiter .api .Assertions .assertLinesMatch ;
3845import static org .junit .jupiter .api .Assertions .assertNotNull ;
3946
4047public class OkHttpClientTests {
48+ static final String RETURN_HEADERS_AS_IS_PATH = "/returnHeadersAsIs" ;
4149
4250 private static final String SHORT_BODY = "hi there" ;
4351 private static final String LONG_BODY = createLongBody ();
@@ -46,14 +54,18 @@ public class OkHttpClientTests {
4654
4755 @ BeforeAll
4856 public static void beforeClass () {
49- server = new WireMockServer (WireMockConfiguration .options ().dynamicPort ().disableRequestJournal ());
50- server .stubFor (
51- WireMock .get ("/short" ).willReturn (WireMock .aResponse ().withBody (SHORT_BODY )));
52- server .stubFor (WireMock .get ("/long" ).willReturn (WireMock .aResponse ().withBody (LONG_BODY )));
53- server .stubFor (WireMock .get ("/error" )
54- .willReturn (WireMock .aResponse ().withBody ("error" ).withStatus (500 )));
55- server .stubFor (
56- WireMock .post ("/shortPost" ).willReturn (WireMock .aResponse ().withBody (SHORT_BODY )));
57+ server = new WireMockServer (WireMockConfiguration .options ()
58+ .extensions (new OkHttpAsyncHttpClientResponseTransformer ())
59+ .dynamicPort ()
60+ .disableRequestJournal ()
61+ .gzipDisabled (true ));
62+
63+ server .stubFor (get ("/short" ).willReturn (aResponse ().withBody (SHORT_BODY )));
64+ server .stubFor (get ("/long" ).willReturn (aResponse ().withBody (LONG_BODY )));
65+ server .stubFor (get ("/error" ).willReturn (aResponse ().withBody ("error" ).withStatus (500 )));
66+ server .stubFor (post ("/shortPost" ).willReturn (aResponse ().withBody (SHORT_BODY )));
67+ server .stubFor (get (RETURN_HEADERS_AS_IS_PATH ).willReturn (aResponse ()
68+ .withTransformers (OkHttpAsyncHttpClientResponseTransformer .NAME )));
5769 server .start ();
5870 }
5971
@@ -96,22 +108,21 @@ public void testFlowableWhenServerReturnsBodyAndNoErrorsWhenHttp500Returned() {
96108 assertEquals (500 , response .getStatusCode ());
97109 }
98110
99- @ Disabled ("Not working accurately at present" )
100111 @ Test
101112 public void testFlowableBackpressure () {
102113 HttpResponse response = getResponse ("/long" );
103- //
114+
104115 StepVerifierOptions stepVerifierOptions = StepVerifierOptions .create ();
105116 stepVerifierOptions .initialRequest (0 );
106- //
117+
107118 StepVerifier .create (response .getBody (), stepVerifierOptions )
108119 .expectNextCount (0 )
109120 .thenRequest (1 )
110121 .expectNextCount (1 )
111122 .thenRequest (3 )
112123 .expectNextCount (3 )
113- .thenRequest (Long .MAX_VALUE )// TODO: Check with smaldini, what is the verifier operator to ignore all next emissions
114- .expectNextCount ( 1507 )
124+ .thenRequest (Long .MAX_VALUE )
125+ .thenConsumeWhile ( ByteBuffer :: hasRemaining )
115126 .verifyComplete ();
116127 }
117128
@@ -228,6 +239,38 @@ public void testConcurrentRequests() throws NoSuchAlgorithmException {
228239// assertEquals(numRequests * LONG_BODY.getBytes(StandardCharsets.UTF_8).length, numBytes);
229240 }
230241
242+ @ Test
243+ public void validateHeadersReturnAsIs () {
244+ HttpClient client = new OkHttpClientProvider ().createInstance ();
245+
246+ final String singleValueHeaderName = "singleValue" ;
247+ final String singleValueHeaderValue = "value" ;
248+
249+ final String multiValueHeaderName = "Multi-value" ;
250+ final List <String > multiValueHeaderValue = Arrays .asList ("value1" , "value2" );
251+
252+ HttpHeaders headers = new HttpHeaders ()
253+ .set (singleValueHeaderName , singleValueHeaderValue )
254+ .set (multiValueHeaderName , multiValueHeaderValue );
255+
256+ StepVerifier .create (client .send (new HttpRequest (HttpMethod .GET , url (server , RETURN_HEADERS_AS_IS_PATH ),
257+ headers , Flux .empty ())))
258+ .assertNext (response -> {
259+ assertEquals (200 , response .getStatusCode ());
260+
261+ HttpHeaders responseHeaders = response .getHeaders ();
262+ HttpHeader singleValueHeader = responseHeaders .get (singleValueHeaderName );
263+ assertEquals (singleValueHeaderName , singleValueHeader .getName ());
264+ assertEquals (singleValueHeaderValue , singleValueHeader .getValue ());
265+
266+ HttpHeader multiValueHeader = responseHeaders .get ("Multi-value" );
267+ assertEquals (multiValueHeaderName , multiValueHeader .getName ());
268+ assertLinesMatch (multiValueHeaderValue , multiValueHeader .getValuesList ());
269+ })
270+ .expectComplete ()
271+ .verify (Duration .ofSeconds (10 ));
272+ }
273+
231274 private static MessageDigest md5Digest () {
232275 try {
233276 return MessageDigest .getInstance ("MD5" );
0 commit comments