Skip to content

Commit f68be3f

Browse files
authored
HttpTester client now has WithPathReplacementValues, bug fix with query parameters (#21)
* HttpTester client now has WithPathReplacementValues, bug fix with query parameters * small update
1 parent f367f60 commit f68be3f

File tree

7 files changed

+217
-89
lines changed

7 files changed

+217
-89
lines changed

Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>0.2.9</Version>
3+
<Version>0.2.11</Version>
44
</PropertyGroup>
55
</Project>

src/QAToolKit.Engine.HttpTester.Test/Fixtures/BicycleFixture.cs

+25
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@ public static Bicycle Get()
1515
};
1616
}
1717

18+
public static Bicycle GetCannondale()
19+
{
20+
return new Bicycle
21+
{
22+
Id = 2,
23+
Name = "CAADX",
24+
Brand = "Cannondale",
25+
Type = BicycleType.Gravel
26+
};
27+
}
28+
29+
public static List<Bicycle> GetCannondaleArray()
30+
{
31+
return new List<Bicycle>()
32+
{
33+
new Bicycle()
34+
{
35+
Id = 2,
36+
Name = "CAADX",
37+
Brand = "Cannondale",
38+
Type = BicycleType.Gravel
39+
}
40+
};
41+
}
42+
1843
public static Bicycle GetFoil()
1944
{
2045
return new Bicycle

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public async Task HttpTestAsserterSimple_Success()
3131
var duration = client.Duration;
3232
var assertResults = asserter
3333
.ResponseContentContains("scott")
34-
.RequestDurationEquals(duration, (x) => x < 1000)
34+
.RequestDurationEquals(duration, (x) => x < 2000)
3535
.ResponseStatusCodeEquals(HttpStatusCode.OK)
3636
.ResponseHasHttpHeader("Date")
3737
.AssertAll();
@@ -116,7 +116,7 @@ public async Task HttpTestAsserterHeaderMissing_Fails()
116116
var duration = client.Duration;
117117
Assert.Throws<ArgumentNullException>(() => asserter
118118
.ResponseContentContains("scott")
119-
.RequestDurationEquals(duration, (x) => x < 1000)
119+
.RequestDurationEquals(duration, (x) => x < 2000)
120120
.ResponseStatusCodeEquals(HttpStatusCode.OK)
121121
.ResponseHasHttpHeader(null)
122122
.AssertAll());

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

+70-1
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ public void HttpTesterClientBrochureUploadMultipartPresent_Fails()
548548
.WithMultipart("Metadata.Year", "2000")
549549
.WithMultipart("Metadata.Name", "Brochure 2000");
550550

551-
var exception = Assert.Throws<QAToolKitEngineHttpTesterException>(() => client.WithJsonBody<string>("1234"));
551+
var exception = Assert.Throws<QAToolKitEngineHttpTesterException>(() => client.WithJsonBody<string>("1234"));
552552
Assert.StartsWith("Body multipart/form-data already defined", exception.Message);
553553
}
554554
}
@@ -617,5 +617,74 @@ public async Task HttpTesterClientAddGetHttpRequest_Success()
617617
Assert.True(response.IsSuccessStatusCode);
618618
}
619619
}
620+
621+
[Fact]
622+
public async Task HttpTesterClientGetBikeByIdRequest_Success()
623+
{
624+
var urlSource = new SwaggerUrlSource(options =>
625+
{
626+
options.AddBaseUrl(new Uri("https://qatoolkitapi.azurewebsites.net/"));
627+
options.AddRequestFilters(new RequestFilter()
628+
{
629+
EndpointNameWhitelist = new string[] { "GetBike" }
630+
});
631+
options.UseSwaggerExampleValues = true;
632+
});
633+
634+
var requests = await urlSource.Load(new Uri[] {
635+
new Uri("https://qatoolkitapi.azurewebsites.net/swagger/v2/swagger.json")
636+
});
637+
638+
using (var client = new HttpTesterClient())
639+
{
640+
var response = await client
641+
.CreateHttpRequest(requests.FirstOrDefault())
642+
.WithPathReplacementValues(new Dictionary<string, string>() { { "id", "2" } })
643+
.WithQueryParams(new Dictionary<string, string>() { { "api-version", "2" } })
644+
.Start();
645+
646+
var msg = await response.GetResponseBody<Bicycle>();
647+
648+
var expecterResponse = BicycleFixture.GetCannondale().ToExpectedObject();
649+
expecterResponse.ShouldEqual(msg);
650+
651+
Assert.True(client.Duration < 2000);
652+
Assert.True(response.IsSuccessStatusCode);
653+
}
654+
}
655+
656+
[Fact]
657+
public async Task HttpTesterClientGetBikesByTypeHttpRequest_Success()
658+
{
659+
var urlSource = new SwaggerUrlSource(options =>
660+
{
661+
options.AddBaseUrl(new Uri("https://qatoolkitapi.azurewebsites.net/"));
662+
options.AddRequestFilters(new RequestFilter()
663+
{
664+
EndpointNameWhitelist = new string[] { "GetAllBikes" }
665+
});
666+
options.UseSwaggerExampleValues = true;
667+
});
668+
669+
var requests = await urlSource.Load(new Uri[] {
670+
new Uri("https://qatoolkitapi.azurewebsites.net/swagger/v2/swagger.json")
671+
});
672+
673+
using (var client = new HttpTesterClient())
674+
{
675+
var response = await client
676+
.CreateHttpRequest(requests.FirstOrDefault())
677+
.WithQueryParams(new Dictionary<string, string>() { { "api-version", "2" }, {"bicycleType", "1" } })
678+
.Start();
679+
680+
var msg = await response.GetResponseBody<List<Bicycle>>();
681+
682+
var expecterResponse = BicycleFixture.GetCannondaleArray().ToExpectedObject();
683+
expecterResponse.ShouldEqual(msg);
684+
685+
Assert.True(client.Duration < 2000);
686+
Assert.True(response.IsSuccessStatusCode);
687+
}
688+
}
620689
}
621690
}

src/QAToolKit.Engine.HttpTester/HttpTestAsserter.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using QAToolKit.Engine.HttpTester.Models;
44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
76
using System.Net;
87
using System.Net.Http;
98

@@ -97,7 +96,7 @@ public IHttpTestAsserter ResponseHasHttpHeader(string headerName)
9796
Name = nameof(ResponseHasHttpHeader),
9897
Message = $"Contains header '{headerName}'.",
9998
IsTrue = _httpResponseMessage.Headers.TryGetValues(headerName, out var values)
100-
});
99+
});
101100

102101
return this;
103102
}

0 commit comments

Comments
 (0)