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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.IO;
using System.Net.Http;
#if !NET8_0_OR_GREATER
using System.Linq;
Expand Down Expand Up @@ -77,7 +78,14 @@ protected override bool Matches(HttpContent value)

if (_body is not null)
{
byte[] content = value.ReadAsByteArrayAsync().ConfigureAwait(false).GetAwaiter().GetResult();
#if NET8_0_OR_GREATER
Stream stream = value.ReadAsStream();
#else
Stream stream = value.ReadAsStreamAsync().ConfigureAwait(false).GetAwaiter().GetResult();
#endif
using MemoryStream ms = new();
stream.CopyTo(ms);
byte[] content = ms.ToArray();
switch (_bodyMatchType)
{
case BodyMatchType.Exact when
Expand Down
14 changes: 8 additions & 6 deletions Source/Mockolate/Web/ItExtensions.HttpContent.IsStringContent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Net.Http;
using System.Text.RegularExpressions;
using Mockolate.Internals;
Expand Down Expand Up @@ -120,14 +121,15 @@ protected override bool Matches(HttpContent value)
return false;
}

if (value is not StringContent)
{
return false;
}

if (_body is not null)
{
string content = value.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
#if NET8_0_OR_GREATER
Stream stream = value.ReadAsStream();
#else
Stream stream = value.ReadAsStreamAsync().ConfigureAwait(false).GetAwaiter().GetResult();
#endif
using StreamReader reader = new(stream);
string content = reader.ReadToEnd();
switch (_bodyMatchType)
{
case BodyMatchType.Exact when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ public sealed partial class ItExtensionsTests
{
public sealed class IsStringContentTests
{
[Fact]
public async Task ShouldNotCheckHttpContentType()
{
string expectedValue = "foo";
byte[] bytes = Encoding.UTF8.GetBytes(expectedValue);
HttpClient httpClient = Mock.Create<HttpClient>();
httpClient.SetupMock.Method
.PostAsync(It.IsAny<Uri>(), It.IsStringContent().WithBody("foo"))
.ReturnsAsync(new HttpResponseMessage(HttpStatusCode.OK));

HttpResponseMessage result = await httpClient.PostAsync("https://www.aweXpect.com",
new ByteArrayContent(bytes),
CancellationToken.None);

await That(result.StatusCode).IsEqualTo(HttpStatusCode.OK);
}
#if !NETFRAMEWORK
[Fact]
public async Task ShouldSupportMonitoring()
Expand Down
14 changes: 14 additions & 0 deletions Tests/Mockolate.Tests/Web/ItExtensionsTests.UriTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ await That(result.StatusCode)
.IsEqualTo(HttpStatusCode.NotImplemented);
}

[Theory]
[InlineData("https://www.aweXpect.com")]
[InlineData(443)]
[InlineData(null)]
public async Task WhenTypeDoesNotMatch_ShouldReturnFalse(object? value)
Comment thread
vbreuss marked this conversation as resolved.
{
ItExtensions.IUriParameter sut = It.IsUri();
IParameter parameter = (IParameter)sut;

bool result = parameter.Matches(value);

await That(result).IsFalse();
}

[Theory]
[InlineData("https://www.aweXpect.com/foo/bar?x=123&y=234", "www.awexpect.com", true)]
[InlineData("https://www.aweXpect.com/foo/bar?x=123&y=234", "*awexpect*", true)]
Expand Down
Loading