Skip to content

Commit 45a6b79

Browse files
Add WithStringBody() assertion.
1 parent e18b6d4 commit 45a6b79

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

src/Testing.Azure.Functions.Http/HttpResponseDataAssertions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace PosInformatique.Testing.Azure.Functions.Http
88
{
9+
using System.Text;
910
using System.Text.Json;
1011
using global::FluentAssertions;
1112
using global::FluentAssertions.Common;
@@ -77,5 +78,32 @@ public HttpResponseDataAssertions WithJsonBody<T>(T expectedJsonObject, JsonSeri
7778

7879
return this;
7980
}
81+
82+
/// <summary>
83+
/// Assert the content of the <see cref="HttpResponseData.Body"/> to check if it is
84+
/// string encoded with the specified <paramref name="encoding"/>.
85+
/// </summary>
86+
/// <param name="expectedString">Expected string encoded.</param>
87+
/// <param name="encoding">Encoding of the string to check. If <see langword="null"/>, the UTF-8 encoding is used.</param>
88+
/// <returns>The current instance of the <see cref="HttpResponseDataAssertions"/> to continue the assertions.</returns>
89+
public HttpResponseDataAssertions WithStringBody(string expectedString, Encoding? encoding = null)
90+
{
91+
if (encoding is null)
92+
{
93+
encoding = Encoding.UTF8;
94+
}
95+
96+
var bodyStream = this.response.GetBodyStream();
97+
98+
using var memoryStream = new MemoryStream();
99+
100+
bodyStream.CopyTo(memoryStream);
101+
102+
var actualString = encoding.GetString(memoryStream.ToArray());
103+
104+
actualString.Should().Be(expectedString);
105+
106+
return this;
107+
}
80108
}
81109
}

tests/Testing.Azure.Functions.Http.Tests/HttpResponseDataAssertionsTest.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace PosInformatique.Testing.Azure.Functions.Http.Tests
88
{
9+
using System.Text;
910
using System.Text.Json;
1011
using Xunit.Sdk;
1112

@@ -141,5 +142,79 @@ public void WithEmptyBody_Failed()
141142

142143
response.Body.Position.Should().Be(position);
143144
}
145+
146+
[Fact]
147+
public void WithStringBody()
148+
{
149+
var response = new HttpResponseDataImplementation(new FunctionContextImplementation(new HttpRequestDataMock()));
150+
response.Body.Write(Encoding.Unicode.GetBytes("The string"));
151+
152+
var assertions = new HttpResponseDataAssertions(response);
153+
154+
var position = response.Body.Position;
155+
156+
assertions.WithStringBody("The string", Encoding.Unicode)
157+
.Should().BeSameAs(assertions);
158+
159+
response.Body.Position.Should().Be(position);
160+
}
161+
162+
[Fact]
163+
public void WithStringBody_DefaultEncoding()
164+
{
165+
var response = new HttpResponseDataImplementation(new FunctionContextImplementation(new HttpRequestDataMock()));
166+
response.Body.Write(Encoding.UTF8.GetBytes("The string"));
167+
168+
var assertions = new HttpResponseDataAssertions(response);
169+
170+
var position = response.Body.Position;
171+
172+
assertions.WithStringBody("The string")
173+
.Should().BeSameAs(assertions);
174+
175+
response.Body.Position.Should().Be(position);
176+
}
177+
178+
[Fact]
179+
public void WithStringBody_DifferentValue_Failed()
180+
{
181+
var response = new HttpResponseDataImplementation(new FunctionContextImplementation(new HttpRequestDataMock()));
182+
response.Body.Write(Encoding.Unicode.GetBytes("The other string"));
183+
184+
var assertions = new HttpResponseDataAssertions(response);
185+
186+
var position = response.Body.Position;
187+
188+
var act = () =>
189+
{
190+
assertions.WithStringBody("The string", Encoding.Unicode);
191+
};
192+
193+
act.Should().ThrowExactly<XunitException>()
194+
.WithMessage("Expected actualString to be \"The string\" with a length of 10, but \"The other string\" has a length of 16, differs near \"oth\" (index 4).");
195+
196+
response.Body.Position.Should().Be(position);
197+
}
198+
199+
[Fact]
200+
public void WithStringBody_DifferentEncoding_Failed()
201+
{
202+
var response = new HttpResponseDataImplementation(new FunctionContextImplementation(new HttpRequestDataMock()));
203+
response.Body.Write(Encoding.UTF8.GetBytes("The other string"));
204+
205+
var assertions = new HttpResponseDataAssertions(response);
206+
207+
var position = response.Body.Position;
208+
209+
var act = () =>
210+
{
211+
assertions.WithStringBody("The string", Encoding.Unicode);
212+
};
213+
214+
act.Should().ThrowExactly<XunitException>()
215+
.WithMessage("Expected actualString to be \"The string\" with a length of 10, but \"\u2065瑯敨\u2072瑳楲杮\" has a length of 8, differs near \"\u2065\" (index 0).");
216+
217+
response.Body.Position.Should().Be(position);
218+
}
144219
}
145220
}

0 commit comments

Comments
 (0)