diff --git a/Directory.Build.props b/Directory.Build.props index bf8f8fa..59ed3a3 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,5 @@ - 0.3.2 + 0.3.3 \ No newline at end of file diff --git a/README.md b/README.md index 9286598..7391949 100644 --- a/README.md +++ b/README.md @@ -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`: diff --git a/src/QAToolKit.Engine.HttpTester.Test/HttpTestAsserterTests.cs b/src/QAToolKit.Engine.HttpTester.Test/HttpTestAsserterTests.cs index 2bf9423..581f806 100644 --- a/src/QAToolKit.Engine.HttpTester.Test/HttpTestAsserterTests.cs +++ b/src/QAToolKit.Engine.HttpTester.Test/HttpTestAsserterTests.cs @@ -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; @@ -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(); @@ -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()); } @@ -146,6 +149,7 @@ public async Task HttpTestAsserterBodyNull_Fails() Assert.Throws(() => asserter .ResponseContentContains(null) .RequestDurationEquals(duration, (x) => x < 1000) + .ResponseContentTypeEquals("application/json") .ResponseStatusCodeEquals(HttpStatusCode.OK) .AssertAll()); } @@ -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); diff --git a/src/QAToolKit.Engine.HttpTester/HttpTestAsserter.cs b/src/QAToolKit.Engine.HttpTester/HttpTestAsserter.cs index 6564097..776ffb1 100644 --- a/src/QAToolKit.Engine.HttpTester/HttpTestAsserter.cs +++ b/src/QAToolKit.Engine.HttpTester/HttpTestAsserter.cs @@ -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 { @@ -59,6 +61,31 @@ public IHttpTestAsserter ResponseContentContains(string keyword, bool caseInsens return this; } + + /// + /// Check if the response contains specified Content Type + /// + /// + /// + /// + 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; + } /// /// Verify request duration diff --git a/src/QAToolKit.Engine.HttpTester/Interfaces/IHttpTestAsserter.cs b/src/QAToolKit.Engine.HttpTester/Interfaces/IHttpTestAsserter.cs index 0f795d0..d621001 100644 --- a/src/QAToolKit.Engine.HttpTester/Interfaces/IHttpTestAsserter.cs +++ b/src/QAToolKit.Engine.HttpTester/Interfaces/IHttpTestAsserter.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Net; +using System.Net.Http.Headers; namespace QAToolKit.Engine.HttpTester.Interfaces { @@ -47,6 +48,12 @@ public interface IHttpTestAsserter /// IHttpTestAsserter ResponseBodyIsEmpty(); /// + /// Check if the response contains specified Content Type + /// + /// + /// + IHttpTestAsserter ResponseContentTypeEquals(string contentType); + /// /// Return all Assert messages of the Asserter /// ///