Skip to content

Commit 00563ce

Browse files
committed
Added options to test string content of HTTP response message and deeply equal object content of HTTP response message (#168)
1 parent cd69c8a commit 00563ce

File tree

13 files changed

+209
-35
lines changed

13 files changed

+209
-35
lines changed

src/MyWebApi.Tests/BuildersTests/HttpMessagesTests/HttpHandlerResponseMessageTestBuilderTests.cs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ public void WithResponseModelShouldNotThrowExceptionWithCorrectResponseModel()
6767
}
6868

6969
[Test]
70-
[ExpectedException(
71-
typeof(ResponseModelAssertionException),
72-
ExpectedMessage = "When testing ResponseMessageHandler expected HTTP response message model to be the given model, but in fact it was a different model.")]
73-
public void WithResponseModelShouldThrowExceptionWithIncorrectResponseModel()
70+
public void WithResponseModelShouldNotThrowExceptionWithDeeplyEqualResponseModel()
7471
{
7572
MyWebApi
7673
.Handler<ResponseMessageHandler>()
@@ -79,6 +76,22 @@ public void WithResponseModelShouldThrowExceptionWithIncorrectResponseModel()
7976
.WithResponseModel(TestObjectFactory.GetListOfResponseModels());
8077
}
8178

79+
[Test]
80+
[ExpectedException(
81+
typeof(ResponseModelAssertionException),
82+
ExpectedMessage = "When testing ResponseMessageHandler expected HTTP response message model to be the given model, but in fact it was a different model.")]
83+
public void WithResponseModelShouldThrowExceptionWithDeeplyUnequalResponseModel()
84+
{
85+
var another = TestObjectFactory.GetListOfResponseModels();
86+
another.Add(new ResponseModel());
87+
88+
MyWebApi
89+
.Handler<ResponseMessageHandler>()
90+
.WithHttpRequestMessage(new HttpRequestMessage())
91+
.ShouldReturnHttpResponseMessage()
92+
.WithResponseModel(another);
93+
}
94+
8295
[Test]
8396
public void WithContentOfTypeShouldNotThrowExceptionWithCorrectContent()
8497
{
@@ -102,6 +115,35 @@ public void WithContentOfTypeShouldThrowExceptionWithIncorrectContent()
102115
.WithContentOfType<StreamContent>();
103116
}
104117

118+
[Test]
119+
public void WithStringContentOfTypeShouldNotThrowExceptionWithCorrectContent()
120+
{
121+
var request = new HttpRequestMessage();
122+
request.Headers.Add("StringContent", "StringContent");
123+
124+
MyWebApi
125+
.Handler<ResponseMessageHandler>()
126+
.WithHttpRequestMessage(request)
127+
.ShouldReturnHttpResponseMessage()
128+
.WithStringContent("Test string");
129+
}
130+
131+
[Test]
132+
[ExpectedException(
133+
typeof(HttpResponseMessageAssertionException),
134+
ExpectedMessage = "When testing ResponseMessageHandler expected HTTP response message result string content to be 'Another string', but was in fact 'Test string'.")]
135+
public void WithStringContentOfTypeShouldThrowExceptionWithIncorrectContent()
136+
{
137+
var request = new HttpRequestMessage();
138+
request.Headers.Add("StringContent", "StringContent");
139+
140+
MyWebApi
141+
.Handler<ResponseMessageHandler>()
142+
.WithHttpRequestMessage(request)
143+
.ShouldReturnHttpResponseMessage()
144+
.WithStringContent("Another string");
145+
}
146+
105147
[Test]
106148
public void WithMediaTypeFormatterShouldNotThrowExceptionWithCorrectMediaTypeFormatter()
107149
{

src/MyWebApi.Tests/BuildersTests/HttpMessagesTests/HttpResponseMessageTestBuilderTests.cs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ public void WithResponseModelShouldNotThrowExceptionWithCorrectResponseModel()
7070
}
7171

7272
[Test]
73-
[ExpectedException(
74-
typeof(ResponseModelAssertionException),
75-
ExpectedMessage = "When calling HttpResponseMessageAction action in WebApiController expected HTTP response message model to be the given model, but in fact it was a different model.")]
76-
public void WithResponseModelShouldThrowExceptionWithIncorrectResponseModel()
73+
public void WithResponseModelShouldNotThrowExceptionWithDeeplyEqualResponseModel()
7774
{
7875
MyWebApi
7976
.Controller<WebApiController>()
@@ -83,6 +80,23 @@ public void WithResponseModelShouldThrowExceptionWithIncorrectResponseModel()
8380
.WithResponseModel(TestObjectFactory.GetListOfResponseModels());
8481
}
8582

83+
[Test]
84+
[ExpectedException(
85+
typeof(ResponseModelAssertionException),
86+
ExpectedMessage = "When calling HttpResponseMessageAction action in WebApiController expected HTTP response message model to be the given model, but in fact it was a different model.")]
87+
public void WithResponseModelShouldThrowExceptionWithDeeplyUnequalResponseModel()
88+
{
89+
var another = TestObjectFactory.GetListOfResponseModels();
90+
another.Add(new ResponseModel());
91+
92+
MyWebApi
93+
.Controller<WebApiController>()
94+
.Calling(c => c.HttpResponseMessageAction())
95+
.ShouldReturn()
96+
.HttpResponseMessage()
97+
.WithResponseModel(another);
98+
}
99+
86100
[Test]
87101
public void WithContentOfTypeShouldNotThrowExceptionWithCorrectContent()
88102
{
@@ -108,6 +122,37 @@ public void WithContentOfTypeShouldThrowExceptionWithIncorrectContent()
108122
.WithContentOfType<StreamContent>();
109123
}
110124

125+
[Test]
126+
public void WithStringContentOfTypeShouldNotThrowExceptionWithCorrectContent()
127+
{
128+
var request = new HttpRequestMessage();
129+
request.Headers.Add("StringContent", "StringContent");
130+
131+
MyWebApi
132+
.Controller<WebApiController>()
133+
.Calling(c => c.HttpResponseMessageWithStringContent())
134+
.ShouldReturn()
135+
.HttpResponseMessage()
136+
.WithStringContent("Test string");
137+
}
138+
139+
[Test]
140+
[ExpectedException(
141+
typeof(HttpResponseMessageAssertionException),
142+
ExpectedMessage = "When calling HttpResponseMessageWithStringContent action in WebApiController expected HTTP response message result string content to be 'Another string', but was in fact 'Test string'.")]
143+
public void WithStringContentOfTypeShouldThrowExceptionWithIncorrectContent()
144+
{
145+
var request = new HttpRequestMessage();
146+
request.Headers.Add("StringContent", "StringContent");
147+
148+
MyWebApi
149+
.Controller<WebApiController>()
150+
.Calling(c => c.HttpResponseMessageWithStringContent())
151+
.ShouldReturn()
152+
.HttpResponseMessage()
153+
.WithStringContent("Another string");
154+
}
155+
111156
[Test]
112157
public void WithMediaTypeFormatterShouldNotThrowExceptionWithCorrectMediaTypeFormatter()
113158
{

src/MyWebApi.Tests/BuildersTests/ModelsTests/ResponseModelTestBuilderTests.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace MyWebApi.Tests.BuildersTests.ModelsTests
1818
{
1919
using System.Collections.Generic;
20+
using System.Linq;
2021
using Exceptions;
2122
using NUnit.Framework;
2223
using Setups.Controllers;
@@ -113,20 +114,36 @@ public void WithResponseModelShouldNotThrowExceptionWithCorrectPassedExpectedObj
113114
.WithResponseModel(controller.ResponseModel);
114115
}
115116

117+
[Test]
118+
public void WithResponceModelShouldNotThrowExceptionWithDeeplyEqualPassedExpectedObject()
119+
{
120+
var controller = new WebApiController();
121+
122+
MyWebApi
123+
.Controller<WebApiController>()
124+
.Calling(c => c.OkResultWithResponse())
125+
.ShouldReturn()
126+
.Ok()
127+
.WithResponseModel(controller.ResponseModel);
128+
}
129+
116130
[Test]
117131
[ExpectedException(
118132
typeof(ResponseModelAssertionException),
119-
ExpectedMessage = "When calling OkResultWithResponse action in WebApiController expected response model ICollection<ResponseModel> to be the given model, but in fact it was a different model.")]
133+
ExpectedMessage = "When calling OkResultWithResponse action in WebApiController expected response model List<ResponseModel> to be the given model, but in fact it was a different model.")]
120134
public void WithResponceModelShouldThrowExceptionWithDifferentPassedExpectedObject()
121135
{
122136
var controller = new WebApiController();
123137

138+
var another = controller.ResponseModel.ToList();
139+
another.Add(new ResponseModel());
140+
124141
MyWebApi
125142
.Controller<WebApiController>()
126143
.Calling(c => c.OkResultWithResponse())
127144
.ShouldReturn()
128145
.Ok()
129-
.WithResponseModel(controller.ResponseModel);
146+
.WithResponseModel(another);
130147
}
131148

132149
[Test]

src/MyWebApi.Tests/BuildersTests/ServersTests/ServerTestBuilderTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public void HttpTestsShouldWorkCorrectlyWithGlobalTestServer()
7979
.ShouldReturnHttpResponseMessage()
8080
.WithStatusCode(HttpStatusCode.OK)
8181
.AndAlso()
82-
.WithResponseModelOfType<int>();
82+
.WithResponseModelOfType<int>()
83+
.Passing(m => m == 5);
8384

8485
MyWebApi
8586
.Server()

src/MyWebApi.Tests/Setups/Controllers/WebApiController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ public HttpResponseMessage HttpResponseMessageAction()
117117
return response;
118118
}
119119

120+
public HttpResponseMessage HttpResponseMessageWithStringContent()
121+
{
122+
return new HttpResponseMessage
123+
{
124+
Content = new StringContent("Test string")
125+
};
126+
}
127+
120128
public HttpResponseMessage HttpResponseMessageWithResponseModelAction()
121129
{
122130
return this.Request.CreateResponse(HttpStatusCode.BadRequest, this.responseModel);

src/MyWebApi.Tests/Setups/Handlers/ResponseMessageHandler.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
4343
return Task.Run(
4444
() =>
4545
{
46+
if (request.Headers.Contains("StringContent"))
47+
{
48+
return new HttpResponseMessage
49+
{
50+
Content = new StringContent("Test string")
51+
};
52+
}
53+
4654
if (request.Headers.Contains("NoContent"))
4755
{
4856
return new HttpResponseMessage(HttpStatusCode.NoContent);;

src/MyWebApi/Builders/Contracts/HttpResponseMessages/IHttpHandlerResponseMessageTestBuilder.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,19 @@ namespace MyWebApi.Builders.Contracts.HttpResponseMessages
3030
public interface IHttpHandlerResponseMessageTestBuilder : IBaseHandlerTestBuilder
3131
{
3232
/// <summary>
33-
/// Tests whether certain type of response model is returned from the HTTP response message content.
33+
/// Tests whether certain type of response model is returned from the HTTP response message object content.
3434
/// </summary>
3535
/// <typeparam name="TResponseModel">Type of the response model.</typeparam>
3636
/// <returns>Builder for testing the response model errors.</returns>
3737
IHttpHandlerModelDetailsTestBuilder<TResponseModel> WithResponseModelOfType<TResponseModel>();
3838

3939
/// <summary>
40-
/// Tests whether an object is returned from the HTTP response message content.
40+
/// Tests whether a deeply equal object to the provided one is returned from the HTTP response message object content.
4141
/// </summary>
4242
/// <typeparam name="TResponseModel">Type of the response model.</typeparam>
4343
/// <param name="expectedModel">Expected model to be returned.</param>
4444
/// <returns>Builder for testing the response model errors.</returns>
45-
IHttpHandlerModelDetailsTestBuilder<TResponseModel> WithResponseModel<TResponseModel>(TResponseModel expectedModel)
46-
where TResponseModel : class;
45+
IHttpHandlerModelDetailsTestBuilder<TResponseModel> WithResponseModel<TResponseModel>(TResponseModel expectedModel);
4746

4847
/// <summary>
4948
/// Tests whether the content of the HTTP response message is of certain type.
@@ -53,6 +52,13 @@ IHttpHandlerModelDetailsTestBuilder<TResponseModel> WithResponseModel<TResponseM
5352
IAndHttpHandlerResponseMessageTestBuilder WithContentOfType<TContentType>()
5453
where TContentType : HttpContent;
5554

55+
/// <summary>
56+
/// Tests whether the content of the HTTP response message is the provided string.
57+
/// </summary>
58+
/// <param name="content">Expected string content.</param>
59+
/// <returns>The same HTTP response message test builder.</returns>
60+
IAndHttpHandlerResponseMessageTestBuilder WithStringContent(string content);
61+
5662
/// <summary>
5763
/// Tests whether the HTTP response message has the provided media type formatter.
5864
/// </summary>

src/MyWebApi/Builders/Contracts/HttpResponseMessages/IHttpResponseMessageTestBuilder.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,19 @@ namespace MyWebApi.Builders.Contracts.HttpResponseMessages
3030
public interface IHttpResponseMessageTestBuilder : IBaseTestBuilderWithCaughtException
3131
{
3232
/// <summary>
33-
/// Tests whether certain type of response model is returned from the HTTP response message content.
33+
/// Tests whether certain type of response model is returned from the HTTP response message object content.
3434
/// </summary>
3535
/// <typeparam name="TResponseModel">Type of the response model.</typeparam>
3636
/// <returns>Builder for testing the response model errors.</returns>
3737
IModelDetailsTestBuilder<TResponseModel> WithResponseModelOfType<TResponseModel>();
3838

3939
/// <summary>
40-
/// Tests whether an object is returned from the HTTP response message content.
40+
/// Tests whether a deeply equal object to the provided one is returned from the HTTP response message object content.
4141
/// </summary>
4242
/// <typeparam name="TResponseModel">Type of the response model.</typeparam>
4343
/// <param name="expectedModel">Expected model to be returned.</param>
4444
/// <returns>Builder for testing the response model errors.</returns>
45-
IModelDetailsTestBuilder<TResponseModel> WithResponseModel<TResponseModel>(TResponseModel expectedModel)
46-
where TResponseModel : class;
45+
IModelDetailsTestBuilder<TResponseModel> WithResponseModel<TResponseModel>(TResponseModel expectedModel);
4746

4847
/// <summary>
4948
/// Tests whether the content of the HTTP response message is of certain type.
@@ -53,6 +52,13 @@ IModelDetailsTestBuilder<TResponseModel> WithResponseModel<TResponseModel>(TResp
5352
IAndHttpResponseMessageTestBuilder WithContentOfType<TContentType>()
5453
where TContentType : HttpContent;
5554

55+
/// <summary>
56+
/// Tests whether the content of the HTTP response message is the provided string.
57+
/// </summary>
58+
/// <param name="content">Expected string content.</param>
59+
/// <returns>The same HTTP response message test builder.</returns>
60+
IAndHttpResponseMessageTestBuilder WithStringContent(string content);
61+
5662
/// <summary>
5763
/// Tests whether the HTTP response message has the provided media type formatter.
5864
/// </summary>

src/MyWebApi/Builders/Contracts/Models/IBaseResponseModelTestBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ public interface IBaseResponseModelTestBuilder : IBaseTestBuilderWithCaughtExcep
3131
IModelDetailsTestBuilder<TResponseModel> WithResponseModelOfType<TResponseModel>();
3232

3333
/// <summary>
34-
/// Tests whether an object is returned from the invoked action.
34+
/// Tests whether a deeply equal object to the provided one is returned from the invoked action.
3535
/// </summary>
3636
/// <typeparam name="TResponseModel">Type of the response model.</typeparam>
3737
/// <param name="expectedModel">Expected model to be returned.</param>
3838
/// <returns>Builder for testing the response model errors.</returns>
39-
IModelDetailsTestBuilder<TResponseModel> WithResponseModel<TResponseModel>(TResponseModel expectedModel)
40-
where TResponseModel : class;
39+
IModelDetailsTestBuilder<TResponseModel> WithResponseModel<TResponseModel>(TResponseModel expectedModel);
4140
}
4241
}

src/MyWebApi/Builders/HttpMessages/HttpHandlerResponseMessageTestBuilder.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,12 @@ public IHttpHandlerModelDetailsTestBuilder<TResponseModel> WithResponseModelOfTy
6969
}
7070

7171
/// <summary>
72-
/// Tests whether an object is returned from the invoked HTTP response message content.
72+
/// Tests whether a deeply equal object to the provided one is returned from the invoked HTTP response message content.
7373
/// </summary>
7474
/// <typeparam name="TResponseModel">Type of the response model.</typeparam>
7575
/// <param name="expectedModel">Expected model to be returned.</param>
7676
/// <returns>Builder for testing the response model errors.</returns>
7777
public IHttpHandlerModelDetailsTestBuilder<TResponseModel> WithResponseModel<TResponseModel>(TResponseModel expectedModel)
78-
where TResponseModel : class
7978
{
8079
var actualModel = HttpResponseMessageValidator.WithResponseModel(
8180
this.httpResponseMessage.Content,
@@ -104,6 +103,21 @@ public IAndHttpHandlerResponseMessageTestBuilder WithContentOfType<TContentType>
104103
return this;
105104
}
106105

106+
/// <summary>
107+
/// Tests whether the content of the HTTP response message is the provided string.
108+
/// </summary>
109+
/// <param name="content">Expected string content.</param>
110+
/// <returns>The same HTTP response message test builder.</returns>
111+
public IAndHttpHandlerResponseMessageTestBuilder WithStringContent(string content)
112+
{
113+
HttpResponseMessageValidator.WithStringContent(
114+
this.httpResponseMessage.Content,
115+
content,
116+
this.ThrowNewHttpResponseMessageAssertionException);
117+
118+
return this;
119+
}
120+
107121
/// <summary>
108122
/// Tests whether the HTTP response message has the provided media type formatter.
109123
/// </summary>

0 commit comments

Comments
 (0)