Skip to content

Commit 38eaa61

Browse files
committed
Support for querystring in scripts
1 parent 38ad2ca commit 38eaa61

File tree

4 files changed

+43
-44
lines changed

4 files changed

+43
-44
lines changed

UnitTests/TestDynamicResponse.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public void CanAccessGlobalVariables()
4242
Assert.Equal("foobar", Eval("RequestBody + RequestPath", new RequestInfo { RequestBody = "foo", RequestPath = "bar" }));
4343
}
4444

45+
[Fact]
46+
public void CanAccessQueryString()
47+
{
48+
Assert.Equal("foobar?ama", Eval("RequestBody + RequestPath + QueryString", new RequestInfo { RequestBody = "foo", RequestPath = "bar", QueryString = "?ama" }));
49+
}
50+
4551
[Fact]
4652
public void MultilineScript()
4753
{

UnitTests/TestTestCommand.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public TestTestCommand()
100100
{
101101
dc = new DirectoryCreator();
102102
dc.AddFile("endpoint1\\endpoint.json", TESTCOMMAND_CONSTANTS.ENDPOINTJSON);
103+
dc.AddFile("endpoint1\\myscript.csscript", "return \"Hello world\";");
103104
dc.AddFile("endpoint1\\content.txt", "FOOBARBOOBAR");
104105
dc.AddFile("tests\\tests.json", TESTCOMMAND_CONSTANTS.TESTS);
105106
dc.AddFile("tests\\example.txt", "FOOBARBOOBAR");
@@ -129,6 +130,22 @@ public void CanReadTestsFromJSONFile()
129130
Assert.Equal("FOOBARBOOBAR", test.ExpectedResponseBody);
130131
}
131132

133+
[Fact]
134+
async public void RequestBodyCanBeUnspecified()
135+
{
136+
var endpointCollection = EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName);
137+
138+
var test = new NetmockeryTestCase
139+
{
140+
RequestPath = "/foo/",
141+
ExpectedRequestMatcher = "Any request",
142+
ExpectedResponseBody = "Hello world"
143+
};
144+
145+
var result = await test.ExecuteAsync(endpointCollection, false);
146+
Assert.True(result.OK, result.Message);
147+
}
148+
132149
[Fact]
133150
public void RequestBodyCanBeReadFromFile()
134151
{

netmockery/NetmockeryTestCase.cs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,20 @@ namespace netmockery
1212
public class TestCaseHttpRequest : IHttpRequestWrapper
1313
{
1414
private string path;
15+
private string querystring;
1516
private HeaderDictionary headerDictionary = new HeaderDictionary();
1617

17-
public TestCaseHttpRequest(string path)
18+
public TestCaseHttpRequest(string path, string querystring)
1819
{
1920
this.path = path;
20-
}
21-
22-
public IHeaderDictionary Headers
23-
{
24-
get
25-
{
26-
return headerDictionary;
27-
}
21+
this.querystring = querystring;
2822
}
2923

30-
public PathString Path
31-
{
32-
get
33-
{
24+
public IHeaderDictionary Headers => headerDictionary;
3425

35-
return new PathString(path);
36-
}
37-
}
26+
public PathString Path => new PathString(path);
27+
28+
public QueryString QueryString => new QueryString(querystring);
3829
}
3930

4031
public class TestCaseHttpResponse : IHttpResponseWrapper
@@ -44,13 +35,7 @@ public class TestCaseHttpResponse : IHttpResponseWrapper
4435
Encoding writtenEncoding;
4536
string contentType;
4637

47-
public Stream Body
48-
{
49-
get
50-
{
51-
return memoryStream;
52-
}
53-
}
38+
public Stream Body => memoryStream;
5439

5540
public string ContentType
5641
{
@@ -62,12 +47,12 @@ public string ContentType
6247

6348
async public Task WriteAsync(string content, Encoding encoding)
6449
{
65-
6650
writtenContent = content;
6751
writtenEncoding = encoding;
6852
await Task.Yield();
6953
}
7054
}
55+
7156
public class NetmockeryTestCase
7257
{
7358
public string Name;
@@ -145,7 +130,7 @@ async public Task<NetmockeryTestCaseResult> ExecuteAsync(EndpointCollection endp
145130
else
146131
{
147132
bool singleMatch;
148-
var matcher_and_creator = endpoint.Resolve(new PathString(RequestPath), new QueryString(QueryString), RequestBody, null, out singleMatch);
133+
var matcher_and_creator = endpoint.Resolve(new PathString(RequestPath), new QueryString(QueryString), RequestBody ?? "", null, out singleMatch);
149134
if (matcher_and_creator != null)
150135
{
151136
var responseCreator = matcher_and_creator.Item2;
@@ -158,7 +143,7 @@ async public Task<NetmockeryTestCaseResult> ExecuteAsync(EndpointCollection endp
158143
{
159144
if (NeedsResponseBody)
160145
{
161-
var responseBodyBytes = await responseCreator.CreateResponseAsync(new TestCaseHttpRequest(RequestPath), Encoding.UTF8.GetBytes(RequestBody), new TestCaseHttpResponse(), endpoint.Directory);
146+
var responseBodyBytes = await responseCreator.CreateResponseAsync(new TestCaseHttpRequest(RequestPath, QueryString), Encoding.UTF8.GetBytes(RequestBody ?? ""), new TestCaseHttpResponse(), endpoint.Directory);
162147
responseBody = Encoding.UTF8.GetString(responseBodyBytes);
163148
}
164149
string message;
@@ -197,7 +182,7 @@ async public Task<Tuple<string, string>> GetResponseAsync(EndpointCollection end
197182
if (matcher_and_creator != null)
198183
{
199184
var responseCreator = matcher_and_creator.Item2;
200-
var responseBodyBytes = await responseCreator.CreateResponseAsync(new TestCaseHttpRequest(RequestPath), Encoding.UTF8.GetBytes(RequestBody), new TestCaseHttpResponse(), endpoint.Directory);
185+
var responseBodyBytes = await responseCreator.CreateResponseAsync(new TestCaseHttpRequest(RequestPath, QueryString), Encoding.UTF8.GetBytes(RequestBody), new TestCaseHttpResponse(), endpoint.Directory);
201186
return Tuple.Create(Encoding.UTF8.GetString(responseBodyBytes), (string)null);
202187
}
203188
else

netmockery/ResponseCreator.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public interface IHttpResponseWrapper
2121
public interface IHttpRequestWrapper
2222
{
2323
PathString Path { get; }
24+
QueryString QueryString { get; }
2425
IHeaderDictionary Headers { get; }
2526
}
2627

@@ -33,21 +34,9 @@ public HttpRequestWrapper(HttpRequest httpRequest)
3334
Debug.Assert(httpRequest != null);
3435
this.httpRequest = httpRequest;
3536
}
36-
public IHeaderDictionary Headers
37-
{
38-
get
39-
{
40-
return httpRequest.Headers;
41-
}
42-
}
43-
44-
public PathString Path
45-
{
46-
get
47-
{
48-
return httpRequest.Path;
49-
}
50-
}
37+
public IHeaderDictionary Headers => httpRequest.Headers;
38+
public PathString Path => httpRequest.Path;
39+
public QueryString QueryString => httpRequest.QueryString;
5140
}
5241

5342
public class HttpResponseWrapper : IHttpResponseWrapper
@@ -87,8 +76,9 @@ public interface IResponseCreatorWithFilename
8776

8877
public class RequestInfo
8978
{
90-
public string RequestBody;
9179
public string RequestPath;
80+
public string QueryString;
81+
public string RequestBody;
9282
public IHeaderDictionary Headers;
9383
public string EndpointDirectory;
9484
}
@@ -111,8 +101,9 @@ public override async Task<byte[]> CreateResponseAsync(IHttpRequestWrapper reque
111101
{
112102
var responseBody = GetBodyAndExecuteReplacements(new RequestInfo
113103
{
114-
RequestBody = Encoding.UTF8.GetString(requestBody),
115104
RequestPath = request.Path.ToString(),
105+
QueryString = request.QueryString.ToString(),
106+
RequestBody = Encoding.UTF8.GetString(requestBody),
116107
Headers = request.Headers,
117108
EndpointDirectory = endpointDirectory
118109
});

0 commit comments

Comments
 (0)