private async Task CheckConfigdirectoryAsync(string directory) { Assert.True(Directory.Exists(directory), $"Directory {directory} does not exist"); var endpointCollection = EndpointCollectionReader.ReadFromDirectory(directory); Assert.True(endpointCollection.Endpoints.Count() > 0, $"No endpoints defined in {directory}"); output.WriteLine(directory); if (!TestRunner.HasTestSuite(directory)) { output.WriteLine($"No tests in {directory}"); return; } var tests = new ConsoleTestRunner(endpointCollection); foreach (var test in tests.Tests) { output.WriteLine(test.Name); var result = await test.ExecuteAsync(endpointCollection, now : tests.Now); output.WriteLine(result.ResultAsString); Assert.True(result.OK, $"Test case {result.TestCase.Name}, message '{result.Message}'"); } }
public void RunTestsWithReplacement() { var testRunner = new ConsoleTestRunner(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); var result = testRunner.ExecuteTestAndOutputResult(1); Assert.True(result.OK, result.ResultAsString); }
public void TestsHaveConstantGetNow() { var testRunner = new ConsoleTestRunner(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); var result = testRunner.ExecuteTestAndOutputResult(3); Assert.True(result.OK, result.ResultAsString); }
public void TestsCanHaveDynamicNow() { dc.DeleteFile("tests\\now.txt"); var testRunner = new ConsoleTestRunner(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); var result = testRunner.ExecuteTestAndOutputResult(3); Assert.True(result.Error, result.ResultAsString); Assert.Null(result.Exception); }
async public void CanExecuteTest() { var endpointCollection = EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName); var testRunner = new ConsoleTestRunner(endpointCollection); var test = testRunner.Tests.ElementAt(0); var result = await test.ExecuteAsync(endpointCollection, handleErrors : false); Assert.True(result.OK); }
private EndpointCollection CreateEndpointWithParam(JSONParam jsonParam) { dc.AddFile("endpoint/endpoint.json", JsonConvert.SerializeObject(DataUtils.CreateScriptEndpoint("endpoint", "script.csscript"))); dc.AddFile("endpoint/script.csscript", "return GetParam(\"greeting\") + \" \" + GetParam(\"subject\");"); dc.AddFile("endpoint/params.json", JsonConvert.SerializeObject(new[] { jsonParam })); return(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); }
public void ReadsFromEndpointJson() { var endpointCollection = EndpointCollectionReader.ReadFromDirectory("examples/example1"); var endpoint3 = endpointCollection.Resolve("/statuscode"); Assert.Equal("StatusCodes", endpoint3.Name); var responseCreators = from t in endpoint3.Responses select (SimpleResponseCreator)t.Item2; Assert.All(responseCreators, r => Assert.Equal("text/plain", r.ContentType)); Assert.Equal(new[] { 200, 404, 499 }, from r in responseCreators select (int) r.StatusCode); }
async public void CanReadExpectedResponseBodyFromFile() { var endpointCollection = EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName); var testRunner = new ConsoleTestRunner(endpointCollection); var test = testRunner.Tests.ElementAt(2); var result = await test.ExecuteAsync(endpointCollection, handleErrors : false); Assert.True(result.OK, result.Message); }
async public void CanCheckExpectedRequestMatcherSuccess() { var testcase = (new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "this is a test", expectedrequestmatcher = "Regex 'test'" }) .Validated().CreateTestCase("."); var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); Assert.True(result.OK); }
public async Task TestRunnerKeepsTrackOfTestCoverage() { var testRunner = new ConsoleTestRunner(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); await testRunner.TestAllAsync(false, true); var coverageInfo = testRunner.GetCoverageInfo(); Assert.NotNull(coverageInfo); Assert.Equal(new[] { "foo", "GetNow" }, coverageInfo.EndpointsCovered); Assert.Equal(0, coverageInfo.EndpointsNotCovered.Length); }
async public void CanExecuteWithQueryStringFailure() { var testcase = (new JSONTest { name = "checksomething", requestpath = "/foo/", querystring = "?a=test", requestbody = "foobar", expectedresponsecreator = "File content.txt" }) .Validated().CreateTestCase("."); var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); Assert.True(result.OK, result.Message); Assert.Null(result.Message); }
public void CanGetResultBody() { var testcase = (new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "this is a test", expectedrequestmatcher = "Regex 'test'" }) .Validated().CreateTestCase("."); var result = testcase.GetResponse(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName), null); Assert.Equal("FOOBARBOOBAR", result.Item1); Assert.Null(result.Item2); }
public async Task TrackTestConverageByResponseRule() { var testRunner = new ConsoleTestRunner(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); await testRunner.TestAllAsync(false, false); var coverageInfo = testRunner.GetCoverageInfo(); Assert.NotNull(coverageInfo); Assert.Equal(new[] { "foo#0", "GetNow#0" }, coverageInfo.ResponseRulesCovered); Assert.Equal(new[] { "foo#1", "foo#2" }, coverageInfo.ResponseRulesNotCovered); }
public async Task TestRunnerKeepsTrackOfCoverageWhenRunningSingleTest() { var testRunner = new ConsoleTestRunner(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); await testRunner.ExecuteTestAndOutputResultAsync(0); var coverageInfo = testRunner.GetCoverageInfo(); Assert.NotNull(coverageInfo); Assert.Equal(new[] { "foo" }, coverageInfo.EndpointsCovered); Assert.Equal(new[] { "GetNow" }, coverageInfo.EndpointsNotCovered); }
public async Task PassingTestCase() { var testcase = new NetmockeryTestCase { RequestPath = "/statuscode/404", ExpectedStatusCode = 404 }; Assert.True(testcase.HasExpectations); var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory("examples/example1"), handleErrors: false); Assert.True(result.OK); }
private void InitializeEndpointCollectionWithGlobalDefaultsOnly() { var jsonEndpoint1 = new JSONEndpoint { name = "foobar", pathregex = "foobar", responses = new[] { new JSONResponse { match = new JSONRequestMatcher(), file = "myfile.xml" } } }; var jsonEndpoint2 = new JSONEndpoint { name = "baz", pathregex = "baz", responses = new[] { new JSONResponse { match = new JSONRequestMatcher(), file = "myfile.xml", contenttype = "text/xml", charset = "utf-8" } } }; var jsonEndpoint3 = new JSONEndpoint { name = "lorem", pathregex = "ipsum", responses = new[] { new JSONResponse { match = new JSONRequestMatcher(), file = "myfile.xml", contenttype = "text/xml", statuscode = "200" } } }; directoryCreator.AddFile("defaults.json", JsonConvert.SerializeObject(new JSONDefaults { charset = "ascii", contenttype = "application/xml" })); directoryCreator.AddFile("endpoint1/endpoint.json", JsonConvert.SerializeObject(jsonEndpoint1)); directoryCreator.AddFile("endpoint2/endpoint.json", JsonConvert.SerializeObject(jsonEndpoint2)); directoryCreator.AddFile("endpoint3/endpoint.json", JsonConvert.SerializeObject(jsonEndpoint3)); endpointCollection = EndpointCollectionReader.ReadFromDirectory(directoryCreator.DirectoryName); }
async public void CanCheckExpectedResponseCreatorSuccess() { var testcase = (new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "this is a test", expectedresponsecreator = "File content.txt" }) .Validated().CreateTestCase("."); Assert.Equal("File content.txt", testcase.ExpectedResponseCreator); var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); Assert.True(result.OK, result.Message); Assert.Null(result.Message); }
private EndpointCollection CreateEndpointWithScript() { dc.AddFile("endpoint/endpoint.json", JsonConvert.SerializeObject(DataUtils.CreateScriptEndpoint("endpoint", "script.csscript"))); dc.AddFile("endpoint/script.csscript", "return GetParam(\"greeting\") + \" \" + GetParam(\"subject\");"); dc.AddFile("endpoint/params.json", JsonConvert.SerializeObject(new[] { new JSONParam { name = "greeting", @default = "Hello", description = "foo" }, new JSONParam { name = "subject", @default = "World", description = "foo" }, })); return(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); }
public async Task FailingTestCase() { Assert.False(new NetmockeryTestCase().HasExpectations); var testcase = new NetmockeryTestCase { RequestPath = "/statuscode/200", ExpectedStatusCode = 404 }; Assert.True(testcase.HasExpectations); var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory("examples/example1"), handleErrors: false); Assert.False(result.OK); Assert.Equal("Expected http status code: 404\nActual: 200", result.Message); }
async public void RequestBodyCanBeUnspecified() { var endpointCollection = EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName); var test = new NetmockeryTestCase { RequestPath = "/foo/", ExpectedRequestMatcher = "Any request", ExpectedResponseBody = "Hello world" }; var result = await test.ExecuteAsync(endpointCollection, false); Assert.True(result.OK, result.Message); }
async public void CanCheckExpectedResponseCreatorError() { var testcase = (new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedresponsecreator = "File content.txt" }) .Validated().CreateTestCase("."); Assert.True(testcase.HasExpectations); Assert.False(testcase.NeedsResponseBody); Assert.Equal("File content.txt", testcase.ExpectedResponseCreator); var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); Assert.True(result.Error); Assert.Equal("Expected response creator: File content.txt\nActual: Execute script myscript.csscript", result.Message); }
async public Task CanCheckExpectedCharSetError() { var testcase = (new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedcharset = "utf-8" }) .Validated().CreateTestCase("."); Assert.Equal("utf-8", testcase.ExpectedCharSet); var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); Assert.Null(result.Exception); Assert.True(result.Error); Assert.Equal("Expected charset: 'utf-8'\nActual: 'us-ascii'", result.Message); }
async public Task CanCheckExpectedContentTypeSuccess() { var testcase = (new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedcontenttype = "text/xml" }) .Validated().CreateTestCase("."); Assert.True(testcase.HasExpectations); Assert.True(testcase.NeedsResponseBody); Assert.Equal("text/xml", testcase.ExpectedContentType); var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); Assert.True(result.OK); }
async public Task CanCheckExpectedContentTypeError() { var testcase = (new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedcontenttype = "text/plain" }) .Validated().CreateTestCase("."); Assert.Equal("text/plain", testcase.ExpectedContentType); var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); Assert.Null(result.Exception); Assert.True(result.Error); Assert.Equal("Expected contenttype: 'text/plain'\nActual: 'text/xml'", result.Message); }
public void EndpointsInitializedCorrectly() { var endpointCollection = EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName); Assert.Equal(dc.DirectoryName, endpointCollection.SourceDirectory); Assert.Equal(2, endpointCollection.Endpoints.Count()); var ep0 = endpointCollection.Get("Endpoint1"); Assert.Equal("Endpoint1", ep0.Name); Assert.Equal("^/ep1/", ep0.PathRegex); Assert.Equal(0, ep0.Responses.Count()); Assert.Equal(Path.Combine(dc.DirectoryName, "endpoint1"), ep0.Directory); var ep1 = endpointCollection.Get("foo"); Assert.Equal(2, ep1.Responses.Count()); }
async public Task CanCheckExpectedCharSetSuccess() { var testcase = (new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedcharset = "us-ascii" }) .Validated().CreateTestCase("."); Assert.True(testcase.HasExpectations); Assert.True(testcase.NeedsResponseBody); Assert.Equal("us-ascii", testcase.ExpectedCharSet); var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); Assert.Null(result.Exception); Assert.Equal(null, result.Message); Assert.True(result.OK); }
private void InitializeEndpointCollectionWithoutDefaults() { var jsonEndpoint1 = new JSONEndpoint { name = "foobar", pathregex = "foobar", responses = new[] { new JSONResponse { match = new JSONRequestMatcher(), file = "myfile.xml" } } }; directoryCreator.AddFile("endpoint1/endpoint.json", JsonConvert.SerializeObject(jsonEndpoint1)); endpointCollection = EndpointCollectionReader.ReadFromDirectory(directoryCreator.DirectoryName); }
async public void CanCheckExpectedRequestMatcherError() { var testcase = (new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedrequestmatcher = "Regex 'test'" }) .Validated().CreateTestCase("."); Assert.True(testcase.HasExpectations); Assert.False(testcase.NeedsResponseBody); Assert.Equal("Regex 'test'", testcase.ExpectedRequestMatcher); Assert.Equal("foobar", testcase.RequestBody); var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName)); Assert.True(result.Error); Assert.Null(result.Exception); Assert.Equal("Expected request matcher: Regex 'test'\nActual: Any request", result.Message); }
private void InitializeEndpointCollectionWithGlobalAndEndpointDefaults() { var jsonEndpoint1 = new JSONEndpoint { name = "noendpointdefaults", pathregex = "noendpointdefaults", responses = new[] { new JSONResponse { match = new JSONRequestMatcher(), file = "myfile.xml" } } }; var jsonEndpoint2 = new JSONEndpoint { name = "endpointdefaults", pathregex = "endpointdefaults", responses = new[] { new JSONResponse { match = new JSONRequestMatcher(), file = "myfile.xml", } } }; var globalDefaults = new JSONDefaults { charset = "ascii", contenttype = "application/xml" }; var endpointDefaults = new JSONDefaults { charset = "UTF-7", contenttype = "text/plain" }; directoryCreator.AddFile("defaults.json", JsonConvert.SerializeObject(globalDefaults)); directoryCreator.AddFile("endpoint1/endpoint.json", JsonConvert.SerializeObject(jsonEndpoint1)); directoryCreator.AddFile("endpoint2/endpoint.json", JsonConvert.SerializeObject(jsonEndpoint2)); directoryCreator.AddFile("endpoint2/defaults.json", JsonConvert.SerializeObject(endpointDefaults)); endpointCollection = EndpointCollectionReader.ReadFromDirectory(directoryCreator.DirectoryName); }
private async Task CheckOutputAsync(string directory, int index) { var testRunner = new ConsoleTestRunner(EndpointCollectionReader.ReadFromDirectory(directory)); await testRunner.ShowResponseAsync(index); }