public async Task <Tuple <string, string> > GetResponseAsync(EndpointCollection endpointCollection, DateTime?now)
        {
            var endpoint = endpointCollection.Resolve(RequestPath);

            if (endpoint == null)
            {
                return(Tuple.Create((string)null, ERROR_NOMATCHING_ENDPOINT));
            }
            var matcher_and_creator = endpoint.Resolve(Method, new PathString(RequestPath), new QueryString(QueryString), RequestBody ?? "", null);

            if (matcher_and_creator != null)
            {
                var responseCreator = matcher_and_creator.ResponseCreator as SimpleResponseCreator;
                if (responseCreator == null)
                {
                    return(Tuple.Create((string)null, $"This response creator is not supported by test framework: {matcher_and_creator.ResponseCreator.ToString()}"));
                }

                var requestInfo = new RequestInfo
                {
                    Endpoint    = endpoint,
                    Headers     = null,
                    QueryString = QueryString,
                    RequestBody = RequestBody,
                    RequestPath = RequestPath
                };
                if (now != null)
                {
                    requestInfo.SetStaticNow(now.Value);
                }
                var responseBody = await responseCreator.GetBodyAndExecuteReplacementsAsync(requestInfo);

                return(Tuple.Create(responseBody, (string)null));
            }
            else
            {
                return(Tuple.Create((string)null, ERROR_ENDPOINT_HAS_NO_MATCH));
            }
        }
Esempio n. 2
0
        async public Task <NetmockeryTestCaseResult> ExecuteAsync(EndpointCollection endpointCollection, bool handleErrors = true, DateTime?now = null)
        {
            Debug.Assert(endpointCollection != null);

            var testResult = new NetmockeryTestCaseResult {
                TestCase = this
            };

            try
            {
                var endpoint = endpointCollection.Resolve(RequestPath);
                if (endpoint == null)
                {
                    return(testResult.SetFailure(ERROR_NOMATCHING_ENDPOINT));
                }
                testResult.EndpointName = endpoint.Name;

                var matcher_and_creator = endpoint.Resolve(new PathString(RequestPath), new QueryString(QueryString), RequestBody ?? "", null);
                if (matcher_and_creator == null)
                {
                    return(testResult.SetFailure(ERROR_ENDPOINT_HAS_NO_MATCH));
                }
                if (!HasExpectations)
                {
                    return(testResult.SetFailure("Test case has no expectations"));
                }

                var    responseCreator = matcher_and_creator.ResponseCreator;
                string responseBody    = null;
                string charset         = "";
                string contenttype     = "";
                if (NeedsResponseBody)
                {
                    var simpleResponseCreator = responseCreator as SimpleResponseCreator;
                    if (simpleResponseCreator == null)
                    {
                        return(testResult.SetFailure($"Response creator {responseCreator.ToString()} not supported by test framework"));
                    }

                    var requestInfo = new RequestInfo
                    {
                        EndpointDirectory = endpoint.Directory,
                        Headers           = null,
                        RequestPath       = RequestPath,
                        QueryString       = QueryString,
                        RequestBody       = RequestBody
                    };
                    if (now != null)
                    {
                        requestInfo.SetStaticNow(now.Value);
                    }
                    responseBody = simpleResponseCreator.GetBodyAndExecuteReplacements(requestInfo);
                    contenttype  = simpleResponseCreator.ContentType ?? "";
                    charset      = simpleResponseCreator.Encoding.WebName;
                }
                string message;
                if (Evaluate(matcher_and_creator.RequestMatcher.ToString(), matcher_and_creator.ResponseCreator.ToString(), responseBody, contenttype, charset, out message))
                {
                    return(testResult.SetSuccess());
                }
                else
                {
                    return(testResult.SetFailure(message));
                }
            }
            catch (Exception exception)
            {
                if (!handleErrors)
                {
                    throw;
                }
                return(testResult.SetException(exception));
            }
        }