Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasnordqvist committed Feb 26, 2018
2 parents 53d843d + 29b8d5f commit db492c5
Showing 1 changed file with 53 additions and 36 deletions.
89 changes: 53 additions & 36 deletions WebAnchor.Tests/Wiki/BuildingAnHttpRequest/MultipartContentTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -32,17 +33,17 @@ public void TestPostWithStringContent()
TestTheRequest<IApi>(
api => api.PostSingleMultipart(new StringContentPart("Content", "Field1", "test.txt") { ContentType = "text/plain" }),
assertMe =>
{
Assert.Equal(HttpMethod.Post, assertMe.Method);
Assert.Equal("api", assertMe.RequestUri.ToString());
var body = assertMe.Content.ReadAsStringAsync().Result;
Assert.Matches(@"--(?<id>[0-9a-f\-]*)
{
VerifyMultipartRequest(
assertMe,
@"--{boundary}
Content-Type: text/plain
Content-Disposition: form-data; name=Field1; filename=test\.txt; filename\*=utf-8''test\.txt
Content-Disposition: form-data; name=Field1; filename=test.txt; filename*=utf-8''test.txt
Content
--\k<id>--", body);
});
--{boundary}--
");
});
}

[Fact]
Expand All @@ -54,15 +55,15 @@ public void TestPostWithStreamContent()
api => api.PostSingleMultipart(new StreamContentPart(stream, "Field1", "test.txt") { ContentType = "text/plain" }),
assertMe =>
{
Assert.Equal(HttpMethod.Post, assertMe.Method);
Assert.Equal("api", assertMe.RequestUri.ToString());
var body = assertMe.Content.ReadAsStringAsync().Result;
Assert.Matches(@"--(?<id>[0-9a-f\-]*)
VerifyMultipartRequest(
assertMe,
@"--{boundary}
Content-Type: text/plain
Content-Disposition: form-data; name=Field1; filename=test\.txt; filename\*=utf-8''test\.txt
Content-Disposition: form-data; name=Field1; filename=test.txt; filename*=utf-8''test.txt
From a stream
--\k<id>--", body);
--{boundary}--
");
});
}

Expand All @@ -73,15 +74,15 @@ public void TestPostWithByteArrayContent()
api => api.PostSingleMultipart(new ByteArrayContentPart(Encoding.UTF8.GetBytes("From bytes"), "Field1", "test.txt") { ContentType = "text/plain" }),
assertMe =>
{
Assert.Equal(HttpMethod.Post, assertMe.Method);
Assert.Equal("api", assertMe.RequestUri.ToString());
var body = assertMe.Content.ReadAsStringAsync().Result;
Assert.Matches(@"--(?<id>[0-9a-f\-]*)
VerifyMultipartRequest(
assertMe,
@"--{boundary}
Content-Type: text/plain
Content-Disposition: form-data; name=Field1; filename=test\.txt; filename\*=utf-8''test\.txt
Content-Disposition: form-data; name=Field1; filename=test.txt; filename*=utf-8''test.txt
From bytes
--\k<id>--", body);
--{boundary}--
");
});
}

Expand All @@ -94,20 +95,20 @@ public void TestPostWithParamArrayContentParts()
new ByteArrayContentPart(Encoding.UTF8.GetBytes("From bytes"), "Field1", "test.txt") { ContentType = "text/plain" }),
assertMe =>
{
Assert.Equal(HttpMethod.Post, assertMe.Method);
Assert.Equal("api", assertMe.RequestUri.ToString());
var body = assertMe.Content.ReadAsStringAsync().Result;
Assert.Matches(@"--(?<id>[0-9a-f\-]*)
VerifyMultipartRequest(
assertMe,
@"--{boundary}
Content-Type: text/plain
Content-Disposition: form-data; name=Field1; filename=test\.txt; filename\*=utf-8''test\.txt
Content-Disposition: form-data; name=Field1; filename=test.txt; filename*=utf-8''test.txt
Content
--\k<id>
--{boundary}
Content-Type: text/plain
Content-Disposition: form-data; name=Field1; filename=test\.txt; filename\*=utf-8''test\.txt
Content-Disposition: form-data; name=Field1; filename=test.txt; filename*=utf-8''test.txt
From bytes
--\k<id>--", body);
--{boundary}--
");
});
}

Expand All @@ -124,23 +125,39 @@ public void TestPostWithEnumerableTypeContentParts()
api => api.PostListMultipart(listData),
assertMe =>
{
Assert.Equal(HttpMethod.Post, assertMe.Method);
Assert.Equal("api", assertMe.RequestUri.ToString());
var body = assertMe.Content.ReadAsStringAsync().Result;
Assert.Matches(@"--(?<id>[0-9a-f\-]*)
VerifyMultipartRequest(
assertMe,
@"--{boundary}
Content-Type: text/plain
Content-Disposition: form-data; name=Field1; filename=test\.txt; filename\*=utf-8''test\.txt
Content-Disposition: form-data; name=Field1; filename=test.txt; filename*=utf-8''test.txt
Content
--\k<id>
--{boundary}
Content-Type: text/plain
Content-Disposition: form-data; name=Field1; filename=test\.txt; filename\*=utf-8''test\.txt
Content-Disposition: form-data; name=Field1; filename=test.txt; filename*=utf-8''test.txt
From bytes
--\k<id>--", body);
--{boundary}--
");
});
}

private static void VerifyMultipartRequest(HttpRequestMessage assertMe, string expectedContent)
{
Assert.Equal(HttpMethod.Post, assertMe.Method);
Assert.Equal("api", assertMe.RequestUri.ToString());

var boundary = assertMe.Content.Headers.ContentType.Parameters.SingleOrDefault(p => p.Name == "boundary")?.Value;
Assert.NotNull(boundary);
Assert.Matches(@"^""[0-9A-Fa-f]{8}[-]?([0-9A-Fa-f]{4}[-]?){3}[0-9A-Fa-f]{12}""$", boundary);

var body = assertMe.Content.ReadAsStringAsync().Result;
Assert.Equal(
expectedContent.Replace("{boundary}", boundary.Trim('"')),
body,
ignoreLineEndingDifferences: true);
}

public class Post
{
public int Id { get; set; }
Expand Down

0 comments on commit db492c5

Please sign in to comment.