Skip to content

Commit 67e4e0b

Browse files
authored
JSON body can be passed as string, logo and test update
* logo and test update * Httptester client updates * JSON body can be passed as string, small bug fixes and new tests
1 parent 5cc7bd4 commit 67e4e0b

File tree

6 files changed

+84
-25
lines changed

6 files changed

+84
-25
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.1</Version>
3+
<Version>0.1.2</Version>
44
</PropertyGroup>
55
</Project>

qatoolkit-64x64.png

-813 Bytes
Loading

qatoolkit-engine-httptester-net.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ Global
1414
Release|Any CPU = Release|Any CPU
1515
EndGlobalSection
1616
GlobalSection(ProjectConfigurationPlatforms) = postSolution
17-
{23F0CF6E-9A0A-4BE5-8F51-51DAF799A0BF}.Debug With Project References|Any CPU.ActiveCfg = Debug With Project References|Any CPU
18-
{23F0CF6E-9A0A-4BE5-8F51-51DAF799A0BF}.Debug With Project References|Any CPU.Build.0 = Debug With Project References|Any CPU
17+
{23F0CF6E-9A0A-4BE5-8F51-51DAF799A0BF}.Debug With Project References|Any CPU.ActiveCfg = Debug|Any CPU
18+
{23F0CF6E-9A0A-4BE5-8F51-51DAF799A0BF}.Debug With Project References|Any CPU.Build.0 = Debug|Any CPU
1919
{23F0CF6E-9A0A-4BE5-8F51-51DAF799A0BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2020
{23F0CF6E-9A0A-4BE5-8F51-51DAF799A0BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
2121
{23F0CF6E-9A0A-4BE5-8F51-51DAF799A0BF}.Release|Any CPU.ActiveCfg = Release|Any CPU

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

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using QAToolKit.Core.HttpRequestTools;
22
using QAToolKit.Core.Models;
3-
using QAToolKit.Engine.HttpTester.Exceptions;
4-
using QAToolKit.Engine.HttpTester.Extensions;
5-
using QAToolKit.Engine.HttpTester.Test.Fixtures;
63
using QAToolKit.Source.Swagger;
74
using System;
85
using System.Collections.Generic;
@@ -12,7 +9,9 @@
129
using System.Threading.Tasks;
1310
using Xunit;
1411
using ExpectedObjects;
15-
using System.Security.Authentication;
12+
using QAToolKit.Engine.HttpTester.Extensions;
13+
using QAToolKit.Engine.HttpTester.Test.Fixtures;
14+
using QAToolKit.Engine.HttpTester.Exceptions;
1615

1716
namespace QAToolKit.Engine.HttpTester.Test
1817
{
@@ -301,5 +300,64 @@ public async Task HttpTesterClientGetWithBodyDisableSSLValidationWithInvalidCert
301300
await Assert.ThrowsAsync<HttpRequestException>(async () => await client.Start());
302301
}
303302
}
303+
304+
[Fact]
305+
public async Task HttpTesterClientReturnDynamic_Success()
306+
{
307+
using (var client = new HttpTesterClient())
308+
{
309+
var response = await client
310+
.CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net"))
311+
.WithQueryParams(new Dictionary<string, string>() { { "api-version", "1" } })
312+
.WithMethod(HttpMethod.Post)
313+
.WithJsonBody(BicycleFixture.GetCfr())
314+
.WithPath("/api/bicycles")
315+
.Start();
316+
317+
var msg = await response.GetResponseBody<dynamic>();
318+
319+
Assert.True(client.Duration < 2000);
320+
Assert.True(response.IsSuccessStatusCode);
321+
Assert.Equal("Giant", msg.brand.ToString());
322+
}
323+
}
324+
325+
[Fact]
326+
public async Task HttpTesterClientPostStringBodyWithFulUrl_Success()
327+
{
328+
using (var client = new HttpTesterClient())
329+
{
330+
var response = await client
331+
.CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net/api/bicycles?api-version=1"))
332+
.WithJsonBody("{\"id\": 5,\"name\":\"EXCEED CFR\",\"brand\":\"Giant\",\"type\":2}")
333+
.WithMethod(HttpMethod.Post)
334+
.Start();
335+
336+
var msg = await response.Content.ReadAsStringAsync();
337+
338+
Assert.True(client.Duration < 2000);
339+
Assert.True(response.IsSuccessStatusCode);
340+
//Assert.Equal("Giant", msg.brand.ToString());
341+
}
342+
}
343+
344+
[Fact]
345+
public async Task HttpTesterClientPostObjectBodyWithFulUrl_Success()
346+
{
347+
using (var client = new HttpTesterClient())
348+
{
349+
var response = await client
350+
.CreateHttpRequest(new Uri("https://qatoolkitapi.azurewebsites.net/api/bicycles?api-version=1"))
351+
.WithJsonBody(BicycleFixture.GetCfr())
352+
.WithMethod(HttpMethod.Post)
353+
.Start();
354+
355+
var msg = await response.GetResponseBody<dynamic>();
356+
357+
Assert.True(client.Duration < 2000);
358+
Assert.True(response.IsSuccessStatusCode);
359+
Assert.Equal("Giant", msg.brand.ToString());
360+
}
361+
}
304362
}
305363
}

src/QAToolKit.Engine.HttpTester/HttpTesterClient.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace QAToolKit.Engine.HttpTester
1414
/// </summary>
1515
public class HttpTesterClient : IHttpTesterClient, IDisposable
1616
{
17-
1817
/// <summary>
1918
/// HttpClient object
2019
/// </summary>
@@ -105,12 +104,24 @@ public IHttpTesterClient WithHeaders(Dictionary<string, string> headers)
105104
/// <summary>
106105
/// Add JSON body to the HTTP client
107106
/// </summary>
108-
/// <typeparam name="T"></typeparam>
109-
/// <param name="bodyObject"></param>
107+
/// <typeparam name="T">Type of DTO object</typeparam>
108+
/// <param name="bodyObject">DTO object that needs to be serialized to JSON</param>
110109
/// <returns></returns>
111110
public IHttpTesterClient WithJsonBody<T>(T bodyObject)
112111
{
113-
_body = JsonConvert.SerializeObject(bodyObject);
112+
if (bodyObject == null)
113+
{
114+
return this;
115+
}
116+
117+
if (typeof(T) == typeof(String))
118+
{
119+
_body = bodyObject.ToString();
120+
}
121+
else
122+
{
123+
_body = JsonConvert.SerializeObject(bodyObject);
124+
}
114125

115126
return this;
116127
}
@@ -160,22 +171,20 @@ public async Task<HttpResponseMessage> Start()
160171
throw new QAToolKitEngineHttpTesterException("'Get' method can not have a HTTP body.");
161172
}
162173

163-
//Process params
174+
string queryString = "";
164175
if (_queryParameters != null)
165176
{
166-
var queryString = "?";
177+
queryString = "?";
167178

168179
foreach (var query in _queryParameters)
169180
{
170181
queryString += $"{query.Key}={query.Value}";
171182
}
172-
173-
_path += queryString;
174183
}
175184

176185
var sw = new Stopwatch();
177186
sw.Start();
178-
using (var requestMessage = new HttpRequestMessage(_httpMethod, _path))
187+
using (var requestMessage = new HttpRequestMessage(_httpMethod, _path + queryString))
179188
{
180189
if (_headers != null)
181190
{
@@ -216,13 +225,5 @@ protected virtual void Dispose(bool disposing)
216225
{
217226
HttpClient?.Dispose();
218227
}
219-
220-
/// <summary>
221-
/// Destructor
222-
/// </summary>
223-
~HttpTesterClient()
224-
{
225-
Dispose(false);
226-
}
227228
}
228229
}

src/QAToolKit.Engine.HttpTester/IHttpTesterClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public interface IHttpTesterClient
1616
/// <param name="baseAddress"></param>
1717
/// <param name="validateCertificate"></param>
1818
/// <returns></returns>
19-
IHttpTesterClient CreateHttpRequest(Uri baseAddress, bool validateCertificate);
19+
IHttpTesterClient CreateHttpRequest(Uri baseAddress, bool validateCertificate = true);
2020
/// <summary>
2121
/// Add URL path to the HTTP client
2222
/// </summary>

0 commit comments

Comments
 (0)