public static async Task Options_Can_Be_Cloned()
        {
            // Arrange
            var url     = "https://google.com/";
            var payload = new MyObject()
            {
                Message = "Hello world!"
            };

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

            // Act
            var clone = options.Clone()
                        .RegisterGetJson(url, payload, statusCode: HttpStatusCode.InternalServerError);

            // Assert
            clone.OnMissingRegistration.ShouldBe(options.OnMissingRegistration);
            clone.OnSend.ShouldBe(options.OnSend);
            clone.ThrowOnMissingRegistration.ShouldBe(options.ThrowOnMissingRegistration);

            await HttpAssert.GetAsync(options, url, HttpStatusCode.NotFound);

            await HttpAssert.GetAsync(clone, url, HttpStatusCode.InternalServerError);

            // Arrange
            options.OnMissingRegistration = (_) => Task.FromResult <HttpResponseMessage>(null);
            options.OnSend = (_) => Task.CompletedTask;
            options.ThrowOnMissingRegistration = true;
            options.Clear();

            // Act and Assert
            clone.ThrowOnMissingRegistration.ShouldNotBe(options.ThrowOnMissingRegistration);
            clone.OnMissingRegistration.ShouldNotBe(options.OnMissingRegistration);
            clone.OnSend.ShouldNotBe(options.OnSend);

            await Assert.ThrowsAsync <HttpRequestNotInterceptedException>(() => HttpAssert.GetAsync(options, url, HttpStatusCode.InternalServerError));

            await HttpAssert.GetAsync(clone, url, HttpStatusCode.InternalServerError);

            // Arrange
            options = new HttpClientInterceptorOptions()
                      .ThrowsOnMissingRegistration();

            // Act
            clone = options.Clone();

            // Assert
            clone.ThrowOnMissingRegistration.ShouldBe(options.ThrowOnMissingRegistration);
            clone.OnMissingRegistration.ShouldBe(options.OnMissingRegistration);
            clone.OnSend.ShouldBe(options.OnSend);
        }
Ejemplo n.º 2
0
        public static async Task Options_Can_Be_Cloned()
        {
            // Arrange
            var url     = "https://google.com/";
            var payload = new MyObject()
            {
                Message = "Hello world!"
            };

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

            // Act
            var clone = options.Clone()
                        .RegisterGet(url, payload, statusCode: HttpStatusCode.InternalServerError);

            // Assert
            clone.ThrowOnMissingRegistration.ShouldBe(options.ThrowOnMissingRegistration);
            await HttpAssert.GetAsync(options, url, HttpStatusCode.NotFound);

            await HttpAssert.GetAsync(clone, url, HttpStatusCode.InternalServerError);

            // Arrange
            options.ThrowOnMissingRegistration = true;
            options.Clear();

            // Act and Assert
            clone.ThrowOnMissingRegistration.ShouldNotBe(options.ThrowOnMissingRegistration);
            await Assert.ThrowsAsync <InvalidOperationException>(() => HttpAssert.GetAsync(options, url, HttpStatusCode.InternalServerError));

            await HttpAssert.GetAsync(clone, url, HttpStatusCode.InternalServerError);

            // Arrange
            options = new HttpClientInterceptorOptions()
            {
                ThrowOnMissingRegistration = true,
            };

            // Act
            clone = options.Clone();

            // Assert
            clone.ThrowOnMissingRegistration.ShouldBe(options.ThrowOnMissingRegistration);
        }