Skip to content

Commit

Permalink
Merge pull request #8 from campersau/master
Browse files Browse the repository at this point in the history
fix multipart serializer
  • Loading branch information
jgauffin committed Nov 11, 2014
2 parents 24e9e2a + 6fc5744 commit 1e22930
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<Compile Include="Net\Protocols\Http\Messages\HeaderParserTests.cs" />
<Compile Include="Net\Protocols\Http\Messages\HttpCookieParserTests.cs" />
<Compile Include="Net\Protocols\Http\Messages\HttpMessageDecoderTests.cs" />
<Compile Include="Net\Protocols\Http\BodyDecoders\MultipartSerializerTests.cs" />
<Compile Include="Net\Protocols\Http\BodyDecoders\UrlDecoderTests.cs" />
<Compile Include="Net\Protocols\Http\HttpEncoderTests.cs" />
<Compile Include="Net\Protocols\Http\HttpMessageDecoderTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.IO;
using System.Text;
using Griffin.Net.Protocols.Http.Serializers;
using Xunit;

namespace Griffin.Core.Tests.Net.Protocols.Http.BodyDecoders
{
public class MultipartSerializerTests
{
[Fact]
public void DecodeFile()
{
var contentType = "multipart/form-data; boundary=AaB03x";
var message = "--AaB03x\r\nContent-Disposition: form-data; name=\"hello\"\r\n\r\nworld\r\n--AaB03x\r\nContent-Disposition: form-data; name=\"file\"; filename=\"file.txt\"\r\nContent-Type: text/plain\r\n\r\nhello world\r\n--AaB03x--";
var bytes = Encoding.ASCII.GetBytes(message);
var body = new MemoryStream(bytes);

var decoder = new MultipartSerializer();
var result = decoder.Deserialize(contentType, body);

Assert.Equal("world", ((FormAndFilesResult)result).Form["hello"]);
Assert.Equal("file", ((FormAndFilesResult)result).Files["file"].Name);
Assert.Equal("file.txt", ((FormAndFilesResult)result).Files["file"].OriginalFileName);
Assert.Equal("text/plain", ((FormAndFilesResult)result).Files["file"].ContentType);
Assert.Equal("hello world", File.ReadAllText(((FormAndFilesResult)result).Files["file"].TempFileName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ public void Serialize(object source, Stream destination, out string contentType)
/// <exception cref="SerializationException">Deserialization failed</exception>
public object Deserialize(string contentType, Stream source)
{
if (!contentType.EndsWith(MimeType))
if (contentType == null) throw new ArgumentNullException("contentType");
if (source == null) throw new ArgumentNullException("source");

if (!contentType.StartsWith(MimeType, StringComparison.OrdinalIgnoreCase))
return null;

var result = new FormAndFilesResult()
Expand All @@ -67,7 +70,7 @@ public object Deserialize(string contentType, Stream source)
};
var contentTypeHeader = new HttpHeaderValue(contentType);
var encodingStr = contentTypeHeader.Parameters["charset"];
var encoding = Encoding.GetEncoding(encodingStr);
var encoding = encodingStr != null ? Encoding.GetEncoding(encodingStr) : Encoding.UTF8;

//multipart/form-data, boundary=AaB03x
var boundry = contentTypeHeader.Parameters.Get("boundary");
Expand Down

0 comments on commit 1e22930

Please sign in to comment.