Skip to content

Commit f367f60

Browse files
authored
Asserter has new methods ResponseStatusCodeIsSuccess and ResponseBodyIsEmpty, small updates
1 parent 457fb55 commit f367f60

File tree

8 files changed

+97
-11
lines changed

8 files changed

+97
-11
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>0.2.8</Version>
3+
<Version>0.2.9</Version>
44
</PropertyGroup>
55
</Project>

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,15 @@ Currently `HttpTesterClient` supports:
173173

174174
### HttpTestAsserter
175175

176-
This is an implementation of the HTTP response message asserter, which can be used to assert different paramters.
176+
This is an implementation of the HTTP response message asserter, which can be used to assert different parameters.
177+
178+
Here is a list of Asserters:
179+
- `ResponseContentContains`: HTTP body contains a string (ignores case)
180+
- `RequestDurationEquals`: Verify request duration
181+
- `ResponseStatusCodeEquals`: Verify if response code equals
182+
- `ResponseHasHttpHeader`: HTTP response contains a header
183+
- `ResponseStatusCodeIsSuccess`: HTTP response status code is one of 2xx
184+
- `ResponseBodyIsEmpty`: HTTP response body is empty
177185

178186
Asserter produces a list of `AssertResult`:
179187

@@ -220,6 +228,7 @@ using (var client = new HttpTesterClient())
220228
.ResponseContentContains("scott")
221229
.RequestDurationEquals(duration, (x) => x < 1000)
222230
.ResponseStatusCodeEquals(HttpStatusCode.OK)
231+
.ResponseStatusCodeIsSuccess()
223232
.AssertAll();
224233

225234
//if you use xUnit, you can assert the results like this
@@ -232,7 +241,9 @@ using (var client = new HttpTesterClient())
232241

233242
## To-do
234243

235-
- **This library is an early alpha version**
244+
- **This library is a beta version**
245+
- Add more Http Asserters
246+
- Cover more test cases with HTTP Tester client
236247

237248
## License
238249

src/QAToolKit.Engine.HttpTester.Test/HttpTestAsserterTests.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public async Task HttpTestAsserterSimple_Success()
3333
.ResponseContentContains("scott")
3434
.RequestDurationEquals(duration, (x) => x < 1000)
3535
.ResponseStatusCodeEquals(HttpStatusCode.OK)
36+
.ResponseHasHttpHeader("Date")
3637
.AssertAll();
3738

3839
foreach (var result in assertResults)
@@ -168,9 +169,38 @@ public async Task HttpTestAsserterAlternativeDurationPredicate_Success()
168169
.ResponseContentContains("id")
169170
.RequestDurationEquals(duration, (x) => (x > 100 && x < 1000))
170171
.ResponseStatusCodeEquals(HttpStatusCode.OK)
172+
.ResponseStatusCodeIsSuccess()
171173
.AssertAll();
172174

173-
Assert.Equal(4, assertResults.ToList().Count);
175+
Assert.Equal(5, assertResults.ToList().Count);
176+
foreach (var result in assertResults)
177+
{
178+
Assert.True(result.IsTrue, result.Message);
179+
}
180+
}
181+
}
182+
183+
[Fact]
184+
public async Task HttpTestAsserterDeleteIsBodyEmpty_Success()
185+
{
186+
using (var client = new HttpTesterClient())
187+
{
188+
var response = await client
189+
.CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net"))
190+
.WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } })
191+
.WithMethod(HttpMethod.Delete)
192+
.WithPath("/api/bicycles/1")
193+
.Start();
194+
195+
var asserter = new HttpTestAsserter(response);
196+
var duration = client.Duration;
197+
var assertResults = asserter
198+
.ResponseBodyIsEmpty()
199+
.RequestDurationEquals(duration, (x) => (x > 100 && x < 1000))
200+
.ResponseStatusCodeIsSuccess()
201+
.AssertAll();
202+
203+
Assert.Equal(3, assertResults.ToList().Count);
174204
foreach (var result in assertResults)
175205
{
176206
Assert.True(result.IsTrue, result.Message);

src/QAToolKit.Engine.HttpTester.Test/HttpTesterClientTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ public async Task HttpTesterClientAddPostHttpRequest_Success()
567567
});
568568

569569
var requests = await urlSource.Load(new Uri[] {
570-
new Uri("https://qatoolkitapi.azurewebsites.net/swagger/v1/swagger.json")
570+
new Uri("https://qatoolkitapi.azurewebsites.net/swagger/v2/swagger.json")
571571
});
572572

573573
using (var client = new HttpTesterClient())
@@ -599,7 +599,7 @@ public async Task HttpTesterClientAddGetHttpRequest_Success()
599599
});
600600

601601
var requests = await urlSource.Load(new Uri[] {
602-
new Uri("https://qatoolkitapi.azurewebsites.net/swagger/v1/swagger.json")
602+
new Uri("https://qatoolkitapi.azurewebsites.net/swagger/v2/swagger.json")
603603
});
604604

605605
using (var client = new HttpTesterClient())

src/QAToolKit.Engine.HttpTester.Test/QAToolKit.Engine.HttpTester.Test.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
</PackageReference>
1414
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
1515
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
16-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
17-
<PackageReference Include="QAToolKit.Source.Swagger" Version="0.3.0" />
16+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
17+
<PackageReference Include="QAToolKit.Source.Swagger" Version="0.3.9" />
1818
<PackageReference Include="ExpectedObjects" Version="2.3.6" />
1919
<PackageReference Include="xunit" Version="2.4.1" />
2020
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

src/QAToolKit.Engine.HttpTester/HttpTestAsserter.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using QAToolKit.Engine.HttpTester.Models;
44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using System.Net;
78
using System.Net.Http;
89

@@ -95,8 +96,8 @@ public IHttpTestAsserter ResponseHasHttpHeader(string headerName)
9596
{
9697
Name = nameof(ResponseHasHttpHeader),
9798
Message = $"Contains header '{headerName}'.",
98-
IsTrue = _httpResponseMessage.Headers.Contains(headerName)
99-
});
99+
IsTrue = _httpResponseMessage.Headers.TryGetValues(headerName, out var values)
100+
});
100101

101102
return this;
102103
}
@@ -117,5 +118,39 @@ public IHttpTestAsserter ResponseStatusCodeEquals(HttpStatusCode httpStatusCode)
117118

118119
return this;
119120
}
121+
122+
/// <summary>
123+
/// HTTP response status code is one of 2xx
124+
/// </summary>
125+
/// <returns></returns>
126+
public IHttpTestAsserter ResponseStatusCodeIsSuccess()
127+
{
128+
_assertResults.Add(new AssertResult()
129+
{
130+
Name = nameof(ResponseStatusCodeIsSuccess),
131+
Message = $"Expected status code is '2xx' return code is '{_httpResponseMessage.StatusCode}'.",
132+
IsTrue = _httpResponseMessage.IsSuccessStatusCode
133+
});
134+
135+
return this;
136+
}
137+
138+
/// <summary>
139+
/// HTTP response body is empty
140+
/// </summary>
141+
/// <returns></returns>
142+
public IHttpTestAsserter ResponseBodyIsEmpty()
143+
{
144+
var bodyString = _httpResponseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult();
145+
146+
_assertResults.Add(new AssertResult()
147+
{
148+
Name = nameof(ResponseBodyIsEmpty),
149+
Message = $"Expected empty body, returned body is '{bodyString}'.",
150+
IsTrue = string.IsNullOrEmpty(bodyString)
151+
});
152+
153+
return this;
154+
}
120155
}
121156
}

src/QAToolKit.Engine.HttpTester/Interfaces/IHttpTestAsserter.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ public interface IHttpTestAsserter
3737
/// <returns></returns>
3838
IHttpTestAsserter ResponseHasHttpHeader(string headerName);
3939
/// <summary>
40+
/// HTTP response status code is one of 2xx
41+
/// </summary>
42+
/// <returns></returns>
43+
IHttpTestAsserter ResponseStatusCodeIsSuccess();
44+
/// <summary>
45+
/// HTTP response body is empty
46+
/// </summary>
47+
/// <returns></returns>
48+
IHttpTestAsserter ResponseBodyIsEmpty();
49+
/// <summary>
4050
/// Return all Assert messages of the Asserter
4151
/// </summary>
4252
/// <returns></returns>

src/QAToolKit.Engine.HttpTester/QAToolKit.Engine.HttpTester.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@
3535

3636
<ItemGroup>
3737
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
38-
<PackageReference Include="QAToolKit.Core" Version="0.3.0" />
38+
<PackageReference Include="QAToolKit.Core" Version="0.3.6" />
3939
</ItemGroup>
4040
</Project>

0 commit comments

Comments
 (0)