Exemplo n.º 1
0
        protected void AssertResponse(
            ApiRequestContext apiContext,
            HttpResponse response,
            int expectedHttpStatus     = 200,
            bool shouldHaveResponse    = true,
            string expectedContentType = null,
            ApiValidationState expectedValidationState      = ApiValidationState.Succeeded,
            NameValuePairs <string, string> extendedHeaders = null,
            bool shouldBeCancelledReuqest       = false,
            string expectedProtocol             = "HTTP/1.1",
            bool?expectedAuthenticationResult   = null,
            string expectedAuthenticationScheme = null,
            string expectedAuthenticationValue  = null,
            string expectedCacheControlValue    = null,
            int?expectedExpiresSecondsAdd       = null,
            string expectedCulture = null,
            AuthenticationType expectedAuthenticatedBy = AuthenticationType.None,
            bool?expectedAuthorizationResult           = null,
            int?expectedAuthorizationResultCount       = 1,
            AuthorizationType expectedAuthorizedBy     = AuthorizationType.None,
            bool?allowCustom500Response = false,
            long?expectedContentLength  = null,
            NameValuePairs <string, string> expectedItems = null)
        {
            apiContext.Should().NotBeNull();
            response.Should().NotBeNull();

            // Check for extended messages and exceptions on expected
            // success responses.  This helps in debuging failing tests
            if (expectedHttpStatus >= 200 && expectedHttpStatus < 300)
            {
                foreach (var error in apiContext.Runtime.Exceptions)
                {
                    error.ToString().Should().BeNull();
                }

                foreach (var error in apiContext.Validation.Errors)
                {
                    error.Should().BeNull();
                }
            }


            apiContext.Response.StatusCode.Should().Be(expectedHttpStatus);
            apiContext.ValidationState().Should().Be(expectedValidationState);
            apiContext.PathBase.Should().Be(apiContext.Request.Path);
            response.StatusCode.Should().Be(expectedHttpStatus);

            var expectedHeaderCount = ((response.StatusCode >= 500 && allowCustom500Response != true) || apiContext.Request.IsCorsPreflightRequest())
                ? 3 + (extendedHeaders?.Count ?? 0) + (shouldHaveResponse ? 0 : -1)
                : 5 + (extendedHeaders?.Count ?? 0) + (shouldHaveResponse ? 0 : -1);

            var responseHeaderCount = response.Headers.Sum(c => c.Value.Count);

            // Check for headers
            apiContext.Response.Headers.Should().HaveCount(expectedHeaderCount);
            responseHeaderCount.Should().Be(expectedHeaderCount);

            response.Headers.Should().ContainKey("Date");
            response.Headers["Date"].Should().Equal(apiContext.Response.Date?.ToString("r"));
            apiContext.Response.Headers.HasHeader("Date").Should().BeTrue();
            apiContext.Response.Headers.GetHeader("Date").Value.Should().Be(apiContext.Response.Date?.ToString("r"));

            var assertionContentLength = expectedContentLength ?? response.Body.Length;

            response.Headers.Should().ContainKey("Content-Length");
            response.Headers["Content-Length"].Should().Equal(assertionContentLength.ToString());
            apiContext.Response.Headers.HasHeader("Content-Length").Should().BeTrue();
            apiContext.Response.Headers.GetHeader("Content-Length").Value.Should().Be(assertionContentLength.ToString());
            apiContext.Response.ContentLength.Should().Be(assertionContentLength);

            if (response.StatusCode < 500 && apiContext.Request.IsCorsPreflightRequest() == false)
            {
                response.Headers.Should().ContainKey("Cache-Control");
                response.Headers["Cache-Control"].Should().Equal(expectedCacheControlValue ?? this.cacheControlNoCache);
                apiContext.Response.Headers.HasHeader("Cache-Control").Should().BeTrue();
                apiContext.Response.Headers.GetHeader("Cache-Control").Value.Should().Be(expectedCacheControlValue ?? this.cacheControlNoCache);

                var expectedExpiresValue = expectedExpiresSecondsAdd.HasValue
                    ? apiContext.Response.Date?.AddSeconds(expectedExpiresSecondsAdd.Value).ToString("r")
                    : apiContext.Response.Date?.AddSeconds(-1).ToString("r");

                response.Headers.Should().ContainKey("Expires");
                response.Headers["Expires"].Should().Equal(expectedExpiresValue);
                apiContext.Response.Headers.HasHeader("Expires").Should().BeTrue();
                apiContext.Response.Headers.GetHeader("Expires").Value.Should().Be(expectedExpiresValue);
            }

            if (shouldHaveResponse)
            {
                response.Headers.Should().ContainKey("Content-Type");
                response.Headers["Content-Type"].Should().Equal(expectedContentType ?? this.applicationJson);
                apiContext.Response.Headers.HasHeader("Content-Type").Should().BeTrue();
                apiContext.Response.Headers.GetHeader("Content-Type").Value.Should().Be(expectedContentType ?? this.applicationJson);
                apiContext.Response.ContentType.Should().Be(expectedContentType);

                apiContext.Response.ContentLength.Should().BeGreaterThan(0);
                int.TryParse(response.Headers["Content-Length"], out var contentLength);
                contentLength.Should().BeGreaterThan(0);

                apiContext.Response.ResponseWriter.Should().NotBeNull();
                apiContext.Response.ResponseWriterOptions.Should().NotBeNull();

                if (apiContext.Request.IsHeadRequest())
                {
                    apiContext.Response.ResponseObject.Should().BeNull();
                }
                else
                {
                    apiContext.Response.ResponseObject.Should().NotBeNull();
                }
            }
            else
            {
                response.Headers.Should().NotContainKey("Content-Type");
                apiContext.Response.Headers.HasHeader("Content-Type").Should().BeFalse();

                if (expectedHttpStatus == 304)
                {
                    apiContext.Response.ResponseObject.Should().NotBeNull();
                }
                else
                {
                    apiContext.Response.ResponseObject.Should().BeNull();
                }
                apiContext.Response.ResponseWriter.Should().BeNull();
                apiContext.Response.ResponseWriterOptions.Should().BeNull();

                apiContext.Response.ContentLength.Should().Be(0);
                int.Parse(response.Headers["Content-Length"]).Should().Be(0);
            }

            if (extendedHeaders != null)
            {
                foreach (var header in extendedHeaders)
                {
                    response.Headers.Should().ContainKey(header.Key);
                    response.Headers[header.Key].Should().Contain(header.Value);

                    apiContext.Response.Headers.HasHeader(header.Key).Should().BeTrue();

                    var matchingHeader = apiContext.Response.Headers
                                         .ToList()
                                         .Where(h => h.Name == header.Key)
                                         .Where(h => h.Value == header.Value)
                                         .FirstOrDefault();

                    matchingHeader.Should().NotBeNull();
                }
            }

            // -------------------------------------
            // Misicalleanous Api Context Validation
            apiContext.Runtime.Duration.Should().NotBeNull();
            apiContext.Runtime.Duration.Duration.Should().Be(
                (int)(apiContext.Runtime.Duration.UtcEnd.Value - apiContext.Runtime.Duration.UtcStart).TotalMilliseconds);

            apiContext.RequestAborted.IsCancellationRequested.Should().Be(shouldBeCancelledReuqest);
            apiContext.Request.Protocol.Should().Be(expectedProtocol);
            apiContext.Request.RequestIdentifier.Should().NotBeNull();

            if (!(apiContext.Configuration?.AllowAnonymous ?? false) && expectedAuthenticationResult.HasValue)
            {
                apiContext.Request.ClientAuthenticationInfo.Should().NotBeNull();
                apiContext.Request.ClientAuthenticationInfo.AuthenticatedBy.Should().Be(expectedAuthenticatedBy);
                apiContext.Request.ClientAuthenticationInfo.AuthScheme.Should().Be(expectedAuthenticationScheme);
                apiContext.Request.ClientAuthenticationInfo.AuthValue.Should().Be(expectedAuthenticationValue);
                apiContext.Request.ClientAuthenticationInfo.AuthResult.Should().NotBeNull();
                apiContext.Request.ClientAuthenticationInfo.AuthResult.IsAuthenticated.Should().Be(expectedAuthenticationResult.Value);
            }

            if (apiContext.Request.ClientAuthenticationInfo?.AuthResult?.IsAuthenticated == true)
            {
                if (!(apiContext.Configuration?.AllowAnonymous ?? false) && expectedAuthorizationResult.HasValue)
                {
                    apiContext.Request.ClientAuthorizationInfo.Should().NotBeNull();
                    apiContext.Request.ClientAuthorizationInfo.AuthorizedBy.Should().Be(expectedAuthorizedBy);
                    apiContext.Request.ClientAuthorizationInfo.AuthResults.Should().HaveCount(expectedAuthorizationResultCount.Value);
                    apiContext.Request.ClientAuthorizationInfo.AuthResults.Last().IsAuthorized.Should().Be(expectedAuthorizationResult.Value);
                }
            }
            else
            {
                apiContext.Request.ClientAuthorizationInfo.Should().BeNull();
            }

            apiContext.Request.AcceptCulture?.Name.Should().Be(expectedCulture ?? CultureInfo.CurrentUICulture.Name);

            apiContext.Items.ContainsKey("requestHandlerCount").Should().BeTrue();
            apiContext.Items["requestHandlerCount"].Should().Be(1);

            var exceptionCount = apiContext.Runtime.Exceptions
                                 .Where(e => e as ApiException == null)
                                 .Count();

            if (apiContext.Response.StatusCode >= 500 && exceptionCount > 0)
            {
                apiContext.Items.ContainsKey("exceptionHandlerCount").Should().BeTrue();
                apiContext.Items["exceptionHandlerCount"].Should().Be(1);
            }

            if (expectedItems != null)
            {
                foreach (var item in expectedItems)
                {
                    apiContext.Items.TryGetValue(item.Key, out var val);
                    val.Should().Be(item.Value);
                }
            }

            var jsonDump = apiContext.Dump();

            jsonDump.Should().NotBeNullOrWhiteSpace();
        }