Skip to content

Commit cc513f6

Browse files
committed
New test options: expectedcharset and expectedcontenttype
1 parent e06d74c commit cc513f6

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

UnitTests/TestTestCommand.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static class TESTCOMMAND_CONSTANTS
3131
'match': {},
3232
'script': 'myscript.csscript',
3333
'contenttype': 'text/xml',
34+
'charset': 'ascii',
3435
'replacements': [
3536
{'search': 'a', 'replace': 'b'},
3637
{'search': 'foo', 'replace': 'bar'}
@@ -292,6 +293,68 @@ async public void CanCheckExpectedRequestMatcherSuccess()
292293
Assert.True(result.OK);
293294
}
294295

296+
[Fact]
297+
async public Task CanCheckExpectedContentTypeSuccess()
298+
{
299+
var testcase =
300+
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedcontenttype = "text/xml" })
301+
.Validated().CreateTestCase(".");
302+
303+
Assert.True(testcase.HasExpectations);
304+
Assert.True(testcase.NeedsResponseBody);
305+
Assert.Equal("text/xml", testcase.ExpectedContentType);
306+
307+
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
308+
Assert.True(result.OK);
309+
}
310+
311+
[Fact]
312+
async public Task CanCheckExpectedContentTypeError()
313+
{
314+
var testcase =
315+
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedcontenttype = "text/plain" })
316+
.Validated().CreateTestCase(".");
317+
318+
Assert.Equal("text/plain", testcase.ExpectedContentType);
319+
320+
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
321+
Assert.Null(result.Exception);
322+
Assert.True(result.Error);
323+
Assert.Equal("Expected contenttype: 'text/plain'\nActual: 'text/xml'", result.Message);
324+
}
325+
326+
[Fact]
327+
async public Task CanCheckExpectedCharSetSuccess()
328+
{
329+
var testcase =
330+
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedcharset = "us-ascii" })
331+
.Validated().CreateTestCase(".");
332+
333+
Assert.True(testcase.HasExpectations);
334+
Assert.True(testcase.NeedsResponseBody);
335+
Assert.Equal("us-ascii", testcase.ExpectedCharSet);
336+
337+
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
338+
Assert.Null(result.Exception);
339+
Assert.Equal(null, result.Message);
340+
Assert.True(result.OK);
341+
}
342+
343+
[Fact]
344+
async public Task CanCheckExpectedCharSetError()
345+
{
346+
var testcase =
347+
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "foobar", expectedcharset = "utf-8" })
348+
.Validated().CreateTestCase(".");
349+
350+
Assert.Equal("utf-8", testcase.ExpectedCharSet);
351+
352+
var result = await testcase.ExecuteAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
353+
Assert.Null(result.Exception);
354+
Assert.True(result.Error);
355+
Assert.Equal("Expected charset: 'utf-8'\nActual: 'us-ascii'", result.Message);
356+
}
357+
295358
[Fact]
296359
public void CanGetResultBody()
297360
{

netmockery/JSONReader.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class JSONTest
2727
public string expectedrequestmatcher;
2828
public string expectedresponsecreator;
2929
public string expectedresponsebody;
30+
public string expectedcontenttype;
31+
public string expectedcharset;
3032

3133
public JSONTest Validated()
3234
{
@@ -58,6 +60,8 @@ public NetmockeryTestCase CreateTestCase(string directory)
5860
ExpectedRequestMatcher = expectedrequestmatcher,
5961
ExpectedResponseCreator = expectedresponsecreator,
6062

63+
ExpectedContentType = expectedcontenttype,
64+
ExpectedCharSet = expectedcharset,
6165
ExpectedResponseBody =
6266
expectedresponsebody != null && expectedresponsebody.StartsWith("file:")
6367
?

netmockery/NetmockeryTestCase.cs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,33 @@ public class NetmockeryTestCase
8080
public string ExpectedRequestMatcher;
8181
public string ExpectedResponseCreator;
8282

83+
public string ExpectedContentType;
84+
public string ExpectedCharSet;
8385
public string ExpectedResponseBody;
8486

8587
public bool NeedsResponseBody
8688
{
8789
get
8890
{
89-
return (new[] { ExpectedResponseBody }).Any(val => val != null);
91+
return (new[] { ExpectedResponseBody, ExpectedContentType, ExpectedCharSet }).Any(val => val != null);
9092
}
9193
}
9294

9395
public bool HasExpectations
9496
{
9597
get
9698
{
97-
return (new[] { ExpectedResponseBody, ExpectedRequestMatcher, ExpectedResponseCreator }).Any(val => val != null);
99+
return (new[] { ExpectedResponseBody, ExpectedRequestMatcher, ExpectedResponseCreator, ExpectedContentType, ExpectedCharSet }).Any(val => val != null);
98100
}
99101
}
100102

101103

102-
public bool Evaluate(string requestMatcher, string responseCreator, string responseBody, out string message)
104+
public bool Evaluate(string requestMatcher, string responseCreator, string responseBody, string contentType, string charset, out string message)
103105
{
104106
Debug.Assert(responseBody != null || !NeedsResponseBody);
107+
Debug.Assert(contentType != null || !NeedsResponseBody);
108+
Debug.Assert(charset != null || !NeedsResponseBody);
109+
105110
Debug.Assert(requestMatcher != null);
106111
Debug.Assert(responseCreator != null);
107112
message = null;
@@ -124,6 +129,18 @@ public bool Evaluate(string requestMatcher, string responseCreator, string respo
124129
return false;
125130
}
126131

132+
if (ExpectedContentType != null && ExpectedContentType != contentType)
133+
{
134+
message = $"Expected contenttype: '{ExpectedContentType}'\nActual: '{contentType}'";
135+
return false;
136+
}
137+
138+
if (ExpectedCharSet != null && ExpectedCharSet != charset)
139+
{
140+
message = $"Expected charset: '{ExpectedCharSet}'\nActual: '{charset}'";
141+
return false;
142+
}
143+
127144
Debug.Assert(message == null);
128145
return true;
129146
}
@@ -157,7 +174,15 @@ async public Task<NetmockeryTestCaseResult> ExecuteAgainstHttpClientAsync(HttpCl
157174
{
158175
responseCreator = responseMessage.Headers.GetValues("X-Netmockery-ResponseCreator").ElementAt(0);
159176
}
160-
if (Evaluate(requestMatcher, responseCreator, body, out message))
177+
var contentType = "";
178+
var charset = "";
179+
if (responseMessage.Content.Headers.ContentType != null)
180+
{
181+
contentType = responseMessage.Content.Headers.ContentType.MediaType;
182+
charset = responseMessage.Content.Headers.ContentType.CharSet;
183+
}
184+
185+
if (Evaluate(requestMatcher, responseCreator, body, contentType, charset, out message))
161186
{
162187
retval.SetSuccess();
163188
}
@@ -204,6 +229,8 @@ async public Task<NetmockeryTestCaseResult> ExecuteAsync(EndpointCollection endp
204229

205230
var responseCreator = matcher_and_creator.ResponseCreator;
206231
string responseBody = null;
232+
string charset = "";
233+
string contenttype = "";
207234
if (NeedsResponseBody)
208235
{
209236
var simpleResponseCreator = responseCreator as SimpleResponseCreator;
@@ -225,9 +252,11 @@ async public Task<NetmockeryTestCaseResult> ExecuteAsync(EndpointCollection endp
225252
requestInfo.SetStaticNow(now.Value);
226253
}
227254
responseBody = simpleResponseCreator.GetBodyAndExecuteReplacements(requestInfo);
255+
contenttype = simpleResponseCreator.ContentType ?? "";
256+
charset = simpleResponseCreator.Encoding.WebName;
228257
}
229258
string message;
230-
if (Evaluate(matcher_and_creator.RequestMatcher.ToString(), matcher_and_creator.ResponseCreator.ToString(), responseBody, out message))
259+
if (Evaluate(matcher_and_creator.RequestMatcher.ToString(), matcher_and_creator.ResponseCreator.ToString(), responseBody, contenttype, charset, out message))
231260
{
232261
return testResult.SetSuccess();
233262
}

0 commit comments

Comments
 (0)