21
21
import java .util .ArrayList ;
22
22
import java .util .List ;
23
23
import java .util .regex .Pattern ;
24
+ import javax .net .ssl .HostnameVerifier ;
24
25
import okhttp3 .Dns ;
25
26
import okhttp3 .HttpUrl ;
26
27
import okhttp3 .MediaType ;
27
28
import okhttp3 .OkHttpClient ;
29
+ import okhttp3 .Protocol ;
30
+ import okhttp3 .RecordingHostnameVerifier ;
28
31
import okhttp3 .Request ;
29
32
import okhttp3 .RequestBody ;
30
33
import okhttp3 .Response ;
34
+ import okhttp3 .internal .tls .SslClient ;
31
35
import okhttp3 .logging .HttpLoggingInterceptor .Level ;
32
36
import okhttp3 .mockwebserver .MockResponse ;
33
37
import okhttp3 .mockwebserver .MockWebServer ;
@@ -50,6 +54,8 @@ public final class HttpLoggingInterceptorTest {
50
54
51
55
@ Rule public final MockWebServer server = new MockWebServer ();
52
56
57
+ private SslClient sslClient = SslClient .localhost ();
58
+ private HostnameVerifier hostnameVerifier = new RecordingHostnameVerifier ();
53
59
private OkHttpClient client ;
54
60
private String host ;
55
61
private HttpUrl url ;
@@ -71,6 +77,8 @@ private void setLevel(Level level) {
71
77
client = new OkHttpClient .Builder ()
72
78
.addNetworkInterceptor (networkInterceptor )
73
79
.addInterceptor (applicationInterceptor )
80
+ .sslSocketFactory (sslClient .socketFactory , sslClient .trustManager )
81
+ .hostnameVerifier (hostnameVerifier )
74
82
.build ();
75
83
76
84
host = server .getHostName () + ":" + server .getPort ();
@@ -117,7 +125,7 @@ private void setLevel(Level level) {
117
125
client .newCall (request ().build ()).execute ();
118
126
119
127
applicationLogs
120
- .assertLogEqual ("--> GET " + url + " http/1.1" )
128
+ .assertLogEqual ("--> GET " + url )
121
129
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms, 0-byte body\\ )" )
122
130
.assertNoMoreLogs ();
123
131
@@ -134,7 +142,7 @@ private void setLevel(Level level) {
134
142
client .newCall (request ().post (RequestBody .create (PLAIN , "Hi?" )).build ()).execute ();
135
143
136
144
applicationLogs
137
- .assertLogEqual ("--> POST " + url + " http/1.1 (3-byte body)" )
145
+ .assertLogEqual ("--> POST " + url + " (3-byte body)" )
138
146
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms, 0-byte body\\ )" )
139
147
.assertNoMoreLogs ();
140
148
@@ -154,7 +162,7 @@ private void setLevel(Level level) {
154
162
response .body ().close ();
155
163
156
164
applicationLogs
157
- .assertLogEqual ("--> GET " + url + " http/1.1" )
165
+ .assertLogEqual ("--> GET " + url )
158
166
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms, 6-byte body\\ )" )
159
167
.assertNoMoreLogs ();
160
168
@@ -174,7 +182,7 @@ private void setLevel(Level level) {
174
182
response .body ().close ();
175
183
176
184
applicationLogs
177
- .assertLogEqual ("--> GET " + url + " http/1.1" )
185
+ .assertLogEqual ("--> GET " + url )
178
186
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms, unknown-length body\\ )" )
179
187
.assertNoMoreLogs ();
180
188
@@ -192,7 +200,7 @@ private void setLevel(Level level) {
192
200
response .body ().close ();
193
201
194
202
applicationLogs
195
- .assertLogEqual ("--> GET " + url + " http/1.1" )
203
+ .assertLogEqual ("--> GET " + url )
196
204
.assertLogEqual ("--> END GET" )
197
205
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms\\ )" )
198
206
.assertLogEqual ("Content-Length: 0" )
@@ -221,7 +229,7 @@ private void setLevel(Level level) {
221
229
response .body ().close ();
222
230
223
231
applicationLogs
224
- .assertLogEqual ("--> POST " + url + " http/1.1" )
232
+ .assertLogEqual ("--> POST " + url )
225
233
.assertLogEqual ("Content-Type: text/plain; charset=utf-8" )
226
234
.assertLogEqual ("Content-Length: 3" )
227
235
.assertLogEqual ("--> END POST" )
@@ -254,7 +262,7 @@ private void setLevel(Level level) {
254
262
response .body ().close ();
255
263
256
264
applicationLogs
257
- .assertLogEqual ("--> POST " + url + " http/1.1" )
265
+ .assertLogEqual ("--> POST " + url )
258
266
.assertLogEqual ("Content-Length: 3" )
259
267
.assertLogEqual ("--> END POST" )
260
268
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms\\ )" )
@@ -293,7 +301,7 @@ private void setLevel(Level level) {
293
301
response .body ().close ();
294
302
295
303
applicationLogs
296
- .assertLogEqual ("--> POST " + url + " http/1.1" )
304
+ .assertLogEqual ("--> POST " + url )
297
305
.assertLogEqual ("Content-Type: text/plain; charset=utf-8" )
298
306
.assertLogEqual ("--> END POST" )
299
307
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms\\ )" )
@@ -326,7 +334,7 @@ private void setLevel(Level level) {
326
334
response .body ().close ();
327
335
328
336
applicationLogs
329
- .assertLogEqual ("--> GET " + url + " http/1.1" )
337
+ .assertLogEqual ("--> GET " + url )
330
338
.assertLogEqual ("--> END GET" )
331
339
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms\\ )" )
332
340
.assertLogEqual ("Content-Length: 6" )
@@ -356,7 +364,7 @@ private void setLevel(Level level) {
356
364
response .body ().close ();
357
365
358
366
applicationLogs
359
- .assertLogEqual ("--> GET " + url + " http/1.1" )
367
+ .assertLogEqual ("--> GET " + url )
360
368
.assertLogEqual ("--> END GET" )
361
369
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms\\ )" )
362
370
.assertLogEqual ("Content-Length: 0" )
@@ -393,7 +401,7 @@ private void bodyGetNoBody(int code) throws IOException {
393
401
response .body ().close ();
394
402
395
403
applicationLogs
396
- .assertLogEqual ("--> GET " + url + " http/1.1" )
404
+ .assertLogEqual ("--> GET " + url )
397
405
.assertLogEqual ("--> END GET" )
398
406
.assertLogMatch ("<-- " + code + " No Content " + url + " \\ (\\ d+ms\\ )" )
399
407
.assertLogEqual ("Content-Length: 0" )
@@ -422,7 +430,7 @@ private void bodyGetNoBody(int code) throws IOException {
422
430
response .body ().close ();
423
431
424
432
applicationLogs
425
- .assertLogEqual ("--> POST " + url + " http/1.1" )
433
+ .assertLogEqual ("--> POST " + url )
426
434
.assertLogEqual ("Content-Type: text/plain; charset=utf-8" )
427
435
.assertLogEqual ("Content-Length: 3" )
428
436
.assertLogEqual ("" )
@@ -460,7 +468,7 @@ private void bodyGetNoBody(int code) throws IOException {
460
468
response .body ().close ();
461
469
462
470
applicationLogs
463
- .assertLogEqual ("--> GET " + url + " http/1.1" )
471
+ .assertLogEqual ("--> GET " + url )
464
472
.assertLogEqual ("--> END GET" )
465
473
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms\\ )" )
466
474
.assertLogEqual ("Content-Length: 6" )
@@ -496,7 +504,7 @@ private void bodyGetNoBody(int code) throws IOException {
496
504
response .body ().close ();
497
505
498
506
applicationLogs
499
- .assertLogEqual ("--> GET " + url + " http/1.1" )
507
+ .assertLogEqual ("--> GET " + url )
500
508
.assertLogEqual ("--> END GET" )
501
509
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms\\ )" )
502
510
.assertLogEqual ("Transfer-encoding: chunked" )
@@ -548,7 +556,7 @@ private void bodyGetNoBody(int code) throws IOException {
548
556
.assertNoMoreLogs ();
549
557
550
558
applicationLogs
551
- .assertLogEqual ("--> GET " + url + " http/1.1" )
559
+ .assertLogEqual ("--> GET " + url )
552
560
.assertLogEqual ("--> END GET" )
553
561
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms\\ )" )
554
562
.assertLogEqual ("Content-Type: text/plain; charset=utf-8" )
@@ -583,7 +591,7 @@ private void bodyGetNoBody(int code) throws IOException {
583
591
.assertNoMoreLogs ();
584
592
585
593
applicationLogs
586
- .assertLogEqual ("--> GET " + url + " http/1.1" )
594
+ .assertLogEqual ("--> GET " + url )
587
595
.assertLogEqual ("--> END GET" )
588
596
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms\\ )" )
589
597
.assertLogEqual ("Content-Type: text/html; charset=0" )
@@ -622,7 +630,7 @@ private void bodyGetNoBody(int code) throws IOException {
622
630
response .body ().close ();
623
631
624
632
applicationLogs
625
- .assertLogEqual ("--> GET " + url + " http/1.1" )
633
+ .assertLogEqual ("--> GET " + url )
626
634
.assertLogEqual ("--> END GET" )
627
635
.assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms\\ )" )
628
636
.assertLogEqual ("Content-Length: 9" )
@@ -664,11 +672,32 @@ private void bodyGetNoBody(int code) throws IOException {
664
672
}
665
673
666
674
applicationLogs
667
- .assertLogEqual ("--> GET " + url + " http/1.1" )
675
+ .assertLogEqual ("--> GET " + url )
668
676
.assertLogEqual ("<-- HTTP FAILED: java.net.UnknownHostException: reason" )
669
677
.assertNoMoreLogs ();
670
678
}
671
679
680
+ @ Test public void http2 () throws Exception {
681
+ server .useHttps (sslClient .socketFactory , false );
682
+ url = server .url ("/" );
683
+
684
+ setLevel (Level .BASIC );
685
+
686
+ server .enqueue (new MockResponse ());
687
+ Response response = client .newCall (request ().build ()).execute ();
688
+ assertEquals (Protocol .HTTP_2 , response .protocol ());
689
+
690
+ applicationLogs
691
+ .assertLogEqual ("--> GET " + url )
692
+ .assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms, 0-byte body\\ )" )
693
+ .assertNoMoreLogs ();
694
+
695
+ networkLogs
696
+ .assertLogEqual ("--> GET " + url + " h2" )
697
+ .assertLogMatch ("<-- 200 OK " + url + " \\ (\\ d+ms, 0-byte body\\ )" )
698
+ .assertNoMoreLogs ();
699
+ }
700
+
672
701
private Request .Builder request () {
673
702
return new Request .Builder ().url (url );
674
703
}
0 commit comments