Skip to content

Commit 28f430c

Browse files
authored
Implementation of assert named ResponseContentTypeEquals
1 parent 25c7452 commit 28f430c

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
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.3.2</Version>
3+
<Version>0.3.3</Version>
44
</PropertyGroup>
55
</Project>

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,13 @@ You can pass `X509Certificate2` objet to the `WithCertificateAuthentication` met
213213
This is an implementation of the HTTP response message asserter, which can be used to assert different parameters.
214214

215215
Here is a list of Asserters:
216-
- `ResponseContentContains`: HTTP body contains a string (ignores case)
217-
- `RequestDurationEquals`: Verify request duration
218-
- `ResponseStatusCodeEquals`: Verify if response code equals
219-
- `ResponseHasHttpHeader`: HTTP response contains a header
220-
- `ResponseStatusCodeIsSuccess`: HTTP response status code is one of 2xx
221-
- `ResponseBodyIsEmpty`: HTTP response body is empty
216+
- `ResponseContentContains`: HTTP body contains a string (ignores case).
217+
- `RequestDurationEquals`: Verify request duration.
218+
- `ResponseStatusCodeEquals`: Verify if response code equals to specified.
219+
- `ResponseHasHttpHeader`: HTTP response contains a header.
220+
- `ResponseStatusCodeIsSuccess`: HTTP response status code is one of 2xx.
221+
- `ResponseBodyIsEmpty`: HTTP response body is empty.
222+
- `ResponseContentTypeEquals`: Check if HTTP response media type equals to specified.
222223

223224
Asserter produces a list of `AssertResult`:
224225

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.Net;
77
using System.Net.Http;
8+
using System.Net.Http.Headers;
89
using System.Runtime.InteropServices;
910
using System.Threading.Tasks;
1011
using Xunit;
@@ -34,6 +35,7 @@ public async Task HttpTestAsserterSimple_Success()
3435
.ResponseContentContains("scott")
3536
.RequestDurationEquals(duration, (x) => x < 2000)
3637
.ResponseStatusCodeEquals(HttpStatusCode.OK)
38+
.ResponseContentTypeEquals("application/json")
3739
.ResponseHasHttpHeader("Date")
3840
.AssertAll();
3941

@@ -121,6 +123,7 @@ public async Task HttpTestAsserterHeaderMissing_Fails()
121123
.RequestDurationEquals(duration, (x) => x < 2000)
122124
.RequestDurationEquals(httpDuration, (x) => x < 1800)
123125
.ResponseStatusCodeEquals(HttpStatusCode.OK)
126+
.ResponseContentTypeEquals("application/json")
124127
.ResponseHasHttpHeader(null)
125128
.AssertAll());
126129
}
@@ -146,6 +149,7 @@ public async Task HttpTestAsserterBodyNull_Fails()
146149
Assert.Throws<ArgumentNullException>(() => asserter
147150
.ResponseContentContains(null)
148151
.RequestDurationEquals(duration, (x) => x < 1000)
152+
.ResponseContentTypeEquals("application/json")
149153
.ResponseStatusCodeEquals(HttpStatusCode.OK)
150154
.AssertAll());
151155
}
@@ -172,10 +176,11 @@ public async Task HttpTestAsserterAlternativeDurationPredicate_Success()
172176
.ResponseContentContains("id")
173177
.RequestDurationEquals(duration, (x) => (x > 100 && x < 1000))
174178
.ResponseStatusCodeEquals(HttpStatusCode.OK)
179+
.ResponseContentTypeEquals("application/json")
175180
.ResponseStatusCodeIsSuccess()
176181
.AssertAll();
177182

178-
Assert.Equal(5, assertResults.ToList().Count);
183+
Assert.Equal(6, assertResults.ToList().Count);
179184
foreach (var result in assertResults)
180185
{
181186
Assert.True(result.IsTrue, result.Message);

src/QAToolKit.Engine.HttpTester/HttpTestAsserter.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.Collections.Generic;
66
using System.Net;
77
using System.Net.Http;
8+
using System.Net.Http.Headers;
9+
using System.Net.Mime;
810

911
namespace QAToolKit.Engine.HttpTester
1012
{
@@ -59,6 +61,31 @@ public IHttpTestAsserter ResponseContentContains(string keyword, bool caseInsens
5961

6062
return this;
6163
}
64+
65+
/// <summary>
66+
/// Check if the response contains specified Content Type
67+
/// </summary>
68+
/// <param name="contentType"></param>
69+
/// <returns></returns>
70+
/// <exception cref="ArgumentNullException"></exception>
71+
public IHttpTestAsserter ResponseContentTypeEquals(string contentType)
72+
{
73+
if (contentType == null)
74+
{
75+
throw new ArgumentNullException($"{nameof(contentType)} is null.");
76+
}
77+
78+
var bodyString = _httpResponseMessage.Content.Headers.ContentType;
79+
80+
_assertResults.Add(new AssertResult()
81+
{
82+
Name = nameof(ResponseContentTypeEquals),
83+
Message = $"Response content-type equals '{contentType}'.",
84+
IsTrue = _httpResponseMessage.Content.Headers.ContentType.MediaType == contentType
85+
});
86+
87+
return this;
88+
}
6289

6390
/// <summary>
6491
/// Verify request duration

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Net;
5+
using System.Net.Http.Headers;
56

67
namespace QAToolKit.Engine.HttpTester.Interfaces
78
{
@@ -47,6 +48,12 @@ public interface IHttpTestAsserter
4748
/// <returns></returns>
4849
IHttpTestAsserter ResponseBodyIsEmpty();
4950
/// <summary>
51+
/// Check if the response contains specified Content Type
52+
/// </summary>
53+
/// <param name="contentType"></param>
54+
/// <returns></returns>
55+
IHttpTestAsserter ResponseContentTypeEquals(string contentType);
56+
/// <summary>
5057
/// Return all Assert messages of the Asserter
5158
/// </summary>
5259
/// <returns></returns>

0 commit comments

Comments
 (0)