Exemplo n.º 1
0
        /// <summary>
        /// Take a scenario definition and convert the prototype request into a fully formed request. This includes appending
        /// the base URL to the request URL, executing any test-setup requests, and replacing the placeholders in the prototype
        /// request with proper values.
        /// </summary>
        /// <param name="scenario"></param>
        /// <param name="documents"></param>
        /// <param name="account"></param>
        /// <returns></returns>
        public async Task <ValidationResult <HttpRequest> > GenerateMethodRequestAsync(ScenarioDefinition scenario, DocSet documents, IServiceAccount primaryAccount, IServiceAccount secondaryAccount)
        {
            var parser  = new HttpParser();
            var request = parser.ParseHttpRequest(this.Request);

            AddAccessTokenToRequest(primaryAccount.CreateCredentials(), request);
            AddTestHeaderToRequest(scenario, request);
            AddAdditionalHeadersToRequest(primaryAccount, request);

            List <ValidationError> errors = new List <ValidationError>();

            if (null != scenario)
            {
                var storedValuesForScenario = new Dictionary <string, string>();

                if (null != scenario.TestSetupRequests)
                {
                    foreach (var setupRequest in scenario.TestSetupRequests)
                    {
                        try
                        {
                            // Use secondary account for specific setup requests
                            if (setupRequest.SecondaryAccount)
                            {
                                if (secondaryAccount == null)
                                {
                                    // We are expecting a secondary account
                                    errors.Add(
                                        new ValidationError(
                                            ValidationErrorCode.SecondaryAccountMissing,
                                            "GenerateMethodRequestAsync",
                                            "Expected secondary account for test scenario"));

                                    return(new ValidationResult <HttpRequest>(null, errors));
                                }

                                var result = await setupRequest.MakeSetupRequestAsync(storedValuesForScenario, documents, scenario, secondaryAccount);

                                errors.AddRange(result.Messages);

                                if (result.IsWarningOrError)
                                {
                                    // If we can an error or warning back from a setup method, we fail the whole request.
                                    return(new ValidationResult <HttpRequest>(null, errors));
                                }
                            }
                            else
                            {
                                var result = await setupRequest.MakeSetupRequestAsync(storedValuesForScenario, documents, scenario, primaryAccount);

                                errors.AddRange(result.Messages);

                                if (result.IsWarningOrError)
                                {
                                    // If we can an error or warning back from a setup method, we fail the whole request.
                                    return(new ValidationResult <HttpRequest>(null, errors));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            return(new ValidationResult <HttpRequest>(null, new ValidationError(ValidationErrorCode.ConsolidatedError, null, "An exception occured while processing setup-requests: {0}", ex.Message)));
                        }
                    }
                }

                try
                {
                    var placeholderValues = scenario.RequestParameters.ToPlaceholderValuesArray(storedValuesForScenario);
                    request.RewriteRequestWithParameters(placeholderValues);
                }
                catch (Exception ex)
                {
                    // Error when applying parameters to the request
                    errors.Add(
                        new ValidationError(
                            ValidationErrorCode.RewriteRequestFailure,
                            "GenerateMethodRequestAsync",
                            ex.Message));

                    return(new ValidationResult <HttpRequest>(null, errors));
                }

                if (scenario.StatusCodesToRetry != null)
                {
                    request.RetryOnStatusCode =
                        (from status in scenario.StatusCodesToRetry select(System.Net.HttpStatusCode) status).ToList();
                }
            }

            if (string.IsNullOrEmpty(request.Accept))
            {
                if (!string.IsNullOrEmpty(ValidationConfig.ODataMetadataLevel))
                {
                    request.Accept = MimeTypeJson + "; " + ValidationConfig.ODataMetadataLevel;
                }
                else
                {
                    request.Accept = MimeTypeJson;
                }
            }

            this.ModifyRequestForAccount(request, primaryAccount);
            return(new ValidationResult <HttpRequest>(request, errors));
        }
 public static string HttpMethodVerb(this MethodDefinition method)
 {
     HttpParser parser = new HttpParser();
     var request = parser.ParseHttpRequest(method.Request);
     return request.Method;
 }
Exemplo n.º 3
0
        public void ParseOdataMethod(string odataUrl, string method)
        {
            var request = HttpParser.ParseHttpRequest(odataUrl);

            Assert.AreEqual(method, request.Method, "Parsed Method should be equal to request header method");
        }
Exemplo n.º 4
0
        public void HttpVersionShouldDefaultToHttp1(string odataUrl)
        {
            var request = HttpParser.ParseHttpRequest(odataUrl);

            Assert.AreEqual("HTTP/1.1", request.HttpVersion, "When HttpVersion is not specified, default to HTTP/1.1");
        }
Exemplo n.º 5
0
        public void HttpVersionShouldBeRespected(string odataUrl, string httpVersion)
        {
            var request = HttpParser.ParseHttpRequest(odataUrl);

            Assert.AreEqual(httpVersion, request.HttpVersion, "When HttpVersion is specified, should be respected");
        }
Exemplo n.º 6
0
        public void ParseOdataUrl(string odataUrl, string actualUrl)
        {
            var request = HttpParser.ParseHttpRequest(odataUrl);

            Assert.AreEqual(actualUrl, request.Url, "Parsed Url should be equal to request header url");
        }
Exemplo n.º 7
0
        /// <summary>
        /// Take a scenario definition and convert the prototype request into a fully formed request. This includes appending
        /// the base URL to the request URL, executing any test-setup requests, and replacing the placeholders in the prototype
        /// request with proper values.
        /// </summary>
        /// <param name="scenario"></param>
        /// <param name="baseUrl"></param>
        /// <param name="credentials"></param>
        /// <param name="documents"></param>
        /// <returns></returns>
        public async Task <ValidationResult <HttpRequest> > GenerateMethodRequestAsync(ScenarioDefinition scenario, string baseUrl, AuthenicationCredentials credentials, DocSet documents)
        {
            var parser  = new HttpParser();
            var request = parser.ParseHttpRequest(this.Request);

            AddAccessTokenToRequest(credentials, request);
            AddTestHeaderToRequest(scenario, request);

            List <ValidationError> errors = new List <ValidationError>();

            if (null != scenario)
            {
                var storedValuesForScenario = new Dictionary <string, string>();

                if (null != scenario.TestSetupRequests)
                {
                    foreach (var setupRequest in scenario.TestSetupRequests)
                    {
                        var result = await setupRequest.MakeSetupRequestAsync(baseUrl, credentials, storedValuesForScenario, documents, scenario);

                        errors.AddRange(result.Messages);

                        if (result.IsWarningOrError)
                        {
                            // If we can an error or warning back from a setup method, we fail the whole request.
                            return(new ValidationResult <HttpRequest>(null, errors));
                        }
                    }
                }

                try
                {
                    var placeholderValues = scenario.RequestParameters.ToPlaceholderValuesArray(storedValuesForScenario);
                    request.RewriteRequestWithParameters(placeholderValues);
                }
                catch (Exception ex)
                {
                    // Error when applying parameters to the request
                    errors.Add(
                        new ValidationError(
                            ValidationErrorCode.RewriteRequestFailure,
                            "GenerateMethodRequestAsync",
                            ex.Message));

                    return(new ValidationResult <HttpRequest>(null, errors));
                }

                if (scenario.StatusCodesToRetry != null)
                {
                    request.RetryOnStatusCode =
                        (from status in scenario.StatusCodesToRetry select(System.Net.HttpStatusCode) status).ToList();
                }
            }

            if (string.IsNullOrEmpty(request.Accept))
            {
                if (!string.IsNullOrEmpty(ValidationConfig.ODataMetadataLevel))
                {
                    request.Accept = MimeTypeJson + "; " + ValidationConfig.ODataMetadataLevel;
                }
                else
                {
                    request.Accept = MimeTypeJson;
                }
            }

            return(new ValidationResult <HttpRequest>(request, errors));
        }