Ejemplo n.º 1
0
        public void NoTelemetryWhenDisabled()
        {
            var mockHandler = new Mock <HttpClientHandler> {
                CallBase = true
            };
            var httpClient = new SystemNetHttpClient(
                new System.Net.Http.HttpClient(mockHandler.Object),
                enableTelemetry: false);
            var stripeClient = new StripeClient("sk_test_123", httpClient: httpClient);

            mockHandler.Reset();
            var fakeServer = FakeServer.ForMockHandler(mockHandler);

            fakeServer.Delay = TimeSpan.FromMilliseconds(20);

            var service = new BalanceService(stripeClient);

            service.Get();
            fakeServer.Delay = TimeSpan.FromMilliseconds(40);
            service.Get();
            service.Get();

            mockHandler.Protected()
            .Verify(
                "SendAsync",
                Times.Exactly(3),
                ItExpr.Is <HttpRequestMessage>(m =>
                                               !m.Headers.Contains("X-Stripe-Client-Telemetry")),
                ItExpr.IsAny <CancellationToken>());
        }
Ejemplo n.º 2
0
        public async Task TelemetryWorksWithConcurrentRequests()
        {
            this.MockHttpClientFixture.Reset();
            var fakeServer = FakeServer.ForMockHandler(this.MockHttpClientFixture.MockHandler);

            fakeServer.Delay = TimeSpan.FromMilliseconds(20);

            var service = new BalanceService(this.StripeClient);

            // the first 2 requests will not contain telemetry
            await Task.WhenAll(service.GetAsync(), service.GetAsync());

            // the following 2 requests will contain telemetry
            await Task.WhenAll(service.GetAsync(), service.GetAsync());

            this.MockHttpClientFixture.MockHandler.Protected()
            .Verify(
                "SendAsync",
                Times.Exactly(2),
                ItExpr.Is <HttpRequestMessage>(m =>
                                               !m.Headers.Contains("X-Stripe-Client-Telemetry")),
                ItExpr.IsAny <CancellationToken>());

            this.MockHttpClientFixture.MockHandler.Protected()
            .Verify(
                "SendAsync",
                Times.Once(),
                ItExpr.Is <HttpRequestMessage>(m =>
                                               TelemetryHeaderMatcher(
                                                   m.Headers,
                                                   (s) => s == "req_1",
                                                   (d) => d >= 15)),
                ItExpr.IsAny <CancellationToken>());

            this.MockHttpClientFixture.MockHandler.Protected()
            .Verify(
                "SendAsync",
                Times.Once(),
                ItExpr.Is <HttpRequestMessage>(m =>
                                               TelemetryHeaderMatcher(
                                                   m.Headers,
                                                   (s) => s == "req_2",
                                                   (d) => d >= 15)),
                ItExpr.IsAny <CancellationToken>());
        }
Ejemplo n.º 3
0
        public void TelemetryWorks()
        {
            this.MockHttpClientFixture.Reset();
            var fakeServer = FakeServer.ForMockHandler(this.MockHttpClientFixture.MockHandler);

            fakeServer.Delay = TimeSpan.FromMilliseconds(20);

            var service = new BalanceService(this.StripeClient);

            service.Get();
            fakeServer.Delay = TimeSpan.FromMilliseconds(40);
            service.Get();
            service.Get();

            this.MockHttpClientFixture.MockHandler.Protected()
            .Verify(
                "SendAsync",
                Times.Once(),
                ItExpr.Is <HttpRequestMessage>(m =>
                                               !m.Headers.Contains("X-Stripe-Client-Telemetry")),
                ItExpr.IsAny <CancellationToken>());

            this.MockHttpClientFixture.MockHandler.Protected()
            .Verify(
                "SendAsync",
                Times.Once(),
                ItExpr.Is <HttpRequestMessage>(m =>
                                               TelemetryHeaderMatcher(
                                                   m.Headers,
                                                   (s) => s == "req_1",
                                                   (d) => d >= 15)),
                ItExpr.IsAny <CancellationToken>());

            this.MockHttpClientFixture.MockHandler.Protected()
            .Verify(
                "SendAsync",
                Times.Once(),
                ItExpr.Is <HttpRequestMessage>(m =>
                                               TelemetryHeaderMatcher(
                                                   m.Headers,
                                                   (s) => s == "req_2",
                                                   (d) => d >= 30)),
                ItExpr.IsAny <CancellationToken>());
        }