Пример #1
0
 public TestRunner(EndpointCollection endpointCollection)
 {
     this.endpointCollection = endpointCollection;
     Debug.Assert(HasTestSuite(endpointCollection.SourceDirectory));
     testcases = ReadFromDirectory(endpointCollection.SourceDirectory);
     SetStaticTimeIfConfigured(endpointCollection.SourceDirectory);
 }
        static public EndpointCollection ReadFromDirectory(string directoryName)
        {
            var retval = new EndpointCollection {
                SourceDirectory = directoryName
            };
            var globalDefaultsFile = Path.Combine(directoryName, "defaults.json");

            var globalDefaults =
                File.Exists(globalDefaultsFile)
                ?
                JsonConvert.DeserializeObject <JSONDefaults>(File.ReadAllText(globalDefaultsFile))
                :
                null;

            if (globalDefaults != null)
            {
                globalDefaults.ThrowExceptionIfAdditionalData();
            }

            foreach (var subdirectory in Directory.GetDirectories(directoryName))
            {
                var endpointFile = Path.Combine(subdirectory, "endpoint.json");
                if (File.Exists(endpointFile))
                {
                    retval.Add(JSONReader.ReadEndpoint(File.ReadAllText(endpointFile), subdirectory, globalDefaults));
                }
            }
            return(retval);
        }
Пример #3
0
 public void Reload()
 {
     if (!canReload)
     {
         throw new InvalidOperationException();
     }
     endpointCollection = EndpointCollectionReader.ReadFromDirectory(directory);
     reloadTimestamps.Add(DateTime.Now);
 }
Пример #4
0
 public static void Dump(EndpointCollection endpointCollection)
 {
     foreach (var endpoint in endpointCollection.Endpoints)
     {
         WriteLine($"{endpoint.Name} {endpoint.PathRegex}");
         foreach (var response in endpoint.Responses)
         {
             WriteLine($"    {response.Item1} -> {response.Item2}");
         }
     }
 }
Пример #5
0
        static public EndpointCollection WithEndpoints(params Endpoint[] endpoints)
        {
            var retval = new EndpointCollection();

            foreach (var endpoint in endpoints)
            {
                retval.Add(endpoint);
            }

            return(retval);
        }
Пример #6
0
        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));
            }
        }
Пример #7
0
 public ConsoleTestRunner(EndpointCollection endpointCollection) : base(endpointCollection)
 {
 }
Пример #8
0
        public static void Test(ParsedCommandLine commandArgs, EndpointCollection endpointCollection)
        {
            if (!TestRunner.HasTestSuite(endpointCollection.SourceDirectory))
            {
                Error.WriteLine("ERROR: No test suite found");
                return;
            }

            if (commandArgs.Diff && commandArgs.Only == null)
            {
                Error.WriteLine("ERROR: --diff can only be specified with --only");
                return;
            }

            var testRunner = new ConsoleTestRunner(endpointCollection);

            if (commandArgs.Url != null)
            {
                testRunner.Url = commandArgs.Url;
            }

            if (commandArgs.Only != null)
            {
                var indexes = ParseOnlyArgument(commandArgs.Only, (from testCase in testRunner.Tests select testCase.Name).ToArray());
                if (indexes.Length == 0)
                {
                    Error.WriteLine("ERROR: No testcases matches --only");
                }

                foreach (var index in indexes)
                {
                    if (commandArgs.Diff)
                    {
                        var diffTool = Environment.GetEnvironmentVariable("DIFFTOOL");
                        if (diffTool == null)
                        {
                            Error.WriteLine("ERROR: No diff tool configured. Set DIFFTOOL environment variable to point to executable.");
                            return;
                        }

                        var testCase = testRunner.Tests.ElementAt(index);
                        if (testCase.ExpectedResponseBody == null)
                        {
                            Error.WriteLine($"ERROR: Test case has no expected response body");
                            return;
                        }

                        var responseTuple = testCase.GetResponse(endpointCollection, testRunner.Now);
                        if (responseTuple.Item2 != null)
                        {
                            Error.WriteLine($"ERROR: {responseTuple.Item2}");
                            return;
                        }

                        var expectedFilename = Path.GetTempFileName();
                        var actualFilename   = Path.GetTempFileName();

                        File.WriteAllText(expectedFilename, testCase.ExpectedResponseBody);
                        File.WriteAllText(actualFilename, responseTuple.Item1);

                        StartExternalDiffTool(diffTool, expectedFilename, actualFilename);
                    }
                    else
                    {
                        if (commandArgs.ShowResponse)
                        {
                            testRunner.ShowResponse(index);
                        }
                        else
                        {
                            testRunner.ExecuteTestAndOutputResult(index);
                        }
                    }
                }
            }
            else
            {
                testRunner.TestAll(commandArgs.Stop, true);
            }
        }
Пример #9
0
 public EndpointCollectionProvider(EndpointCollection endpointCollection)
 {
     reloadTimestamps.Add(DateTime.Now);
     canReload = false;
     this.endpointCollection = endpointCollection;
 }
Пример #10
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));
            }
        }