Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>0.3.2</Version>
<Version>0.3.3</Version>
</PropertyGroup>
</Project>
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,13 @@ You can pass `X509Certificate2` objet to the `WithCertificateAuthentication` met
This is an implementation of the HTTP response message asserter, which can be used to assert different parameters.

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

Asserter produces a list of `AssertResult`:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -34,6 +35,7 @@ public async Task HttpTestAsserterSimple_Success()
.ResponseContentContains("scott")
.RequestDurationEquals(duration, (x) => x < 2000)
.ResponseStatusCodeEquals(HttpStatusCode.OK)
.ResponseContentTypeEquals("application/json")
.ResponseHasHttpHeader("Date")
.AssertAll();

Expand Down Expand Up @@ -121,6 +123,7 @@ public async Task HttpTestAsserterHeaderMissing_Fails()
.RequestDurationEquals(duration, (x) => x < 2000)
.RequestDurationEquals(httpDuration, (x) => x < 1800)
.ResponseStatusCodeEquals(HttpStatusCode.OK)
.ResponseContentTypeEquals("application/json")
.ResponseHasHttpHeader(null)
.AssertAll());
}
Expand All @@ -146,6 +149,7 @@ public async Task HttpTestAsserterBodyNull_Fails()
Assert.Throws<ArgumentNullException>(() => asserter
.ResponseContentContains(null)
.RequestDurationEquals(duration, (x) => x < 1000)
.ResponseContentTypeEquals("application/json")
.ResponseStatusCodeEquals(HttpStatusCode.OK)
.AssertAll());
}
Expand All @@ -172,10 +176,11 @@ public async Task HttpTestAsserterAlternativeDurationPredicate_Success()
.ResponseContentContains("id")
.RequestDurationEquals(duration, (x) => (x > 100 && x < 1000))
.ResponseStatusCodeEquals(HttpStatusCode.OK)
.ResponseContentTypeEquals("application/json")
.ResponseStatusCodeIsSuccess()
.AssertAll();

Assert.Equal(5, assertResults.ToList().Count);
Assert.Equal(6, assertResults.ToList().Count);
foreach (var result in assertResults)
{
Assert.True(result.IsTrue, result.Message);
Expand Down
27 changes: 27 additions & 0 deletions src/QAToolKit.Engine.HttpTester/HttpTestAsserter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.Mime;

namespace QAToolKit.Engine.HttpTester
{
Expand Down Expand Up @@ -59,6 +61,31 @@ public IHttpTestAsserter ResponseContentContains(string keyword, bool caseInsens

return this;
}

/// <summary>
/// Check if the response contains specified Content Type
/// </summary>
/// <param name="contentType"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public IHttpTestAsserter ResponseContentTypeEquals(string contentType)
{
if (contentType == null)
{
throw new ArgumentNullException($"{nameof(contentType)} is null.");
}

var bodyString = _httpResponseMessage.Content.Headers.ContentType;

_assertResults.Add(new AssertResult()
{
Name = nameof(ResponseContentTypeEquals),
Message = $"Response content-type equals '{contentType}'.",
IsTrue = _httpResponseMessage.Content.Headers.ContentType.MediaType == contentType
});

return this;
}

/// <summary>
/// Verify request duration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http.Headers;

namespace QAToolKit.Engine.HttpTester.Interfaces
{
Expand Down Expand Up @@ -47,6 +48,12 @@ public interface IHttpTestAsserter
/// <returns></returns>
IHttpTestAsserter ResponseBodyIsEmpty();
/// <summary>
/// Check if the response contains specified Content Type
/// </summary>
/// <param name="contentType"></param>
/// <returns></returns>
IHttpTestAsserter ResponseContentTypeEquals(string contentType);
/// <summary>
/// Return all Assert messages of the Asserter
/// </summary>
/// <returns></returns>
Expand Down