コード例 #1
0
        public static async Task Use_Scopes_To_Change_And_Then_Restore_Behavior()
        {
            // Arrange
            var builder = new HttpRequestInterceptionBuilder()
                          .ForHost("public.je-apis.com")
                          .WithJsonContent(new { value = 1 });

            var options = new HttpClientInterceptorOptions()
                          .Register(builder);

            string json1;
            string json2;
            string json3;

            // Act
            using (var client = options.CreateHttpClient())
            {
                json1 = await client.GetStringAsync("http://public.je-apis.com");

                using (options.BeginScope())
                {
                    options.Register(builder.WithJsonContent(new { value = 2 }));

                    json2 = await client.GetStringAsync("http://public.je-apis.com");
                }

                json3 = await client.GetStringAsync("http://public.je-apis.com");
            }

            // Assert
            json1.ShouldNotBe(json2);
            json1.ShouldBe(json3);
        }
コード例 #2
0
        public static async Task Options_Can_Be_Scoped()
        {
            // Arrange
            var url     = "https://google.com/";
            var payload = new MyObject()
            {
                Message = "Hello world!"
            };

            var options = new HttpClientInterceptorOptions()
                          .RegisterGetJson(url, payload, statusCode: HttpStatusCode.NotFound);

            // Act - begin first scope
            using (options.BeginScope())
            {
                // Assert - original registration
                await HttpAssert.GetAsync(options, url, HttpStatusCode.NotFound);

                // Arrange - first update to registration
                options.RegisterGetJson(url, payload, statusCode: HttpStatusCode.InternalServerError);

                // Assert - first updated registration
                await HttpAssert.GetAsync(options, url, HttpStatusCode.InternalServerError);

                // Act - begin second scope
                using (options.BeginScope())
                {
                    // Arrange - second update to registration
                    options.RegisterGetJson(url, payload, statusCode: HttpStatusCode.RequestTimeout);

                    // Assert - second updated registration
                    await HttpAssert.GetAsync(options, url, HttpStatusCode.RequestTimeout);
                }

                // Assert - first updated registration
                await HttpAssert.GetAsync(options, url, HttpStatusCode.InternalServerError);
            }

            // Assert - original registration
            await HttpAssert.GetAsync(options, url, HttpStatusCode.NotFound);
        }