|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Xunit; |
| 6 | +using netmockery; |
| 7 | + |
| 8 | +namespace UnitTests |
| 9 | +{ |
| 10 | + public static class TESTCOMMAND_CONSTANTS |
| 11 | + { |
| 12 | + public const string ENDPOINTJSON = @" |
| 13 | +{ |
| 14 | + 'name': 'foo', |
| 15 | + 'pathregex': '^/foo/$', |
| 16 | + 'responses': [ |
| 17 | + { |
| 18 | + 'match': {'regex': 'test'}, |
| 19 | + 'response': { |
| 20 | + 'file': 'content.txt', |
| 21 | + 'contenttype': 'text/plain' |
| 22 | + } |
| 23 | + }, |
| 24 | + { |
| 25 | + 'match': {}, |
| 26 | + 'response': { |
| 27 | + 'script': 'myscript.csscript', |
| 28 | + 'contenttype': 'text/xml', |
| 29 | + 'replacements': [ |
| 30 | + {'search': 'a', 'replace': 'b'}, |
| 31 | + {'search': 'foo', 'replace': 'bar'} |
| 32 | + ] |
| 33 | + } |
| 34 | + } |
| 35 | + ] |
| 36 | +} |
| 37 | +"; |
| 38 | + |
| 39 | + public const string TESTS = @" |
| 40 | +[ |
| 41 | + { |
| 42 | + 'name': '/foo/ request works', |
| 43 | + 'requestpath': '/foo/', |
| 44 | + 'requestbody': 'heisann test', |
| 45 | + 'expectedresponsebody': 'FOOBARBOOBAR' |
| 46 | + } |
| 47 | +] |
| 48 | +"; |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + public class TestTestCommandWithoutTestsuite : IDisposable |
| 53 | + { |
| 54 | + DirectoryCreator dc; |
| 55 | + public TestTestCommandWithoutTestsuite() |
| 56 | + { |
| 57 | + dc = new DirectoryCreator(); |
| 58 | + dc.AddFile("endpoint1\\endpoint.json", TESTCOMMAND_CONSTANTS.ENDPOINTJSON); |
| 59 | + } |
| 60 | + |
| 61 | + public void Dispose() |
| 62 | + { |
| 63 | + dc.Dispose(); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void CheckIfTestSuiteExists() |
| 68 | + { |
| 69 | + Assert.False(EndpointTestDefinition.HasTestSuite(dc.DirectoryName)); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void WorksIfEndpointNamedTest() |
| 74 | + { |
| 75 | + dc.AddFile("tests\\endpoint.json", TESTCOMMAND_CONSTANTS.ENDPOINTJSON); |
| 76 | + Assert.False(EndpointTestDefinition.HasTestSuite(dc.DirectoryName)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + public class TestTestCommand : IDisposable |
| 82 | + { |
| 83 | + DirectoryCreator dc; |
| 84 | + public TestTestCommand() |
| 85 | + { |
| 86 | + dc = new DirectoryCreator(); |
| 87 | + dc.AddFile("endpoint1\\endpoint.json", TESTCOMMAND_CONSTANTS.ENDPOINTJSON); |
| 88 | + dc.AddFile("endpoint1\\content.txt", "FOOBARBOOBAR"); |
| 89 | + dc.AddFile("tests\\tests.json", TESTCOMMAND_CONSTANTS.TESTS); |
| 90 | + } |
| 91 | + |
| 92 | + public void Dispose() |
| 93 | + { |
| 94 | + dc.Dispose(); |
| 95 | + } |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public void DetectsTestSuite() |
| 99 | + { |
| 100 | + Assert.True(EndpointTestDefinition.HasTestSuite(dc.DirectoryName)); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void CanReadTestsFromJSONFile() |
| 106 | + { |
| 107 | + var endpointTestDefinition = EndpointTestDefinition.ReadFromDirectory(dc.DirectoryName); |
| 108 | + Assert.Equal(1, endpointTestDefinition.Tests.Count()); |
| 109 | + var test = endpointTestDefinition.Tests.ElementAt(0); |
| 110 | + Assert.Equal("/foo/ request works", test.Name); |
| 111 | + Assert.Equal("/foo/", test.RequestPath); |
| 112 | + Assert.Equal("heisann test", test.RequestBody); |
| 113 | + Assert.Equal("FOOBARBOOBAR", test.ExpectedResponseBody); |
| 114 | + } |
| 115 | + |
| 116 | + [Fact] |
| 117 | + async public void CanExecuteTest() |
| 118 | + { |
| 119 | + var endpointTestDefinition = EndpointTestDefinition.ReadFromDirectory(dc.DirectoryName); |
| 120 | + var test = endpointTestDefinition.Tests.ElementAt(0); |
| 121 | + |
| 122 | + var result = await test.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName), handleErrors: false); |
| 123 | + Assert.True(result.OK); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments