Skip to content

Commit 24a5d36

Browse files
committed
Merged with develop
2 parents 67e4e0b + 5ac3e71 commit 24a5d36

File tree

9 files changed

+519
-9
lines changed

9 files changed

+519
-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: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using QAToolKit.Core.HttpRequestTools;
1+
using ExpectedObjects;
2+
using QAToolKit.Core.HttpRequestTools;
23
using QAToolKit.Core.Models;
34
using QAToolKit.Source.Swagger;
45
using System;
@@ -8,10 +9,6 @@
89
using System.Net.Http;
910
using System.Threading.Tasks;
1011
using Xunit;
11-
using ExpectedObjects;
12-
using QAToolKit.Engine.HttpTester.Extensions;
13-
using QAToolKit.Engine.HttpTester.Test.Fixtures;
14-
using QAToolKit.Engine.HttpTester.Exceptions;
1512

1613
namespace QAToolKit.Engine.HttpTester.Test
1714
{
@@ -297,7 +294,7 @@ public async Task HttpTesterClientGetWithBodyDisableSSLValidationWithInvalidCert
297294
.WithMethod(HttpMethod.Get)
298295
.WithPath("/api/bicycles/1");
299296

300-
await Assert.ThrowsAsync<HttpRequestException>(async () => await client.Start());
297+
await Assert.ThrowsAsync<HttpRequestException>(async () => await client.Start());
301298
}
302299
}
303300

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

0 commit comments

Comments
 (0)