Skip to content

Commit 097c8f3

Browse files
committed
Responses can specify HTTP status codes (other than 200)
Merge branch 'ArneBK-httpStatusCode'
2 parents a10fcd8 + bbcafc5 commit 097c8f3

File tree

14 files changed

+258
-58
lines changed

14 files changed

+258
-58
lines changed

UnitTests/IntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void CheckConfigdirectory(string directory)
6161
foreach (var test in tests.Tests)
6262
{
6363
output.WriteLine(test.Name);
64-
var result = test.ExecuteAsync(endpointCollection, now: tests.Now).Result;
64+
var result = test.Execute(endpointCollection, now: tests.Now);
6565
output.WriteLine(result.ResultAsString);
6666
Assert.True(result.OK, $"Test case {result.TestCase.Name}, message '{result.Message}'");
6767
}

UnitTests/TestDefaults.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading.Tasks;
66
using netmockery;
77
using Xunit;
8+
using System.Net;
89

910
namespace UnitTests
1011
{
@@ -60,9 +61,24 @@ public void InitializeEndpointCollectionWithGlobalDefaultsOnly()
6061
}
6162
};
6263

64+
var jsonEndpoint3 = new JSONEndpoint
65+
{
66+
name = "lorem",
67+
pathregex = "ipsum",
68+
responses = new[] {
69+
new JSONResponse {
70+
match = new JSONRequestMatcher(),
71+
file = "myfile.xml",
72+
contenttype = "text/xml",
73+
statuscode = 200
74+
}
75+
}
76+
};
77+
6378
directoryCreator.AddFile("defaults.json", JsonConvert.SerializeObject(new JSONDefaults { charset = "ascii", contenttype = "application/xml" }));
6479
directoryCreator.AddFile("endpoint1\\endpoint.json", JsonConvert.SerializeObject(jsonEndpoint1));
6580
directoryCreator.AddFile("endpoint2\\endpoint.json", JsonConvert.SerializeObject(jsonEndpoint2));
81+
directoryCreator.AddFile("endpoint3\\endpoint.json", JsonConvert.SerializeObject(jsonEndpoint3));
6682

6783
endpointCollection = EndpointCollectionReader.ReadFromDirectory(directoryCreator.DirectoryName);
6884
}
@@ -149,5 +165,14 @@ public void ValidateDefaultEncodingWithoutDefaults()
149165
var responseCreator = endpointCollection.Get("foobar").Responses.Single().Item2 as SimpleResponseCreator;
150166
Assert.Equal("utf-8", responseCreator.Encoding.WebName);
151167
}
168+
169+
[Fact]
170+
public void ValidateDefaultHttpResponseCode()
171+
{
172+
InitializeEndpointCollectionWithGlobalDefaultsOnly();
173+
174+
var responseCreator = endpointCollection.Get("lorem").Responses.Single().Item2 as SimpleResponseCreator;
175+
Assert.Equal(200, responseCreator.StatusCode);
176+
}
152177
}
153178
}

UnitTests/TestStatusCode.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
using netmockery;
7+
using System.Net;
8+
9+
namespace UnitTests
10+
{
11+
public class TestStatusCode
12+
{
13+
[Fact]
14+
public void DefaultIs200()
15+
{
16+
var responseCreator = CreateJsonResponse().Validated().CreateResponseCreator(".") as SimpleResponseCreator;
17+
Assert.Equal(200, responseCreator.StatusCode);
18+
}
19+
20+
[Fact]
21+
public void CanSetStatusCode()
22+
{
23+
var responseCreator = CreateJsonResponse(404).Validated().CreateResponseCreator(".") as SimpleResponseCreator;
24+
Assert.NotEqual(200, responseCreator.StatusCode);
25+
Assert.Equal(404, responseCreator.StatusCode);
26+
}
27+
28+
[Fact]
29+
public void CanUseCustomCodes()
30+
{
31+
var responseCreator = CreateJsonResponse(422).Validated().CreateResponseCreator(".") as SimpleResponseCreator;
32+
Assert.NotEqual(200, responseCreator.StatusCode);
33+
Assert.Equal(422, responseCreator.StatusCode);
34+
}
35+
36+
[Fact]
37+
public void ReadsFromEndpointJson()
38+
{
39+
var endpointCollection = EndpointCollectionReader.ReadFromDirectory("examples\\example1");
40+
41+
var endpoint3 = endpointCollection.Resolve("/statuscode");
42+
Assert.Equal("StatusCodes", endpoint3.Name);
43+
44+
var responseCreators = from t in endpoint3.Responses select (SimpleResponseCreator)t.Item2;
45+
Assert.All(responseCreators, r => Assert.Equal("text/plain", r.ContentType));
46+
Assert.Equal(new[] { 200, 404, 499 }, from r in responseCreators select (int) r.StatusCode);
47+
}
48+
49+
[Fact]
50+
public void FailingTestCase()
51+
{
52+
Assert.False(new NetmockeryTestCase().HasExpectations);
53+
54+
var testcase = new NetmockeryTestCase
55+
{
56+
RequestPath = "/statuscode/200",
57+
ExpectedStatusCode = 404
58+
};
59+
60+
Assert.True(testcase.HasExpectations);
61+
var result = testcase.Execute(EndpointCollectionReader.ReadFromDirectory("examples\\example1"), handleErrors: false);
62+
Assert.False(result.OK);
63+
Assert.Equal("Expected http status code: 404\nActual: 200", result.Message);
64+
}
65+
66+
[Fact]
67+
public void PassingTestCase()
68+
{
69+
var testcase = new NetmockeryTestCase
70+
{
71+
RequestPath = "/statuscode/404",
72+
ExpectedStatusCode = 404
73+
};
74+
75+
Assert.True(testcase.HasExpectations);
76+
var result = testcase.Execute(EndpointCollectionReader.ReadFromDirectory("examples\\example1"), handleErrors: false);
77+
Assert.True(result.OK);
78+
}
79+
80+
JSONResponse CreateJsonResponse(int statuscode = -1)
81+
{
82+
var retval = new JSONResponse
83+
{
84+
match = new JSONRequestMatcher(),
85+
literal = "Heisann"
86+
};
87+
88+
if (statuscode != -1)
89+
{
90+
retval.statuscode = statuscode;
91+
}
92+
93+
return retval;
94+
}
95+
}
96+
}

UnitTests/TestTestCommand.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void CanReadTestsFromJSONFile()
154154
}
155155

156156
[Fact]
157-
async public void RequestBodyCanBeUnspecified()
157+
public void RequestBodyCanBeUnspecified()
158158
{
159159
var endpointCollection = EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName);
160160

@@ -165,7 +165,7 @@ async public void RequestBodyCanBeUnspecified()
165165
ExpectedResponseBody = "Hello world"
166166
};
167167

168-
var result = await test.ExecuteAsync(endpointCollection, false);
168+
var result = test.Execute(endpointCollection, false);
169169
Assert.True(result.OK, result.Message);
170170
}
171171

@@ -243,30 +243,30 @@ public void TestRunnerKeepsTrackOfCoverageWhenRunningSingleTest()
243243
}
244244

245245
[Fact]
246-
async public void CanExecuteTest()
246+
public void CanExecuteTest()
247247
{
248248
var endpointCollection = EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName);
249249
var testRunner = new ConsoleTestRunner(endpointCollection);
250250

251251
var test = testRunner.Tests.ElementAt(0);
252-
var result = await test.ExecuteAsync(endpointCollection, handleErrors: false);
252+
var result = test.Execute(endpointCollection, handleErrors: false);
253253
Assert.True(result.OK);
254254
}
255255

256256
[Fact]
257-
async public void CanReadExpectedResponseBodyFromFile()
257+
public void CanReadExpectedResponseBodyFromFile()
258258
{
259259
var endpointCollection = EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName);
260260
var testRunner = new ConsoleTestRunner(endpointCollection);
261261

262262
var test = testRunner.Tests.ElementAt(2);
263263

264-
var result = await test.ExecuteAsync(endpointCollection, handleErrors: false);
264+
var result = test.Execute(endpointCollection, handleErrors: false);
265265
Assert.True(result.OK, result.Message);
266266
}
267267

268268
[Fact]
269-
async public void CanCheckExpectedRequestMatcherError()
269+
public void CanCheckExpectedRequestMatcherError()
270270
{
271271
var testcase =
272272
(new JSONTest { name="checksomething", requestpath = "/foo/", requestbody = "foobar", expectedrequestmatcher = "Regex 'test'" })
@@ -277,24 +277,24 @@ async public void CanCheckExpectedRequestMatcherError()
277277
Assert.Equal("Regex 'test'", testcase.ExpectedRequestMatcher);
278278
Assert.Equal("foobar", testcase.RequestBody);
279279

280-
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
280+
var result = testcase.Execute(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
281281
Assert.True(result.Error);
282282
Assert.Null(result.Exception);
283283
Assert.Equal("Expected request matcher: Regex 'test'\nActual: Any request", result.Message);
284284
}
285285

286286
[Fact]
287-
async public void CanCheckExpectedRequestMatcherSuccess()
287+
public void CanCheckExpectedRequestMatcherSuccess()
288288
{
289289
var testcase =
290290
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "this is a test", expectedrequestmatcher = "Regex 'test'" })
291291
.Validated().CreateTestCase(".");
292-
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
292+
var result = testcase.Execute(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
293293
Assert.True(result.OK);
294294
}
295295

296296
[Fact]
297-
async public Task CanCheckExpectedContentTypeSuccess()
297+
public void CanCheckExpectedContentTypeSuccess()
298298
{
299299
var testcase =
300300
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedcontenttype = "text/xml" })
@@ -304,27 +304,27 @@ async public Task CanCheckExpectedContentTypeSuccess()
304304
Assert.True(testcase.NeedsResponseBody);
305305
Assert.Equal("text/xml", testcase.ExpectedContentType);
306306

307-
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
307+
var result = testcase.Execute(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
308308
Assert.True(result.OK);
309309
}
310310

311311
[Fact]
312-
async public Task CanCheckExpectedContentTypeError()
312+
public void CanCheckExpectedContentTypeError()
313313
{
314314
var testcase =
315315
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedcontenttype = "text/plain" })
316316
.Validated().CreateTestCase(".");
317317

318318
Assert.Equal("text/plain", testcase.ExpectedContentType);
319319

320-
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
320+
var result = testcase.Execute(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
321321
Assert.Null(result.Exception);
322322
Assert.True(result.Error);
323323
Assert.Equal("Expected contenttype: 'text/plain'\nActual: 'text/xml'", result.Message);
324324
}
325325

326326
[Fact]
327-
async public Task CanCheckExpectedCharSetSuccess()
327+
public void CanCheckExpectedCharSetSuccess()
328328
{
329329
var testcase =
330330
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedcharset = "us-ascii" })
@@ -334,22 +334,22 @@ async public Task CanCheckExpectedCharSetSuccess()
334334
Assert.True(testcase.NeedsResponseBody);
335335
Assert.Equal("us-ascii", testcase.ExpectedCharSet);
336336

337-
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
337+
var result = testcase.Execute(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
338338
Assert.Null(result.Exception);
339339
Assert.Equal(null, result.Message);
340340
Assert.True(result.OK);
341341
}
342342

343343
[Fact]
344-
async public Task CanCheckExpectedCharSetError()
344+
public void CanCheckExpectedCharSetError()
345345
{
346346
var testcase =
347347
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedcharset = "utf-8" })
348348
.Validated().CreateTestCase(".");
349349

350350
Assert.Equal("utf-8", testcase.ExpectedCharSet);
351351

352-
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
352+
var result = testcase.Execute(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
353353
Assert.Null(result.Exception);
354354
Assert.True(result.Error);
355355
Assert.Equal("Expected charset: 'utf-8'\nActual: 'us-ascii'", result.Message);
@@ -367,27 +367,27 @@ public void CanGetResultBody()
367367
}
368368

369369
[Fact]
370-
async public void CanCheckExpectedResponseCreatorError()
370+
public void CanCheckExpectedResponseCreatorError()
371371
{
372372
var testcase =
373373
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedresponsecreator = "File content.txt" })
374374
.Validated().CreateTestCase(".");
375375
Assert.True(testcase.HasExpectations);
376376
Assert.False(testcase.NeedsResponseBody);
377377
Assert.Equal("File content.txt", testcase.ExpectedResponseCreator);
378-
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
378+
var result = testcase.Execute(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
379379
Assert.True(result.Error);
380380
Assert.Equal("Expected response creator: File content.txt\nActual: Execute script myscript.csscript", result.Message);
381381
}
382382

383383
[Fact]
384-
async public void CanCheckExpectedResponseCreatorSuccess()
384+
public void CanCheckExpectedResponseCreatorSuccess()
385385
{
386386
var testcase =
387387
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "this is a test", expectedresponsecreator = "File content.txt" })
388388
.Validated().CreateTestCase(".");
389389
Assert.Equal("File content.txt", testcase.ExpectedResponseCreator);
390-
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
390+
var result = testcase.Execute(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
391391
Assert.True(result.OK, result.Message);
392392
Assert.Null(result.Message);
393393
}
@@ -400,12 +400,12 @@ public void CanReadQueryString()
400400
}
401401

402402
[Fact]
403-
async public void CanExecuteWithQueryStringFailure()
403+
public void CanExecuteWithQueryStringFailure()
404404
{
405405
var testcase =
406406
(new JSONTest { name = "checksomething", requestpath = "/foo/", querystring = "?a=test", requestbody = "foobar", expectedresponsecreator = "File content.txt" })
407407
.Validated().CreateTestCase(".");
408-
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
408+
var result = testcase.Execute(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
409409
Assert.True(result.OK, result.Message);
410410
Assert.Null(result.Message);
411411
}

UnitTests/TestWebUI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public async Task HomeRedirectsToEndpoints()
4545
Assert.Equal("/__netmockery", response.Headers.Location.ToString());
4646
}
4747

48-
const int EXPECTED_NUMBER_OF_URLS = 19;
48+
const int EXPECTED_NUMBER_OF_URLS = 22;
4949
const int EXPECTED_EXTRA_URLS_PER_REQUEST = 2;
5050

5151
[Fact]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contenttype": "text/plain"
3+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "StatusCodes",
3+
"pathregex": "/statuscode",
4+
5+
"responses": [
6+
{
7+
"match": { "regex": "200" },
8+
"literal": "Hello world with 200"
9+
},
10+
11+
{
12+
"match": { "regex": "404" },
13+
"literal": "Hello world with 404",
14+
"statuscode": 404
15+
},
16+
17+
{
18+
"match": { "regex": "499" },
19+
"literal": "Hello world with 499",
20+
"statuscode": 499
21+
}
22+
]
23+
}

netmockery/ForwardResponseCreator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public override async Task<byte[]> CreateResponseAsync(IHttpRequestWrapper reque
6868
var responseMessage = await httpClient.SendAsync(httpMsg);
6969

7070
response.ContentType = responseMessage.Content.Headers.ContentType.ToString();
71+
response.HttpStatusCode = responseMessage.StatusCode;
7172
var responseBodyStream = await responseMessage.Content.ReadAsStreamAsync();
7273
var memoryStream = new MemoryStream();
7374
responseBodyStream.CopyTo(memoryStream);

0 commit comments

Comments
 (0)