|
| 1 | +using Newtonsoft.Json; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using netmockery; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace UnitTests |
| 10 | +{ |
| 11 | + public class TestDefaults : IDisposable |
| 12 | + { |
| 13 | + DirectoryCreator directoryCreator = new DirectoryCreator(); |
| 14 | + EndpointCollection endpointCollection; |
| 15 | + |
| 16 | + public void InitializeEndpointCollectionWithoutDefaults() |
| 17 | + { |
| 18 | + var jsonEndpoint1 = new JSONEndpoint |
| 19 | + { |
| 20 | + name = "foobar", |
| 21 | + pathregex = "foobar", |
| 22 | + responses = new[] { |
| 23 | + new JSONResponse { |
| 24 | + match = new JSONRequestMatcher(), |
| 25 | + file = "myfile.xml" |
| 26 | + } |
| 27 | + } |
| 28 | + }; |
| 29 | + |
| 30 | + directoryCreator.AddFile("endpoint1\\endpoint.json", JsonConvert.SerializeObject(jsonEndpoint1)); |
| 31 | + |
| 32 | + endpointCollection = EndpointCollectionReader.ReadFromDirectory(directoryCreator.DirectoryName); |
| 33 | + } |
| 34 | + |
| 35 | + public void InitializeEndpointCollectionWithGlobalDefaultsOnly() |
| 36 | + { |
| 37 | + var jsonEndpoint1 = new JSONEndpoint |
| 38 | + { |
| 39 | + name = "foobar", |
| 40 | + pathregex = "foobar", |
| 41 | + responses = new[] { |
| 42 | + new JSONResponse { |
| 43 | + match = new JSONRequestMatcher(), |
| 44 | + file = "myfile.xml" |
| 45 | + } |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + var jsonEndpoint2 = new JSONEndpoint |
| 50 | + { |
| 51 | + name = "baz", |
| 52 | + pathregex = "baz", |
| 53 | + responses = new[] { |
| 54 | + new JSONResponse { |
| 55 | + match = new JSONRequestMatcher(), |
| 56 | + file = "myfile.xml", |
| 57 | + contenttype = "text/xml", |
| 58 | + charset = "utf-8" |
| 59 | + } |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + directoryCreator.AddFile("defaults.json", JsonConvert.SerializeObject(new JSONDefaults { charset = "ascii", contenttype = "application/xml" })); |
| 64 | + directoryCreator.AddFile("endpoint1\\endpoint.json", JsonConvert.SerializeObject(jsonEndpoint1)); |
| 65 | + directoryCreator.AddFile("endpoint2\\endpoint.json", JsonConvert.SerializeObject(jsonEndpoint2)); |
| 66 | + |
| 67 | + endpointCollection = EndpointCollectionReader.ReadFromDirectory(directoryCreator.DirectoryName); |
| 68 | + } |
| 69 | + |
| 70 | + public void InitializeEndpointCollectionWithGlobalAndEndpointDefaults() |
| 71 | + { |
| 72 | + var jsonEndpoint1 = new JSONEndpoint |
| 73 | + { |
| 74 | + name = "noendpointdefaults", |
| 75 | + pathregex = "noendpointdefaults", |
| 76 | + responses = new[] { |
| 77 | + new JSONResponse { |
| 78 | + match = new JSONRequestMatcher(), |
| 79 | + file = "myfile.xml" |
| 80 | + } |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + var jsonEndpoint2 = new JSONEndpoint |
| 85 | + { |
| 86 | + name = "endpointdefaults", |
| 87 | + pathregex = "endpointdefaults", |
| 88 | + responses = new[] { |
| 89 | + new JSONResponse { |
| 90 | + match = new JSONRequestMatcher(), |
| 91 | + file = "myfile.xml", |
| 92 | + } |
| 93 | + } |
| 94 | + }; |
| 95 | + var globalDefaults = new JSONDefaults { charset = "ascii", contenttype = "application/xml" }; |
| 96 | + var endpointDefaults = new JSONDefaults { charset = "UTF-7", contenttype = "text/plain" }; |
| 97 | + |
| 98 | + directoryCreator.AddFile("defaults.json", JsonConvert.SerializeObject(globalDefaults)); |
| 99 | + directoryCreator.AddFile("endpoint1\\endpoint.json", JsonConvert.SerializeObject(jsonEndpoint1)); |
| 100 | + directoryCreator.AddFile("endpoint2\\endpoint.json", JsonConvert.SerializeObject(jsonEndpoint2)); |
| 101 | + directoryCreator.AddFile("endpoint2\\defaults.json", JsonConvert.SerializeObject(endpointDefaults)); |
| 102 | + |
| 103 | + endpointCollection = EndpointCollectionReader.ReadFromDirectory(directoryCreator.DirectoryName); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + public void Dispose() |
| 108 | + { |
| 109 | + directoryCreator.Dispose(); |
| 110 | + } |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public void EndpointDefaultsAreApplied() |
| 114 | + { |
| 115 | + InitializeEndpointCollectionWithGlobalAndEndpointDefaults(); |
| 116 | + |
| 117 | + var responseCreator = endpointCollection.Get("endpointdefaults").Responses.Single().Item2 as SimpleResponseCreator; |
| 118 | + Assert.Equal("text/plain", responseCreator.ContentType); |
| 119 | + Assert.Equal("utf-7", responseCreator.Encoding.WebName); |
| 120 | + } |
| 121 | + |
| 122 | + [Fact] |
| 123 | + public void GlobalDefaultsAreApplied() |
| 124 | + { |
| 125 | + InitializeEndpointCollectionWithGlobalDefaultsOnly(); |
| 126 | + |
| 127 | + var endpoint = endpointCollection.Get("foobar"); |
| 128 | + var responseCreator = endpoint.Responses.Single().Item2 as SimpleResponseCreator; |
| 129 | + Assert.Equal("application/xml", responseCreator.ContentType); |
| 130 | + Assert.Equal("us-ascii", responseCreator.Encoding.WebName); |
| 131 | + } |
| 132 | + |
| 133 | + [Fact] |
| 134 | + public void DefaultsCanBeOverridden() |
| 135 | + { |
| 136 | + InitializeEndpointCollectionWithGlobalDefaultsOnly(); |
| 137 | + |
| 138 | + var endpoint = endpointCollection.Get("baz"); |
| 139 | + var responseCreator = endpoint.Responses.Single().Item2 as SimpleResponseCreator; |
| 140 | + Assert.Equal("text/xml", responseCreator.ContentType); |
| 141 | + Assert.Equal("utf-8", responseCreator.Encoding.WebName); |
| 142 | + } |
| 143 | + |
| 144 | + [Fact] |
| 145 | + public void ValidateDefaultEncodingWithoutDefaults() |
| 146 | + { |
| 147 | + InitializeEndpointCollectionWithoutDefaults(); |
| 148 | + |
| 149 | + var responseCreator = endpointCollection.Get("foobar").Responses.Single().Item2 as SimpleResponseCreator; |
| 150 | + Assert.Equal("utf-8", responseCreator.Encoding.WebName); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments