Skip to content

Commit 501602a

Browse files
committed
Test framework WiP
1 parent 52bcefd commit 501602a

File tree

4 files changed

+79
-12
lines changed

4 files changed

+79
-12
lines changed

netmockery/ForwardResponseCreator.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.AspNetCore.Http;
1010
using System.Net.Http.Headers;
1111
using System.Text.RegularExpressions;
12+
using System.Diagnostics;
1213
//using Microsoft.Net.Http.Headers;
1314

1415
namespace netmockery
@@ -44,7 +45,8 @@ public override async Task<byte[]> CreateResponseAsync(IHttpRequestWrapper reque
4445
requestPath = Regex.Replace(requestPath, StripPath, "");
4546
}
4647
var httpMsg = new HttpRequestMessage(HttpMethod.Post, Url + requestPath);
47-
48+
49+
Debug.Assert(request.Headers != null);
4850
foreach (var header in request.Headers)
4951
{
5052
if (! HEADERS_TO_SKIP.Contains(header.Key.ToLower()))
@@ -53,8 +55,11 @@ public override async Task<byte[]> CreateResponseAsync(IHttpRequestWrapper reque
5355
}
5456
}
5557
httpMsg.Content = new ByteArrayContent(body);
56-
httpMsg.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(request.Headers["Content-Type"]);
57-
58+
if ((string) request.Headers["Content-Type"] != null)
59+
{
60+
httpMsg.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(request.Headers["Content-Type"]);
61+
}
62+
5863
var httpClient =
5964
ProxyUrl == null ?
6065
new HttpClient() :

netmockery/JSONReader.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ public static Endpoint ReadEndpoint(string jsonString, string rootDir)
1919
public class JSONTest
2020
{
2121
/*
22-
{
23-
'name': '/foo/ request works',
24-
'requestpath': '/foo/',
25-
'requestbody': 'heisann test',
26-
'expectedresponsebody': 'FOOBARBOOBAR'
27-
}
28-
22+
* Typer tester vi boer stoette:
23+
* - expectedendpointname
24+
* - expectedrequestmatcher
2925
*/
3026
public string name;
3127
public string requestpath;
@@ -34,7 +30,11 @@ public class JSONTest
3430

3531
public NetmockeryTestCase CreateTestCase()
3632
{
37-
return new NetmockeryTestCase { Name = name, RequestPath = requestpath, RequestBody = requestbody, ExpectedResponseBody = expectedresponsebody };
33+
if (requestpath == null)
34+
{
35+
throw new ArgumentNullException(nameof(requestpath));
36+
}
37+
return new NetmockeryTestCase { Name = name, RequestPath = requestpath, RequestBody = requestbody ?? "", ExpectedResponseBody = expectedresponsebody };
3838
}
3939
}
4040

netmockery/NetmockeryTestCase.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace netmockery
1212
public class TestCaseHttpRequest : IHttpRequestWrapper
1313
{
1414
private string path;
15-
private HeaderDictionary headerDictionary;
15+
private HeaderDictionary headerDictionary = new HeaderDictionary();
1616

1717
public TestCaseHttpRequest(string path)
1818
{
@@ -100,6 +100,11 @@ async public Task<NetmockeryTestCaseResult> ExecuteAsync(EndpointCollection endp
100100
var responseBody = Encoding.UTF8.GetString(responseBodyBytes);
101101
retval.OK = responseBody == ExpectedResponseBody;
102102
retval.Error = !retval.OK;
103+
104+
if (retval.Error)
105+
{
106+
retval.Message = $"Expected response body:\n{ExpectedResponseBody}\n\nActual response body:\n{responseBody}";
107+
}
103108
}
104109
else
105110
{
@@ -125,5 +130,32 @@ public class NetmockeryTestCaseResult
125130
public string Message;
126131
public Exception Exception;
127132
public NetmockeryTestCase TestCase;
133+
134+
public string ResultAsString
135+
{
136+
get
137+
{
138+
var shortstatus = "";
139+
if (OK)
140+
{
141+
shortstatus = "OK";
142+
}
143+
if (Error)
144+
{
145+
shortstatus = "Fail";
146+
}
147+
if (Exception != null)
148+
{
149+
shortstatus = "Error";
150+
}
151+
152+
var retval = $"{shortstatus}\n{Message}";
153+
if (Exception != null)
154+
{
155+
retval += "\n" + Exception.ToString();
156+
}
157+
return retval;
158+
}
159+
}
128160
}
129161
}

netmockery/Program.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public static void Main(string[] args)
6363
ViewScript(commandArgs);
6464
break;
6565

66+
case "test":
67+
Test(commandArgs);
68+
break;
69+
6670
default:
6771
Error.WriteLine($"Unknown command {commandName}");
6872
break;
@@ -76,6 +80,32 @@ public static void Main(string[] args)
7680
}
7781

7882

83+
public static void Test(string[] commandArgs)
84+
{
85+
if (EndpointTestDefinition.HasTestSuite(EndpointCollection.SourceDirectory))
86+
{
87+
var testDefinitions = EndpointTestDefinition.ReadFromDirectory(EndpointCollection.SourceDirectory);
88+
var errors = 0;
89+
foreach (var test in testDefinitions.Tests)
90+
{
91+
Write(test.Name.PadRight(40));
92+
var result = test.ExecuteAsync(EndpointCollection).Result;
93+
WriteLine(result.ResultAsString);
94+
if (result.Error)
95+
{
96+
errors++;
97+
}
98+
}
99+
WriteLine();
100+
WriteLine($"Total: {testDefinitions.Tests.Count()} Errors: {errors}");
101+
}
102+
else
103+
{
104+
Error.WriteLine("ERROR: No test suite found");
105+
}
106+
}
107+
108+
79109
public static void RunScript(string[] commandArgs)
80110
{
81111
var scriptfile = commandArgs[0];

0 commit comments

Comments
 (0)