|
| 1 | +using QAToolKit.Engine.HttpTester.Extensions; |
| 2 | +using QAToolKit.Engine.HttpTester.Test.Fixtures; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Net; |
| 7 | +using System.Net.Http; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace QAToolKit.Engine.HttpTester.Test |
| 12 | +{ |
| 13 | + public class HttpTestAsserterTests |
| 14 | + { |
| 15 | + [Fact] |
| 16 | + public async Task HttpTestAsserterSimple_Success() |
| 17 | + { |
| 18 | + using (var client = new HttpTesterClient()) |
| 19 | + { |
| 20 | + var response = await client |
| 21 | + .CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net")) |
| 22 | + .WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } }) |
| 23 | + .WithMethod(HttpMethod.Get) |
| 24 | + .WithPath("/api/bicycles") |
| 25 | + .Start(); |
| 26 | + |
| 27 | + var msg = await response.GetResponseBody<List<Bicycle>>(); |
| 28 | + |
| 29 | + var asserter = new HttpTestAsserter(response); |
| 30 | + |
| 31 | + var duration = client.Duration; |
| 32 | + var assertResults = asserter |
| 33 | + .ResponseContentContains("scott") |
| 34 | + .RequestDurationEquals(duration, (x) => x < 1000) |
| 35 | + .ResponseStatusCodeEquals(HttpStatusCode.OK) |
| 36 | + .AssertAll(); |
| 37 | + |
| 38 | + foreach (var result in assertResults) |
| 39 | + { |
| 40 | + Assert.True(result.IsTrue, result.Message); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public async Task HttpTestAsserterDoesNotContainHeader_Success() |
| 47 | + { |
| 48 | + using (var client = new HttpTesterClient()) |
| 49 | + { |
| 50 | + var response = await client |
| 51 | + .CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net")) |
| 52 | + .WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } }) |
| 53 | + .WithMethod(HttpMethod.Get) |
| 54 | + .WithPath("/api/bicycles") |
| 55 | + .Start(); |
| 56 | + |
| 57 | + var msg = await response.GetResponseBody<List<Bicycle>>(); |
| 58 | + |
| 59 | + var asserter = new HttpTestAsserter(response); |
| 60 | + |
| 61 | + var assertResults = asserter |
| 62 | + .ResponseHasHttpHeader("authentication") |
| 63 | + .AssertAll(); |
| 64 | + |
| 65 | + foreach (var result in assertResults) |
| 66 | + { |
| 67 | + Assert.False(result.IsTrue, result.Message); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public async Task HttpTestAsserterDoesNotContainKeywordInBody_Success() |
| 74 | + { |
| 75 | + using (var client = new HttpTesterClient()) |
| 76 | + { |
| 77 | + var response = await client |
| 78 | + .CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net")) |
| 79 | + .WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } }) |
| 80 | + .WithMethod(HttpMethod.Get) |
| 81 | + .WithPath("/api/bicycles") |
| 82 | + .Start(); |
| 83 | + |
| 84 | + var msg = await response.GetResponseBody<List<Bicycle>>(); |
| 85 | + |
| 86 | + var asserter = new HttpTestAsserter(response); |
| 87 | + |
| 88 | + var assertResults = asserter |
| 89 | + .ResponseContentContains("giant") |
| 90 | + .AssertAll(); |
| 91 | + |
| 92 | + foreach (var result in assertResults) |
| 93 | + { |
| 94 | + Assert.False(result.IsTrue, result.Message); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public async Task HttpTestAsserterHeaderMissing_Fails() |
| 101 | + { |
| 102 | + using (var client = new HttpTesterClient()) |
| 103 | + { |
| 104 | + var response = await client |
| 105 | + .CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net")) |
| 106 | + .WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } }) |
| 107 | + .WithMethod(HttpMethod.Get) |
| 108 | + .WithPath("/api/bicycles") |
| 109 | + .Start(); |
| 110 | + |
| 111 | + var msg = await response.GetResponseBody<List<Bicycle>>(); |
| 112 | + |
| 113 | + var asserter = new HttpTestAsserter(response); |
| 114 | + |
| 115 | + var duration = client.Duration; |
| 116 | + Assert.Throws<ArgumentNullException>(() => asserter |
| 117 | + .ResponseContentContains("scott") |
| 118 | + .RequestDurationEquals(duration, (x) => x < 1000) |
| 119 | + .ResponseStatusCodeEquals(HttpStatusCode.OK) |
| 120 | + .ResponseHasHttpHeader(null) |
| 121 | + .AssertAll()); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public async Task HttpTestAsserterBodyNull_Fails() |
| 127 | + { |
| 128 | + using (var client = new HttpTesterClient()) |
| 129 | + { |
| 130 | + var response = await client |
| 131 | + .CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net")) |
| 132 | + .WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } }) |
| 133 | + .WithMethod(HttpMethod.Get) |
| 134 | + .WithPath("/api/bicycles") |
| 135 | + .Start(); |
| 136 | + |
| 137 | + var msg = await response.GetResponseBody<List<Bicycle>>(); |
| 138 | + |
| 139 | + var asserter = new HttpTestAsserter(response); |
| 140 | + |
| 141 | + var duration = client.Duration; |
| 142 | + Assert.Throws<ArgumentNullException>(() => asserter |
| 143 | + .ResponseContentContains(null) |
| 144 | + .RequestDurationEquals(duration, (x) => x < 1000) |
| 145 | + .ResponseStatusCodeEquals(HttpStatusCode.OK) |
| 146 | + .AssertAll()); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + [Fact] |
| 151 | + public async Task HttpTestAsserterAlternativeDurationPredicate_Success() |
| 152 | + { |
| 153 | + using (var client = new HttpTesterClient()) |
| 154 | + { |
| 155 | + var response = await client |
| 156 | + .CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net")) |
| 157 | + .WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } }) |
| 158 | + .WithMethod(HttpMethod.Get) |
| 159 | + .WithPath("/api/bicycles") |
| 160 | + .Start(); |
| 161 | + |
| 162 | + var msg = await response.GetResponseBody<List<Bicycle>>(); |
| 163 | + |
| 164 | + var asserter = new HttpTestAsserter(response); |
| 165 | + var duration = client.Duration; |
| 166 | + var assertResults = asserter |
| 167 | + .ResponseContentContains("scott") |
| 168 | + .ResponseContentContains("id") |
| 169 | + .RequestDurationEquals(duration, (x) => (x > 100 && x < 1000)) |
| 170 | + .ResponseStatusCodeEquals(HttpStatusCode.OK) |
| 171 | + .AssertAll(); |
| 172 | + |
| 173 | + Assert.Equal(4, assertResults.ToList().Count); |
| 174 | + foreach (var result in assertResults) |
| 175 | + { |
| 176 | + Assert.True(result.IsTrue, result.Message); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments