Skip to content

Commit

Permalink
Add unit test for coverage purposes.
Browse files Browse the repository at this point in the history
  • Loading branch information
twitchax committed Aug 5, 2020
1 parent afa132d commit 8fdd471
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/Core/Extensions/Http.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ private static HttpRequestMessage CreateProxiedHttpRequest(this HttpContext cont
requestMessage.Content = request.Form.ToHttpContent(request.ContentType);
}
else
{
requestMessage.Content = new StreamContent(request.Body);
}
}

// Copy the request headers.
Expand Down
5 changes: 2 additions & 3 deletions src/Core/Helpers/Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;

namespace AspNetCore.Proxy
{
Expand Down Expand Up @@ -76,6 +74,7 @@ internal static string TrimTrailingSlashes(this string s)

internal static HttpContent ToHttpContent(this IFormCollection collection, string contentTypeHeader)
{
// @PreferLinux:
// Form content types resource: https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean/28380690
// There are three possible form content types:
// - text/plain, which should never be used and this does not handle (a request with that will not have IsFormContentType true anyway)
Expand All @@ -91,7 +90,7 @@ internal static HttpContent ToHttpContent(this IFormCollection collection, strin
return new FormUrlEncodedContent(collection.SelectMany(formItemList => formItemList.Value.Select(value => new KeyValuePair<string, string>(formItemList.Key, value))));

if (!contentType.MediaType.Equals("multipart/form-data", StringComparison.OrdinalIgnoreCase))
throw new Exception($"Unknown form content type \"{contentType.MediaType}\"");
throw new Exception($"Unknown form content type `{contentType.MediaType}`.");

// multipart/form-data specification https://tools.ietf.org/html/rfc7578
// It has each value separated by a boundary sequence, which is specified in the Content-Type header.
Expand Down
21 changes: 21 additions & 0 deletions src/Test/Unit/Other.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

using System;
using Xunit;

namespace AspNetCore.Proxy.Tests
{
public class Other
{
[Fact]
public void CanFailOnBadFormContentType()
{
var contentType = "text/plain";

var e = Assert.ThrowsAny<Exception>(() => {
var dummy = Helpers.ToHttpContent(null, contentType);
});

Assert.Equal($"Unknown form content type `{contentType}`.", e.Message);
}
}
}

0 comments on commit 8fdd471

Please sign in to comment.