Exemple #1
0
 public SupplementalFile(string basePath, string relativePath, DocSet parent)
     : base(basePath, relativePath, parent)
 {
 }
Exemple #2
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)
        {
            List <ValidationError> errors  = new List <ValidationError>();
            HttpRequest            request = null;

            try
            {
                request = HttpParser.ParseHttpRequest(this.Request);
            }
            catch (Exception ex)
            {
                errors.Add(
                    new ValidationError(
                        ValidationErrorCode.HttpParserError,
                        "GenerateMethodRequestAsync", ex.Message));
                return(new ValidationResult <HttpRequest>(null, errors));
            }

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

            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));
        }