Skip to content

Commit 5ac3e71

Browse files
authored
HttpTestAsserter for asserting HTTP response
* HttpTestAsserter for asserting HTTP response, WithBasicAuthentication, WithBearerAuthentication added to HttpTesterClient * updated readme file
1 parent 22ec0d8 commit 5ac3e71

File tree

9 files changed

+522
-9
lines changed

9 files changed

+522
-9
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.1.2</Version>
3+
<Version>0.2.0</Version>
44
</PropertyGroup>
55
</Project>

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ using (var client = new HttpTesterClient())
2222
.WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } })
2323
.WithMethod(HttpMethod.Get)
2424
.WithPath("/api/bicycles")
25+
.WithBearerAuthentication("eXy....")
2526
.Start();
2627

2728
var msg = await response.GetResponseBody<List<Bicycle>>();
@@ -54,6 +55,7 @@ using (var client = new HttpTesterClient())
5455
.WithMethod(HttpMethod.Post)
5556
.WithJsonBody(bicycle)
5657
.WithPath("/api/bicycles")
58+
.WithBasicAuthentication("user", "pass")
5759
.Start();
5860

5961
var msg = await response.GetResponseBody<Bicycle>();
@@ -67,6 +69,65 @@ using (var client = new HttpTesterClient())
6769
}
6870
```
6971

72+
### HttpTestAsserter
73+
74+
This is an implementation of the HTTP response message asserter, which can be used to assert different paramters.
75+
76+
Asserter produces a list of `AssertResult`:
77+
78+
```csharp
79+
/// <summary>
80+
/// Assert result object
81+
/// </summary>
82+
public class AssertResult
83+
{
84+
/// <summary>
85+
/// Assert name
86+
/// </summary>
87+
public string Name { get; set; }
88+
/// <summary>
89+
/// Is assert true
90+
/// </summary>
91+
public bool IsTrue { get; set; }
92+
/// <summary>
93+
/// Assert message
94+
/// </summary>
95+
public string Message { get; set; }
96+
}
97+
```
98+
99+
If we extend the previous example:
100+
101+
```csharp
102+
using (var client = new HttpTesterClient())
103+
{
104+
var response = await client
105+
.CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net"))
106+
.WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } })
107+
.WithMethod(HttpMethod.Get)
108+
.WithPath("/api/bicycles")
109+
.Start();
110+
111+
var duration = client.Duration;
112+
113+
//pass response to the asserter
114+
var asserter = new HttpTestAsserter(response);
115+
116+
//Produce the list of assert results
117+
var assertResults = asserter
118+
.ResponseContentContains("scott")
119+
.RequestDurationEquals(duration, (x) => x < 1000)
120+
.ResponseStatusCodeEquals(HttpStatusCode.OK)
121+
.AssertAll();
122+
123+
//if you use xUnit, you can assert the results like this
124+
foreach (var result in assertResults)
125+
{
126+
Assert.True(result.IsTrue, result.Message);
127+
}
128+
}
129+
```
130+
70131
## To-do
71132

72133
- **This library is an early alpha version**
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
using QAToolKit.Engine.HttpTester.Extensions;
2+
using QAToolKit.Engine.HttpTester.Test.Fixtures;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Net;
7+
using System.Net.Http;
8+
using System.Threading.Tasks;
9+
using Xunit;
10+
11+
namespace QAToolKit.Engine.HttpTester.Test
12+
{
13+
public class HttpTestAsserterTests
14+
{
15+
[Fact]
16+
public async Task HttpTestAsserterSimple_Success()
17+
{
18+
using (var client = new HttpTesterClient())
19+
{
20+
var response = await client
21+
.CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net"))
22+
.WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } })
23+
.WithMethod(HttpMethod.Get)
24+
.WithPath("/api/bicycles")
25+
.Start();
26+
27+
var msg = await response.GetResponseBody<List<Bicycle>>();
28+
29+
var asserter = new HttpTestAsserter(response);
30+
31+
var duration = client.Duration;
32+
var assertResults = asserter
33+
.ResponseContentContains("scott")
34+
.RequestDurationEquals(duration, (x) => x < 1000)
35+
.ResponseStatusCodeEquals(HttpStatusCode.OK)
36+
.AssertAll();
37+
38+
foreach (var result in assertResults)
39+
{
40+
Assert.True(result.IsTrue, result.Message);
41+
}
42+
}
43+
}
44+
45+
[Fact]
46+
public async Task HttpTestAsserterDoesNotContainHeader_Success()
47+
{
48+
using (var client = new HttpTesterClient())
49+
{
50+
var response = await client
51+
.CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net"))
52+
.WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } })
53+
.WithMethod(HttpMethod.Get)
54+
.WithPath("/api/bicycles")
55+
.Start();
56+
57+
var msg = await response.GetResponseBody<List<Bicycle>>();
58+
59+
var asserter = new HttpTestAsserter(response);
60+
61+
var assertResults = asserter
62+
.ResponseHasHttpHeader("authentication")
63+
.AssertAll();
64+
65+
foreach (var result in assertResults)
66+
{
67+
Assert.False(result.IsTrue, result.Message);
68+
}
69+
}
70+
}
71+
72+
[Fact]
73+
public async Task HttpTestAsserterDoesNotContainKeywordInBody_Success()
74+
{
75+
using (var client = new HttpTesterClient())
76+
{
77+
var response = await client
78+
.CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net"))
79+
.WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } })
80+
.WithMethod(HttpMethod.Get)
81+
.WithPath("/api/bicycles")
82+
.Start();
83+
84+
var msg = await response.GetResponseBody<List<Bicycle>>();
85+
86+
var asserter = new HttpTestAsserter(response);
87+
88+
var assertResults = asserter
89+
.ResponseContentContains("giant")
90+
.AssertAll();
91+
92+
foreach (var result in assertResults)
93+
{
94+
Assert.False(result.IsTrue, result.Message);
95+
}
96+
}
97+
}
98+
99+
[Fact]
100+
public async Task HttpTestAsserterHeaderMissing_Fails()
101+
{
102+
using (var client = new HttpTesterClient())
103+
{
104+
var response = await client
105+
.CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net"))
106+
.WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } })
107+
.WithMethod(HttpMethod.Get)
108+
.WithPath("/api/bicycles")
109+
.Start();
110+
111+
var msg = await response.GetResponseBody<List<Bicycle>>();
112+
113+
var asserter = new HttpTestAsserter(response);
114+
115+
var duration = client.Duration;
116+
Assert.Throws<ArgumentNullException>(() => asserter
117+
.ResponseContentContains("scott")
118+
.RequestDurationEquals(duration, (x) => x < 1000)
119+
.ResponseStatusCodeEquals(HttpStatusCode.OK)
120+
.ResponseHasHttpHeader(null)
121+
.AssertAll());
122+
}
123+
}
124+
125+
[Fact]
126+
public async Task HttpTestAsserterBodyNull_Fails()
127+
{
128+
using (var client = new HttpTesterClient())
129+
{
130+
var response = await client
131+
.CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net"))
132+
.WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } })
133+
.WithMethod(HttpMethod.Get)
134+
.WithPath("/api/bicycles")
135+
.Start();
136+
137+
var msg = await response.GetResponseBody<List<Bicycle>>();
138+
139+
var asserter = new HttpTestAsserter(response);
140+
141+
var duration = client.Duration;
142+
Assert.Throws<ArgumentNullException>(() => asserter
143+
.ResponseContentContains(null)
144+
.RequestDurationEquals(duration, (x) => x < 1000)
145+
.ResponseStatusCodeEquals(HttpStatusCode.OK)
146+
.AssertAll());
147+
}
148+
}
149+
150+
[Fact]
151+
public async Task HttpTestAsserterAlternativeDurationPredicate_Success()
152+
{
153+
using (var client = new HttpTesterClient())
154+
{
155+
var response = await client
156+
.CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net"))
157+
.WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } })
158+
.WithMethod(HttpMethod.Get)
159+
.WithPath("/api/bicycles")
160+
.Start();
161+
162+
var msg = await response.GetResponseBody<List<Bicycle>>();
163+
164+
var asserter = new HttpTestAsserter(response);
165+
var duration = client.Duration;
166+
var assertResults = asserter
167+
.ResponseContentContains("scott")
168+
.ResponseContentContains("id")
169+
.RequestDurationEquals(duration, (x) => (x > 100 && x < 1000))
170+
.ResponseStatusCodeEquals(HttpStatusCode.OK)
171+
.AssertAll();
172+
173+
Assert.Equal(4, assertResults.ToList().Count);
174+
foreach (var result in assertResults)
175+
{
176+
Assert.True(result.IsTrue, result.Message);
177+
}
178+
}
179+
}
180+
}
181+
}

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

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
using QAToolKit.Core.HttpRequestTools;
1+
using ExpectedObjects;
2+
using QAToolKit.Core.HttpRequestTools;
23
using QAToolKit.Core.Models;
4+
using QAToolKit.Engine.HttpTester.Exceptions;
5+
using QAToolKit.Engine.HttpTester.Extensions;
6+
using QAToolKit.Engine.HttpTester.Test.Fixtures;
37
using QAToolKit.Source.Swagger;
48
using System;
59
using System.Collections.Generic;
@@ -8,10 +12,6 @@
812
using System.Net.Http;
913
using System.Threading.Tasks;
1014
using Xunit;
11-
using ExpectedObjects;
12-
using QAToolKit.Engine.HttpTester.Extensions;
13-
using QAToolKit.Engine.HttpTester.Test.Fixtures;
14-
using QAToolKit.Engine.HttpTester.Exceptions;
1515

1616
namespace QAToolKit.Engine.HttpTester.Test
1717
{
@@ -297,7 +297,7 @@ public async Task HttpTesterClientGetWithBodyDisableSSLValidationWithInvalidCert
297297
.WithMethod(HttpMethod.Get)
298298
.WithPath("/api/bicycles/1");
299299

300-
await Assert.ThrowsAsync<HttpRequestException>(async () => await client.Start());
300+
await Assert.ThrowsAsync<HttpRequestException>(async () => await client.Start());
301301
}
302302
}
303303

@@ -359,5 +359,47 @@ public async Task HttpTesterClientPostObjectBodyWithFulUrl_Success()
359359
Assert.Equal("Giant", msg.brand.ToString());
360360
}
361361
}
362+
363+
[Fact]
364+
public async Task HttpTesterClientPostObjectBodyWithFulUrlWithBasicAuthorization_Success()
365+
{
366+
using (var client = new HttpTesterClient())
367+
{
368+
var response = await client
369+
.CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net/api/bicycles?api-version=1"))
370+
.WithJsonBody(BicycleFixture.GetCfr())
371+
.WithMethod(HttpMethod.Post)
372+
.WithBasicAuthentication("user", "pass")
373+
.Start();
374+
375+
var msg = await response.GetResponseBody<dynamic>();
376+
377+
Assert.True(client.Duration < 2000);
378+
Assert.True(client.HttpClient.DefaultRequestHeaders.Contains("Authorization"));
379+
Assert.True(response.IsSuccessStatusCode);
380+
Assert.Equal("Giant", msg.brand.ToString());
381+
}
382+
}
383+
384+
[Fact]
385+
public async Task HttpTesterClientPostObjectBodyWithFulUrlWithBearerAuthorization_Success()
386+
{
387+
using (var client = new HttpTesterClient())
388+
{
389+
var response = await client
390+
.CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net/api/bicycles?api-version=1"))
391+
.WithJsonBody(BicycleFixture.GetCfr())
392+
.WithMethod(HttpMethod.Post)
393+
.WithBearerAuthentication("123")
394+
.Start();
395+
396+
var msg = await response.GetResponseBody<dynamic>();
397+
398+
Assert.True(client.Duration < 2000);
399+
Assert.True(client.HttpClient.DefaultRequestHeaders.Contains("Authorization"));
400+
Assert.True(response.IsSuccessStatusCode);
401+
Assert.Equal("Giant", msg.brand.ToString());
402+
}
403+
}
362404
}
363405
}

0 commit comments

Comments
 (0)