File size: 52,845 Bytes
7b715bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
/*
Copyright 2013 Google Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using Google.Apis.Http;
using Google.Apis.Logging;
using Google.Apis.Tests.Mocks;
using Google.Apis.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Google.Apis.Tests.Apis.Http
{
    /// <summary>Tests for <see cref="Google.Apis.Http.ConfigurableMessageHandler"/>.</summary>
    public class ConfigurableMessageHandlerTest
    {
        #region Handlers

        /// <summary>Unsuccessful handler which always returns <c>true</c>.</summary>
        private class TrueUnsuccessfulResponseHandler : IHttpUnsuccessfulResponseHandler
        {
            public Task<bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
            {
                TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
                tcs.SetResult(true);
                return tcs.Task;
            }
        }

        /// <summary>Message handler which returns a new successful (and empty) response.</summary>
        private class MockMessageHandler : HttpMessageHandler
        {
            public HttpResponseMessage Response { get; set; } = new HttpResponseMessage();

            protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
                CancellationToken cancellationToken)
            {
                TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompletionSource<HttpResponseMessage>();
                tcs.SetResult(Response);
                return tcs.Task;
            }
        }

        #endregion

        #region Redirect

        /// <summary>Redirect message handler which return redirect response.</summary>
        private class RedirectMessageHandler : CountableMessageHandler
        {
            /// <summary>Gets or sets the redirect location Uri string.</summary>
            private string Location { get; set; }

            /// <summary>Constructs a new redirect message handler with the given location.</summary>
            public RedirectMessageHandler(string location)
            {
                Location = location;
            }

            protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsyncCore(
                HttpRequestMessage request, CancellationToken cancellationToken)
            {
                TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompletionSource<HttpResponseMessage>();
                var response = new HttpResponseMessage();

                response.StatusCode = HttpStatusCode.Redirect;
                response.Headers.Location = new Uri(Location + Calls);
                response.RequestMessage = request;

                if (Calls == 1)
                {
                    // First call the message should contain If-* headers
                    Assert.Equal(new Uri(Location), request.RequestUri);
                    Assert.Single(request.Headers.IfMatch);
                    Assert.Single(request.Headers.IfNoneMatch);
                    Assert.True(request.Headers.IfModifiedSince.HasValue);
                    Assert.True(request.Headers.IfUnmodifiedSince.HasValue);
                }
                else
                {
                    // After first call the message should not contain If-* headers
                    Assert.Equal(new Uri(Location + (Calls - 1)), request.RequestUri);
                    Assert.Empty(request.Headers.IfMatch);
                    Assert.Empty(request.Headers.IfNoneMatch);
                    Assert.Null(request.Headers.IfModifiedSince);
                    Assert.Null(request.Headers.IfUnmodifiedSince);
                }

                tcs.SetResult(response);
                return tcs.Task;
            }
        }

        /// <summary>Tests that the message handler handles redirect messages successfully.</summary>
        [Fact]
        public async Task SendAsync_Redirect()
        {
            var location = "https://google.com";
            var redirectHandler = new RedirectMessageHandler(location);
            var configurableHandler = new ConfigurableMessageHandler(redirectHandler)
            {
                NumRedirects = 5
            };

            using (var client = new HttpClient(configurableHandler))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, location);
                request.Headers.IfModifiedSince = DateTimeOffset.UtcNow;
                request.Headers.IfUnmodifiedSince = DateTimeOffset.UtcNow;
                request.Headers.IfMatch.Add(new EntityTagHeaderValue("\"a\""));
                request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue("\"b\""));

                HttpResponseMessage response = await client.SendAsync(request);

                Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
                Assert.Equal(new Uri(location + (configurableHandler.NumRedirects + 1)), response.Headers.Location);
                Assert.Equal(configurableHandler.NumRedirects + 1, redirectHandler.Calls);
            }
        }

        /// <summary>
        /// Tests that the message handler doesn't handle redirect messages when follow redirect is <c>false</c>. 
        /// </summary>
        [Fact]
        public async Task SendAsync_Redirect_FollowRedirectFalse()
        {
            const int tries = 12;
            var location = "https://google.com";
            var redirectHandler = new RedirectMessageHandler(location);
            var configurableHandler = new ConfigurableMessageHandler(redirectHandler)
            {
                NumRedirects = tries,
                FollowRedirect = false
            };

            using (var client = new HttpClient(configurableHandler))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, location);
                request.Headers.IfModifiedSince = DateTimeOffset.UtcNow;
                request.Headers.IfUnmodifiedSince = DateTimeOffset.UtcNow;
                request.Headers.IfMatch.Add(new EntityTagHeaderValue("\"a\""));
                request.Headers.IfNoneMatch.Add(new EntityTagHeaderValue("\"b\""));

                HttpResponseMessage response = await client.SendAsync(request);

                // there should be only one request because follow redirect is false
                Assert.Equal(HttpStatusCode.Redirect, response.StatusCode);
                Assert.Equal(new Uri(location + 1), response.Headers.Location);
                Assert.Equal(1, redirectHandler.Calls);
            }
        }

        #endregion

        #region Execute interceptor

        /// <summary>
        /// Mock interceptor handler which verifies that an interceptor is being called on a request. 
        /// </summary>
        private class InterceptorMessageHandler : CountableMessageHandler
        {
            /// <summary>Gets or sets an injected response message which will be returned on send.</summary>
            public HttpResponseMessage InjectedResponseMessage { get; set; }

            const string InjectedHeader = "Some-Header";
            const string InjectedValue = "123";

            protected override Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request,
                CancellationToken cancellationToken)
            {
                Assert.Equal(InjectedValue, request.Headers.GetValues(InjectedHeader).First());

                TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompletionSource<HttpResponseMessage>();
                tcs.SetResult(InjectedResponseMessage);
                return tcs.Task;
            }

            /// <summary>A mock interceptor which inject a header to a request.</summary>
            internal class Interceptor : IHttpExecuteInterceptor
            {
                public int Calls { get; set; }

                public Task InterceptAsync(HttpRequestMessage request, CancellationToken token)
                {
                    ++Calls;
                    request.Headers.Add(InjectedHeader, InjectedValue);
                    return Task.Delay(0);
                }
            }
        }

        [Theory]
        [InlineData(HttpStatusCode.OK)]
        [InlineData(HttpStatusCode.BadRequest)]
        public async Task SendAsync_ExecuteInterceptor(HttpStatusCode code)
        {
            var handler = new InterceptorMessageHandler();
            handler.InjectedResponseMessage = new HttpResponseMessage
            {
                StatusCode = code
            };

            var configurableHandler = new ConfigurableMessageHandler(handler);
            var interceptor = new InterceptorMessageHandler.Interceptor();
            configurableHandler.AddExecuteInterceptor(interceptor);

            using (var client = new HttpClient(configurableHandler))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-execute-interceptor");

                HttpResponseMessage response = await client.SendAsync(request);
                Assert.Equal(1, interceptor.Calls);
                Assert.Equal(1, handler.Calls);
            }
        }

        /// <summary>
        /// Tests that execute interceptor is called for each request. In this case an unsuccessful response handler is 
        /// plugged to the handler
        /// </summary>
        [Fact]
        public async Task SendAsync_ExecuteInterceptor_AbnormalResponse_UnsuccessfulResponseHandler()
        {
            var handler = new InterceptorMessageHandler();
            handler.InjectedResponseMessage = new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.ServiceUnavailable
            };

            var configurableHandler = new ConfigurableMessageHandler(handler);
            var interceptor = new InterceptorMessageHandler.Interceptor();
            configurableHandler.AddExecuteInterceptor(interceptor);
            configurableHandler.AddUnsuccessfulResponseHandler(new TrueUnsuccessfulResponseHandler());

            using (var client = new HttpClient(configurableHandler))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-execute-interceptor");

                HttpResponseMessage response = await client.SendAsync(request);
                Assert.Equal(configurableHandler.NumTries, interceptor.Calls);
                Assert.Equal(configurableHandler.NumTries, handler.Calls);
            }
        }

        [Fact]
        public async Task SendAsync_ExecuteInterceptor_AbnormalResponse_UnsuccessfulResponseHandler_PerRequestMaxRetries()
        {
            var handler = new InterceptorMessageHandler();
            handler.InjectedResponseMessage = new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.ServiceUnavailable
            };

            var configurableHandler = new ConfigurableMessageHandler(handler);
            var interceptor = new InterceptorMessageHandler.Interceptor();
            configurableHandler.AddExecuteInterceptor(interceptor);
            configurableHandler.AddUnsuccessfulResponseHandler(new TrueUnsuccessfulResponseHandler());
            // Let's have this request retry for a little longer than default.
            int perRequestRetries = configurableHandler.NumTries + 2;

            using (var client = new HttpClient(configurableHandler))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-execute-interceptor");
                var properties = TestHelpers.GetRequestProperties(request);
                properties.Add(ConfigurableMessageHandler.MaxRetriesKey, perRequestRetries);

                HttpResponseMessage response = await client.SendAsync(request);
                Assert.Equal(perRequestRetries, interceptor.Calls);
                Assert.Equal(perRequestRetries, handler.Calls);
            }
        }

        #endregion

        #region Unsuccessful reponse handler

        /// <summary>
        /// Mock unsuccessful response handler which verifies that unsuccessful response handler is being called.
        /// </summary>
        private class UnsuccessfulResponseMessageHandler : CountableMessageHandler
        {
            /// <summary>Gets or sets the status code to return on the response.</summary>
            public HttpStatusCode ResponseStatusCode { get; set; }

            /// <summary>Gets or sets the cancellation token source.</summary>
            public CancellationTokenSource CancellationTokenSource { get; set; }

            /// <summary>
            /// Gets or sets the request number to invoke the Cancel method on <see cref="CancellationTokenSource"/>.
            /// </summary>
            public int CancelRequestNum { get; set; }

            protected override Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request,
                CancellationToken cancellationToken)
            {
                if (Calls == CancelRequestNum)
                {
                    CancellationTokenSource.Cancel();
                }

                TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompletionSource<HttpResponseMessage>();
                tcs.SetResult(new HttpResponseMessage { StatusCode = ResponseStatusCode });
                return tcs.Task;
            }

            /// <summary>Unsuccessful response handler which "handles" only service unavailable responses.</summary>
            internal class ServiceUnavailableResponseHandler : IHttpUnsuccessfulResponseHandler
            {
                public int Calls { get; set; }

                public Task<bool> HandleResponseAsync(HandleUnsuccessfulResponseArgs args)
                {
                    ++Calls;
                    TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
                    tcs.SetResult(args.Response.StatusCode.Equals(HttpStatusCode.ServiceUnavailable));
                    return tcs.Task;
                }
            }
        }

        [Theory]
        [CombinatorialData]
        public async Task SendAsyncUnsuccessfulReponseHandler(
            [CombinatorialValues(HttpStatusCode.OK, HttpStatusCode.BadGateway, HttpStatusCode.ServiceUnavailable)] HttpStatusCode code,
            [CombinatorialValues(null, 5)] int? maxRetries)
        {
            var handler = new UnsuccessfulResponseMessageHandler { ResponseStatusCode = code };

            var configurableHandler = new ConfigurableMessageHandler(handler);
            var unsuccessfulHandler = new UnsuccessfulResponseMessageHandler.ServiceUnavailableResponseHandler();
            configurableHandler.AddUnsuccessfulResponseHandler(unsuccessfulHandler);

            using (var client = new HttpClient(configurableHandler))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-unsuccessful-handler");
                int expectedMaxRetries = MaybeSetMaxRetries(maxRetries, configurableHandler.NumTries, request);

                HttpResponseMessage response = await client.SendAsync(request);
                Assert.Equal(code, response.StatusCode);

                // if service unavailable, retry will occur because we plugged unsuccessful response handler which 
                // handles service unavailable responses
                if (code == HttpStatusCode.ServiceUnavailable)
                {
                    Assert.Equal(expectedMaxRetries, unsuccessfulHandler.Calls);
                    Assert.Equal(expectedMaxRetries, handler.Calls);
                }
                else
                {
                    // if status is OK, there isn't any call to unsuccessful response handler
                    Assert.Equal(code != HttpStatusCode.OK ? 1 : 0, unsuccessfulHandler.Calls);
                    Assert.Equal(1, handler.Calls);
                }
            }
        }

        /// <summary>Tests abnormal response when unsuccessful response handler isn't plugged.</summary>
        [Fact]
        public async Task SendAsync_AbnormalResponse_WithoutUnsuccessfulReponseHandler()
        {
            var handler = new UnsuccessfulResponseMessageHandler
                {
                    ResponseStatusCode = HttpStatusCode.ServiceUnavailable
                };

            var configurableHandler = new ConfigurableMessageHandler(handler);
            using (var client = new HttpClient(configurableHandler))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-unsuccessful-handler");

                HttpResponseMessage response = await client.SendAsync(request);
                Assert.Equal(HttpStatusCode.ServiceUnavailable, response.StatusCode);
                Assert.Equal(1, handler.Calls);
            }
        }

        #endregion

        #region Exception Handler

        /// <summary>Mock exception message handler which verifies that exception handler is being called.</summary>
        private class ExceptionMessageHandler : CountableMessageHandler
        {
            public ExceptionMessageHandler()
            {
                Exception = new Exception(ExceptionMessage);
            }

            /// <summary>Gets or sets indication if exception should be thrown.</summary>
            public bool ThrowException { get; set; }

            /// <summary>
            /// Gets or sets a specific exception to throw. Default value is <seealso cref="System.Exception"/> 
            /// with <see cref="ExceptionMessage"/>.</summary>
            public Exception Exception { get; set; }

            /// <summary>
            /// The exception message which is thrown in case <see cref="ThrowException"/> is <c>true</c>. 
            /// </summary>
            public const string ExceptionMessage = "Exception from execute";

            protected override Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request,
                CancellationToken cancellationToken)
            {
                if (ThrowException)
                {
                    throw Exception;
                }

                TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompletionSource<HttpResponseMessage>();
                tcs.SetResult(new HttpResponseMessage());
                return tcs.Task;
            }

            /// <summary>Mock Exception handler which "handles" the exception.</summary>
            internal class ExceptionHandler : IHttpExceptionHandler
            {
                public int Calls { get; set; }
                public bool Handle { get; set; }

                public ExceptionHandler(bool handle = true)
                {
                    Handle = handle;
                }

                public Task<bool> HandleExceptionAsync(HandleExceptionArgs args)
                {
                    ++Calls;
                    TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
                    tcs.SetResult(Handle);
                    return tcs.Task;
                }
            }
        }

        [Theory]
        [CombinatorialData]
        public async Task SendAsyncExceptionHandler(bool throwException, bool handle,
            [CombinatorialValues(null, 5)] int? maxRetries)
        {
            var handler = new ExceptionMessageHandler { ThrowException = throwException };

            var configurableHandler = new ConfigurableMessageHandler(handler);
            var exceptionHandler = new ExceptionMessageHandler.ExceptionHandler { Handle = handle };
            configurableHandler.AddExceptionHandler(exceptionHandler);

            using (var client = new HttpClient(configurableHandler))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-exception-handler");
                int expectedMaxRetries = MaybeSetMaxRetries(maxRetries, configurableHandler.NumTries, request);

                try
                {
                    // TODO: Separate these out into throw / not-throw tests
                    HttpResponseMessage response = await client.SendAsync(request);
                    if (throwException)
                    {
                        Assert.Fail("SendAsync should throw an exception");
                    }
                }
                catch (Exception exception)
                {
                    Assert.Equal(ExceptionMessageHandler.ExceptionMessage, exception.Message);
                }

                // if exception is thrown, check if it's handled.
                // If so, there should be expectedMaxRetries calls, otherwise only 1.
                if (throwException)
                {
                    Assert.Equal(handle ? expectedMaxRetries : 1, exceptionHandler.Calls);
                }
                // Exception wasn't supposed to be thrown, so no call to exception handler should be made.
                else
                {
                    Assert.Equal(0, exceptionHandler.Calls);
                }

                Assert.Equal(throwException & handle ? expectedMaxRetries : 1, handler.Calls);
            }
        }

        /// <summary>Tests an exception is thrown on execute and there is no exception handler.</summary>
        [Fact]
        public async Task SendAsync_ThrowException_WithoutExceptionHandler()
        {
            var handler = new ExceptionMessageHandler { ThrowException = true };

            var configurableHandler = new ConfigurableMessageHandler(handler);

            using var client = new HttpClient(configurableHandler);
            var request = new HttpRequestMessage(HttpMethod.Get, "https://test-exception-handler");
            var exception = await Assert.ThrowsAsync<Exception>(() => client.SendAsync(request));
            Assert.Same(ExceptionMessageHandler.ExceptionMessage, exception.Message);
            Assert.Equal(1, handler.Calls);
        }

        #endregion

        #region Back-off

        #region Exception

        /// <summary>
        /// Tests that back-off handler works as expected when exception is thrown. 
        /// Use default max time span (2 minutes).
        /// </summary>
        [Theory]
        [InlineData(null)]
        [InlineData(5)]
        public async Task SendAsync_BackOffExceptionHandler_Throw_Max2Minutes(int? maxRetries)
        {
            // create exponential back-off without delta interval, so expected seconds are exactly 1, 2, 4, 8, etc.
            var initializer = new BackOffHandler.Initializer(new ExponentialBackOff(TimeSpan.Zero));
            await SubtestSendAsync_BackOffExceptionHandler(true, initializer, maxRetries: maxRetries);
        }

        /// <summary>
        /// Tests that back-off handler works as expected when exception is thrown. 
        /// Max time span is set to 200 milliseconds (as a result the back-off handler can't handle the exception).
        /// </summary>
        [Theory]
        [InlineData(null)]
        [InlineData(5)]
        public async Task SendAsync_BackOffExceptionHandler_Throw_Max200Milliseconds(int? maxRetries)
        {
            var initializer = new BackOffHandler.Initializer(new ExponentialBackOff(TimeSpan.Zero))
                {
                    MaxTimeSpan = TimeSpan.FromMilliseconds(200)
                };
            await SubtestSendAsync_BackOffExceptionHandler(true, initializer, maxRetries: maxRetries);
        }

        /// <summary>
        /// Tests that back-off handler works as expected when exception is thrown. 
        /// Max time span is set to 1 hour.
        /// </summary>
        [Theory]
        [InlineData(null)]
        [InlineData(5)]
        public async Task SendAsync_BackOffExceptionHandler_Throw_Max1Hour(int? maxRetries)
        {
            var initializer = new BackOffHandler.Initializer(new ExponentialBackOff(TimeSpan.Zero))
                {
                    MaxTimeSpan = TimeSpan.FromHours(1)
                };
            await SubtestSendAsync_BackOffExceptionHandler(true, initializer,maxRetries: maxRetries);
        }

        /// <summary>
        /// Tests that back-off handler works as expected when 
        /// <seealso cref="System.Threading.Tasks.TaskCanceledException"/>> is thrown. 
        /// </summary>
        [Theory]
        [InlineData(null)]
        [InlineData(5)]
        public async Task SendAsync_BackOffExceptionHandler_ThrowCanceledException(int? maxRetries)
        {
            var initializer = new BackOffHandler.Initializer(new ExponentialBackOff(TimeSpan.Zero));
            await SubtestSendAsync_BackOffExceptionHandler(true, initializer, new TaskCanceledException(), maxRetries);
        }

        /// <summary>
        /// Tests that back-off handler works as expected with the not defaulted exception handler. 
        /// </summary>
        [Theory]
        [InlineData(null)]
        [InlineData(5)]
        public async Task SendAsync_BackOffExceptionHandler_DifferentHandler(int? maxRetries)
        {
            var initializer = new BackOffHandler.Initializer(new ExponentialBackOff(TimeSpan.Zero));
            initializer.HandleExceptionFunc = e => (e is InvalidCastException);
            await SubtestSendAsync_BackOffExceptionHandler(true, initializer, new InvalidCastException(), maxRetries);

            initializer.HandleExceptionFunc = e => !(e is InvalidCastException);
            await SubtestSendAsync_BackOffExceptionHandler(true, initializer, new InvalidCastException(), maxRetries);
        }

        /// <summary>Tests that back-off handler works as expected when exception isn't thrown.</summary>
        [Theory]
        [InlineData(null)]
        [InlineData(5)]
        public async Task SendAsync_BackOffExceptionHandler_DontThrow(int? maxRetries)
        {
            var initializer = new BackOffHandler.Initializer(new ExponentialBackOff(TimeSpan.Zero));
            await SubtestSendAsync_BackOffExceptionHandler(false, initializer, maxRetries: maxRetries);
        }

        private async Task SubtestSendAsync_BackOffExceptionHandler(bool throwException, BackOffHandler.Initializer initializer, Exception exceptionToThrow = null, int? maxRetries = null)
        {
            var handler = new ExceptionMessageHandler { ThrowException = throwException };
            if (exceptionToThrow != null)
            {
                handler.Exception = exceptionToThrow;
            }

            var configurableHandler = new ConfigurableMessageHandler(handler);
            var boHandler = new MockBackOffHandler(initializer);
            configurableHandler.AddExceptionHandler(boHandler);

            var request = new HttpRequestMessage(HttpMethod.Get, "https://test-exception-handler");
            int expectedMaxRetries = MaybeSetMaxRetries(maxRetries, configurableHandler.NumTries, request);

            int boHandleCount = 0;
            // if an exception should be thrown and the handler can handle it then calculate the handle count by the 
            // lg(MaxTimeSpan)
            if (throwException && initializer.HandleExceptionFunc(exceptionToThrow))
            {
                boHandleCount = Math.Min((int)Math.Floor(Math.Log(boHandler.MaxTimeSpan.TotalSeconds, 2)) + 1,
                    expectedMaxRetries - 1);
                boHandleCount = boHandleCount >= 0 ? boHandleCount : 0;
            }

            using (var client = new HttpClient(configurableHandler))
            {
                try
                {
                    // TODO: Separate out exception/non-exception cases
                    HttpResponseMessage response = await client.SendAsync(request);
                    Assert.False(throwException);
                }
                catch (Exception exception)
                {
                    Assert.True(throwException);
                    // Don't compare exception messages because for TaskCanceledException the message
                    // on Mono is different if there is a task associated with the exception.
                    Assert.Equal(handler.Exception.GetType(), exception.GetType());
                }

                Assert.Equal(boHandleCount, boHandler.Waits.Count);
                // check the exponential behavior - wait 1, 2, 4, 8, ... seconds.
                if (throwException)
                {
                    for (int i = 0; i < boHandler.Waits.Count; ++i)
                    {
                        Assert.Equal((int)Math.Pow(2, i), boHandler.Waits[i].TotalSeconds);
                    }
                }
                Assert.Equal(boHandleCount + 1, handler.Calls);
            }
        }

        #endregion

        #region Unsuccessful Response Handler

        /// <summary>
        /// Tests that back-off handler works as expected when the server returns 5xx and the maximum time span is set
        /// to 5 seconds.
        /// </summary>
        [Fact]
        public async Task SendAsync_BackOffUnsuccessfulResponseHandler_ServiceUnavailable_Max5Seconds()
        {
            var initializer = new BackOffHandler.Initializer(new ExponentialBackOff(TimeSpan.Zero))
                {
                    MaxTimeSpan = TimeSpan.FromSeconds(5)
                };
            await SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.ServiceUnavailable, initializer);
        }

        /// <summary>
        /// Tests that back-off handler works as expected when the server returns 5xx and the maximum time span is set
        /// to 10 hours.
        /// </summary>
        [Fact]
        public async Task SendAsync_BackOffUnsuccessfulResponseHandler_ServiceUnavailable_Max10Hours()
        {
            var initializer = new BackOffHandler.Initializer(new ExponentialBackOff(TimeSpan.Zero))
                {
                    MaxTimeSpan = TimeSpan.FromHours(10)
                };
            await SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.ServiceUnavailable, initializer);
        }

        /// <summary>
        /// Tests that back-off handler isn't be called when the server returns a successful response.
        /// </summary>
        [Fact]
        public async Task SendAsync_BackOffUnsuccessfulResponseHandler_OK()
        {
            var initializer = new BackOffHandler.Initializer(new ExponentialBackOff(TimeSpan.Zero));
            await SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.OK, initializer);
        }

        /// <summary>Tests that back-off handler is canceled when cancellation token is used.</summary>
        [Fact]
        public async Task SendAsync_BackOffUnsuccessfulResponseHandler_Cancel()
        {
            // Test back-off with maximum 30 minutes per single request
            var initializer = new BackOffHandler.Initializer(new ExponentialBackOff(TimeSpan.Zero))
            {
                MaxTimeSpan = TimeSpan.FromMinutes(30)
            };
            await SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.ServiceUnavailable, initializer, 2);
            await SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode.ServiceUnavailable, initializer, 6);
        }

        /// <summary>
        /// Subtest that back-off handler works as expected when a successful or abnormal response is returned.
        /// For testing the back-off handler in case of a canceled request, set the <code>cancelRequestNum</code>
        /// parameter to the index of the request you want to cancel.
        /// </summary>
        private async Task SubtestSendAsync_BackOffUnsuccessfulResponseHandler(HttpStatusCode statusCode,
            BackOffHandler.Initializer initializer, int cancelRequestNum = 0, int numTries = 10)
        {
            var handler = new UnsuccessfulResponseMessageHandler { ResponseStatusCode = statusCode };

            CancellationToken cancellationToken = CancellationToken.None;
            bool cancel = cancelRequestNum > 0;

            if (cancel)
            {
                CancellationTokenSource tcs = new CancellationTokenSource();
                handler.CancellationTokenSource = tcs;
                handler.CancelRequestNum = cancelRequestNum;
                cancellationToken = tcs.Token;
            }

            var configurableHandler = new ConfigurableMessageHandler(handler)
            {
                NumTries = numTries
            };
            var boHandler = new MockBackOffHandler(initializer);
            configurableHandler.AddUnsuccessfulResponseHandler(boHandler);

            int boHandleCount = 0;
            if (initializer.HandleUnsuccessfulResponseFunc != null &&
                initializer.HandleUnsuccessfulResponseFunc(new HttpResponseMessage { StatusCode = statusCode }))
            {
                boHandleCount = Math.Min((int)Math.Floor(Math.Log(boHandler.MaxTimeSpan.TotalSeconds, 2)) + 1,
                    configurableHandler.NumTries - 1);
                boHandleCount = boHandleCount >= 0 ? boHandleCount : 0;
                if (cancel)
                {
                    boHandleCount = Math.Min(boHandleCount, cancelRequestNum);
                }
            }

            using (var client = new HttpClient(configurableHandler))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-exception-handler");
                try
                {
                    // TODO: Split out failure and non-failure cases.
                    HttpResponseMessage response = await client.SendAsync(request, cancellationToken);
                    Assert.False(cancel);
                }
                catch (OperationCanceledException)
                {
                    // A canceled request should throw an exception
                    Assert.True(cancel);
                }

                Assert.Equal(boHandleCount, boHandler.Waits.Count);

                // check the exponential behavior - wait 1, 2, 4, 8, ... seconds.
                for (int i = 0; i < boHandler.Waits.Count; ++i)
                {
                    Assert.Equal((int)Math.Pow(2, i), boHandler.Waits[i].TotalSeconds);
                }

                // if the request was canceled the number of calls to the message handler is equal to the number of 
                // calls to back-off handler
                Assert.Equal(boHandleCount + (cancel ? 0 : 1), handler.Calls);
            }
        }

        #endregion

        #endregion

        #region Content

        /// <summary>Mock message handler which verifies that the content is correct on retry.</summary>
        private class ContentMessageHandler : CountableMessageHandler
        {
            public const int NumFails = 4;
            public string ReadContent;

            protected override async Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request,
                CancellationToken cancellationToken)
            {
                if (Calls < NumFails)
                {
                    return new HttpResponseMessage() { StatusCode = HttpStatusCode.ServiceUnavailable };
                }

                ReadContent = await request.Content.ReadAsStringAsync().ConfigureAwait(false);
                return new HttpResponseMessage();
            }
        }

        /// <summary>
        /// Defines the different content types we test in <see cref="SubtestSendAsyncRetryContent"/>.
        /// </summary>
        public enum ContentType
        {
            String,
            Stream,
            ByteArray
        }

        /// <summary>Tests that retry works with different kind of contents (String, Stream and ByteArray).</summary>
        [Theory]
        [InlineData(ContentType.String)]
        [InlineData(ContentType.Stream)]
        [InlineData(ContentType.ByteArray)]
        public async Task SubtestSendAsyncRetryContent(ContentType type)
        {
            var content = "test-content";
            var contentHandler = new ContentMessageHandler();
            var configurableHandler = new ConfigurableMessageHandler(contentHandler)
            {
                NumTries = 10
            };
            configurableHandler.AddUnsuccessfulResponseHandler(new TrueUnsuccessfulResponseHandler());
            using (var client = new HttpClient(configurableHandler))
            {
                var request = new HttpRequestMessage(HttpMethod.Put, "https://test-unsuccessful-handler");
                // set the right content
                switch (type)
                {
                    case ContentType.String:
                        request.Content = new StringContent(content);
                        break;
                    case ContentType.Stream:
                        {
                            var stream = new MemoryStream();
                            var buffer = Encoding.UTF8.GetBytes(content);
                            stream.Write(buffer, 0, buffer.Length);
                            stream.Position = 0;
                            request.Content = new StreamContent(stream);
                        }
                        break;
                    case ContentType.ByteArray:
                        request.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(content));
                        break;
                }

                HttpResponseMessage response = await client.SendAsync(request);
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal(ContentMessageHandler.NumFails, contentHandler.Calls);
                Assert.Equal(content, contentHandler.ReadContent);
            }
        }
        #endregion

        /// <summary>Tests setting number of tries.</summary>
        [Fact]
        public void NumTries_Setter()
        {
            var configurableHandler = new ConfigurableMessageHandler(new HttpClientHandler());

            // Valid values
            configurableHandler.NumTries = ConfigurableMessageHandler.MaxAllowedNumTries;
            configurableHandler.NumTries = ConfigurableMessageHandler.MaxAllowedNumTries - 1;
            configurableHandler.NumTries = 1;

            // Test invalid values
            var exception = Assert.Throws<ArgumentOutOfRangeException>(() => configurableHandler.NumTries = ConfigurableMessageHandler.MaxAllowedNumTries + 1);
            Assert.Equal(nameof(configurableHandler.NumTries), exception.ParamName);

            exception = Assert.Throws<ArgumentOutOfRangeException>(() => configurableHandler.NumTries = 0);
            Assert.Equal(nameof(configurableHandler.NumTries), exception.ParamName);

            exception = Assert.Throws<ArgumentOutOfRangeException>(() => configurableHandler.NumTries = -2);
            Assert.Equal(nameof(configurableHandler.NumTries), exception.ParamName);
        }

        /// <summary>
        /// Tests the number of tries in case of unsuccessful response when unsuccessful response handler is plugged to 
        /// the message handler. 
        /// </summary>
        [Fact]
        public void SendAsync_NumTries()
        {
            SubtestSendAsyncNumTries(5, false);
            SubtestSendAsyncNumTries(5);
            SubtestSendAsyncNumTries(1);
            SubtestSendAsyncNumTries(1, false);
            SubtestSendAsyncNumTries(10);
            SubtestSendAsyncNumTries(10, false);
        }

        /// <summary>
        /// Tests the retry mechanism. In case the abnormal response is handled, there should be retries, but otherwise
        /// there should not be any retry.
        /// </summary>
        /// <param name="numTries"></param>
        /// <param name="handle"></param>
        private void SubtestSendAsyncNumTries(int numTries, bool handle = true)
        {
            var handler = new UnsuccessfulResponseMessageHandler
                {
                    ResponseStatusCode = HttpStatusCode.ServiceUnavailable
                };
            var configurableHandler = new ConfigurableMessageHandler(handler)
                {
                    NumTries = numTries
                };
            if (handle)
            {
                var unsuccessfulHandler = new UnsuccessfulResponseMessageHandler.ServiceUnavailableResponseHandler();
                configurableHandler.AddUnsuccessfulResponseHandler(unsuccessfulHandler);
            }

            using (var client = new HttpClient(configurableHandler))
            {
                client.GetAsync("http://num-retries");
                Assert.Equal(handle ? numTries : 1, handler.Calls);
            }
        }

        /// <summary>Tests that the configurable message handler sets the User-Agent header.</summary>
        [Fact]
        public async Task SendAsync_UserAgent()
        {
            var apiVersion = string.Format("google-api-dotnet-client/{0} (gzip)", Utilities.GetLibraryVersion());
            const string applicationName = "NO NAME";

            var handler = new MockMessageHandler();
            var configurableHandler = new ConfigurableMessageHandler(handler);

            using (var client = new HttpClient(configurableHandler))
            {
                // without application name
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-user-agent");
                HttpResponseMessage response = await client.SendAsync(request);
                var userAgent = string.Join(" ", request.Headers.GetValues("User-Agent").ToArray());
                Assert.Equal(apiVersion, userAgent);

                // with application name
                configurableHandler.ApplicationName = applicationName;
                request = new HttpRequestMessage(HttpMethod.Get, "https://test-user-agent");
                response = await client.SendAsync(request);
                userAgent = string.Join(" ", request.Headers.GetValues("User-Agent").ToArray());
                Assert.Equal(applicationName + " " + apiVersion, userAgent);
            }
        }

        [Fact]
        public async Task SendAsync_GoogleApiClientHeader_Unset()
        {
            var handler = new MockMessageHandler();
            var configurableHandler = new ConfigurableMessageHandler(handler);

            using (var client = new HttpClient(configurableHandler))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-request");
                HttpResponseMessage response = await client.SendAsync(request);
                Assert.False(request.Headers.Contains("x-goog-api-client"));
            }
        }

        [Fact]
        public async Task SendAsync_GoogleApiClientHeader_Set()
        {
            var handler = new MockMessageHandler();
            var configurableHandler = new ConfigurableMessageHandler(handler)
            {
                GoogleApiClientHeader = "test/1.2.3"
            };

            using (var client = new HttpClient(configurableHandler))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-request");
                HttpResponseMessage response = await client.SendAsync(request);
                string header = string.Join(" ", request.Headers.GetValues("x-goog-api-client").ToArray());
                Assert.Equal("test/1.2.3", header);
            }
        }

        private async Task<IList<string>> LogTest(ConfigurableMessageHandler.LogEventType logEvents, bool errorResponse = false)
        {
            var clock = new MockClock(new DateTime(2017, 1, 2, 3, 4, 5, DateTimeKind.Utc));
            var logger = new MemoryLogger(LogLevel.All, clock: clock);
            HttpMessageHandler handler;
            if (errorResponse)
            {
                handler = new MockMessageHandler
                {
                    Response = new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.ServiceUnavailable
                    }
                };
            }
            else
            {
                handler = new MockMessageHandler
                {
                    Response = new HttpResponseMessage
                    {
                        Headers = { { "header1", "One" }, { "header2", "Two" } },
                        Content = new ByteArrayContent(new byte[] { 65, 66, 67, 0, 1, 255, 68, 69 }) // ABC...DE
                    }
                };
            }
            var configurableHandler = new ConfigurableMessageHandler(handler)
            {
                LogEvents = logEvents,
                InstanceLogger = logger
            };
            using (var client = new HttpClient(configurableHandler))
            {
                var request = new HttpRequestMessage(HttpMethod.Get, "https://test-host/")
                {
                    Headers = { Host = "test-host", Referrer = new Uri("https://referrer/") },
                    Content = new ByteArrayContent(new byte[] { 88, 89, 90, 0, 1, 255, 68, 69 }) // XYZ...DE
                };
                await client.SendAsync(request).ConfigureAwait(false);
            }
            return logger.LogEntries;
        }

        [Fact]
        public async Task Logging_RequestUri()
        {
            var logEntries = await LogTest(ConfigurableMessageHandler.LogEventType.RequestUri);
            Assert.Single(logEntries, "D2017-01-02 03:04:05.000000 Google.Apis.Http.ConfigurableMessageHandler Request[00000001] (triesRemaining=3) URI: 'https://test-host/'");
        }

        [Fact]
        public async Task Logging_RequestHeaders()
        {
            var logEntries = await LogTest(ConfigurableMessageHandler.LogEventType.RequestHeaders);
            var entry = Assert.Single(logEntries);
            // Header order may vary, and extra headers may be present (e.g. UserAgent), so test using Contain()
            Assert.Contains("D2017-01-02 03:04:05.000000 Google.Apis.Http.ConfigurableMessageHandler Request[00000001] Headers:\n", entry);
            Assert.Contains("  [Host] 'test-host'", entry);
            Assert.Contains("  [Referer] 'https://referrer/'", entry);
        }

        [Fact]
        public async Task Logging_RequestBody()
        {
            var logEntries = await LogTest(ConfigurableMessageHandler.LogEventType.RequestBody);
            Assert.Single(logEntries, "D2017-01-02 03:04:05.000000 Google.Apis.Http.ConfigurableMessageHandler Request[00000001] Body: 'XYZ...DE'");
        }

        [Fact]
        public async Task Logging_ResponseStatus()
        {
            var logEntries = await LogTest(ConfigurableMessageHandler.LogEventType.ResponseStatus);
            Assert.Single(logEntries, "D2017-01-02 03:04:05.000000 Google.Apis.Http.ConfigurableMessageHandler Response[00000001] Response status: OK 'OK'");
        }

        [Fact]
        public async Task Logging_ResponseError()
        {
            var logEntries = await LogTest(ConfigurableMessageHandler.LogEventType.ResponseAbnormal, true);
            Assert.Equal(2, logEntries.Count);
            Assert.Equal("D2017-01-02 03:04:05.000000 Google.Apis.Http.ConfigurableMessageHandler Response[00000001] An abnormal response wasn't handled. Status code is ServiceUnavailable", logEntries[0]);
            Assert.Equal("D2017-01-02 03:04:05.000000 Google.Apis.Http.ConfigurableMessageHandler Response[00000001] Abnormal response is being returned. Status Code is ServiceUnavailable", logEntries[1]);
        }

        [Fact]
        public async Task Logging_ResponseHeaders()
        {
            var logEntries = await LogTest(ConfigurableMessageHandler.LogEventType.ResponseHeaders);
            var entry = Assert.Single(logEntries);
            Assert.Contains("D2017-01-02 03:04:05.000000 Google.Apis.Http.ConfigurableMessageHandler Response[00000001] Headers:\n", entry);
            Assert.Contains("  [header1] 'One'", entry);
            Assert.Contains("  [header2] 'Two'", entry);
        }

        [Fact]
        public async Task Logging_ResponseBody()
        {
            var logEntries = await LogTest(ConfigurableMessageHandler.LogEventType.ResponseBody);
            Assert.Single(logEntries, "D2017-01-02 03:04:05.000000 Google.Apis.Http.ConfigurableMessageHandler Response[00000001] Body: 'ABC...DE'");
        }

        private class AddsQuotaProject : IHttpExecuteInterceptor
        {
            public Task InterceptAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                request.Headers.Add("x-goog-user-project", "my-project");
                return Task.FromResult(true);
            }
        }

        [Fact]
        public async Task FailsIfQuotaProjectSetWithInterceptors()
        {
            var configurableHandler = new ConfigurableMessageHandler(new HttpClientHandler());
            configurableHandler.AddExecuteInterceptor(new AddsQuotaProject());

            using (var client = new HttpClient(configurableHandler))
            {
               await Assert.ThrowsAsync<InvalidOperationException>(() => client.GetAsync("http://will.be.ignored"));
            }
        }

        [Fact]
        public async Task AcceptsQuotaProjectFromCredential()
        {
            var fakeHandler = new FakeHandler();
            var configurableHandler = new ConfigurableMessageHandler(fakeHandler)
            {
                Credential = new AddsQuotaProject()
            };

            using (var client = new HttpClient(configurableHandler))
            {
                await client.GetAsync("http://will.be.ignored");
            }

            Assert.Single(fakeHandler.LatestRequestHeaders, h => h.Key == "x-goog-user-project");
        }

        [Theory]
        // googleapis.com is no different here than any other domain,
        // we still test for it explicitly to document behaviour.
        [InlineData("googleapis.com")]
        [InlineData("custom.domain")]
        public async Task PropagatesUniverseDomain(string expectedUniverseDomain)
        {
            var fakeHandler = new FakeHandler();
            var configurableHandler = new ConfigurableMessageHandler(fakeHandler)
            {
                UniverseDomain = expectedUniverseDomain
            };

            using (var client = new HttpClient(configurableHandler))
            {
                await client.GetAsync("http://will.be.ignored");
            }

            Assert.True(fakeHandler.LatestRequest.TryGetOption(ConfigurableMessageHandler.UniverseDomainKey, out string universeDomain));
            Assert.Equal(expectedUniverseDomain, universeDomain);
        }

        [Fact]
        public async Task NoUniverseDomain()
        {
            var fakeHandler = new FakeHandler();
            var configurableHandler = new ConfigurableMessageHandler(fakeHandler);

            using (var client = new HttpClient(configurableHandler))
            {
                await client.GetAsync("http://will.be.ignored");
            }

            Assert.False(fakeHandler.LatestRequest.TryGetOption(ConfigurableMessageHandler.UniverseDomainKey, out string universeDomain));
        }

        /// <summary>
        /// Handler for intercepting all authenticated requests.
        /// </summary>
        private class FakeHandler : HttpMessageHandler
        {
            public HttpRequestHeaders LatestRequestHeaders => LatestRequest?.Headers;
            public HttpRequestMessage LatestRequest { get; private set; }

            protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            {
                LatestRequest = request;
                return Task.FromResult(new HttpResponseMessage());
            }
        }

        private int MaybeSetMaxRetries(int? perRequestMaxRetries, int defaultMaxRetries, HttpRequestMessage requestMessage)
        {
            if (perRequestMaxRetries == null)
            {
                return defaultMaxRetries;
            }
            int configuredRetries = perRequestMaxRetries.Value;
            var properties = TestHelpers.GetRequestProperties(requestMessage);
            properties.Add(ConfigurableMessageHandler.MaxRetriesKey, configuredRetries);
            return configuredRetries;
        }
    }
}