Skip to content

Commit 7c19221

Browse files
author
Dennis Doomen
committed
SAVEPOINT
1 parent 3c88431 commit 7c19221

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

Mockly.Specs/HttpMockSpecs.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,27 @@ await act.Should().ThrowAsync<UnexpectedRequestException>()
7777
.WithMessage("Unexpected request to:*GET http://localhost/api/search?q=something with spaces*");
7878
}
7979

80+
[Fact]
81+
public async Task Can_match_any_query()
82+
{
83+
// Arrange
84+
var mock = new HttpMock();
85+
86+
mock.ForGet()
87+
.WithPath("/api/search")
88+
.WithAnyQuery()
89+
.RespondsWithStatus(HttpStatusCode.OK);
90+
91+
// Build step removed;
92+
var client = mock.GetClient();
93+
94+
// Act
95+
var response = await client.GetAsync("http://localhost/api/search?q=something with spaces");
96+
97+
// Assert
98+
response.StatusCode.Should().Be(HttpStatusCode.OK);
99+
}
100+
80101
[Fact]
81102
public async Task Supports_spaces_in_the_query()
82103
{
@@ -98,6 +119,27 @@ public async Task Supports_spaces_in_the_query()
98119
response.StatusCode.Should().Be(HttpStatusCode.OK);
99120
}
100121

122+
[Fact]
123+
public async Task The_query_does_not_require_a_question_mark()
124+
{
125+
// Arrange
126+
var mock = new HttpMock();
127+
128+
mock.ForGet()
129+
.WithPath("/api/search")
130+
.WithQuery("q=something with spaces")
131+
.RespondsWithStatus(HttpStatusCode.OK);
132+
133+
// Build step removed;
134+
var client = mock.GetClient();
135+
136+
// Act
137+
var response = await client.GetAsync("http://localhost/api/search?q=something with spaces");
138+
139+
// Assert
140+
response.StatusCode.Should().Be(HttpStatusCode.OK);
141+
}
142+
101143
[Fact]
102144
public async Task Can_mock_get_request_with_json_response()
103145
{

Mockly/RequestMockBuilder.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,23 @@ public RequestMockBuilder WithPath(string wildcardPattern)
9797
/// </summary>
9898
public RequestMockBuilder WithQuery(string wildcardPattern)
9999
{
100+
if (!wildcardPattern.StartsWith("?", StringComparison.Ordinal))
101+
{
102+
wildcardPattern = "?" + wildcardPattern;
103+
}
104+
100105
queryPattern = wildcardPattern;
101106
return this;
102107
}
103108

109+
/// <summary>
110+
/// Configures the request mock to match any query string in the request URI.
111+
/// </summary>
112+
public RequestMockBuilder WithAnyQuery()
113+
{
114+
return WithQuery("*");
115+
}
116+
104117
/// <summary>
105118
/// Resets the query string matching to match any query string.
106119
/// </summary>
@@ -335,11 +348,27 @@ public RequestMockResponseBuilder RespondsWithODataResult<T>(IEnumerable<T> valu
335348
return new RequestMockResponseBuilder(mock);
336349
}
337350

351+
/// <summary>
352+
/// Responds with plain text and status code <see cref="HttpStatusCode.OK"/>.
353+
/// </summary>
354+
public RequestMockResponseBuilder RespondsWithContent(string content)
355+
{
356+
return RespondsWithContent(HttpStatusCode.OK, content, "text/plain");
357+
}
358+
338359
/// <summary>
339360
/// Responds with raw string content.
340361
/// </summary>
341-
public RequestMockResponseBuilder RespondsWithContent(string content, string contentType = "text/plain",
342-
HttpStatusCode statusCode = HttpStatusCode.OK)
362+
public RequestMockResponseBuilder RespondsWithContent(HttpStatusCode statusCode, string content)
363+
{
364+
return RespondsWithContent(statusCode, content, "text/plain");
365+
}
366+
367+
/// <summary>
368+
/// Responds with raw string content.
369+
/// </summary>
370+
public RequestMockResponseBuilder RespondsWithContent(HttpStatusCode statusCode, string content,
371+
string contentType)
343372
{
344373
var mock = new RequestMock
345374
{

0 commit comments

Comments
 (0)