1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
//-----------------------------------------------------------------------
// <copyright file="MultipartPostPartTests.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Test.Messaging {
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Net;
using DotNetOpenAuth.Messaging;
using NUnit.Framework;
[TestFixture]
public class MultipartPostPartTests : TestBase {
/// <summary>
/// Verifies that the Length property matches the length actually serialized.
/// </summary>
[TestCase]
public void FormDataSerializeMatchesLength() {
var part = MultipartPostPart.CreateFormPart("a", "b");
VerifyLength(part);
}
/// <summary>
/// Verifies that the length property matches the length actually serialized.
/// </summary>
[TestCase]
public void FileSerializeMatchesLength() {
using (TempFileCollection tfc = new TempFileCollection()) {
string file = tfc.AddExtension(".txt");
File.WriteAllText(file, "sometext");
var part = MultipartPostPart.CreateFormFilePart("someformname", file, "text/plain");
VerifyLength(part);
}
}
/// <summary>
/// Verifies file multiparts identify themselves as files and not merely form-data.
/// </summary>
[TestCase]
public void FilePartAsFile() {
var part = MultipartPostPart.CreateFormFilePart("somename", "somefile", "plain/text", new MemoryStream());
Assert.AreEqual("file", part.ContentDisposition);
}
/// <summary>
/// Verifies MultiPartPost sends the right number of bytes.
/// </summary>
[TestCase]
public void MultiPartPostAscii() {
using (TempFileCollection tfc = new TempFileCollection()) {
string file = tfc.AddExtension("txt");
File.WriteAllText(file, "sometext");
this.VerifyFullPost(new List<MultipartPostPart> {
MultipartPostPart.CreateFormPart("a", "b"),
MultipartPostPart.CreateFormFilePart("SomeFormField", file, "text/plain"),
});
}
}
/// <summary>
/// Verifies MultiPartPost sends the right number of bytes.
/// </summary>
[TestCase]
public void MultiPartPostMultiByteCharacters() {
using (TempFileCollection tfc = new TempFileCollection()) {
string file = tfc.AddExtension("txt");
File.WriteAllText(file, "\x1020\x818");
this.VerifyFullPost(new List<MultipartPostPart> {
MultipartPostPart.CreateFormPart("a", "\x987"),
MultipartPostPart.CreateFormFilePart("SomeFormField", file, "text/plain"),
});
}
}
private static void VerifyLength(MultipartPostPart part) {
Contract.Requires(part != null);
var expectedLength = part.Length;
var ms = new MemoryStream();
var sw = new StreamWriter(ms);
part.Serialize(sw);
sw.Flush();
var actualLength = ms.Length;
Assert.AreEqual(expectedLength, actualLength);
}
private void VerifyFullPost(List<MultipartPostPart> parts) {
var request = (HttpWebRequest)WebRequest.Create("http://localhost");
var handler = new Mocks.TestWebRequestHandler();
bool posted = false;
handler.Callback = req => {
foreach (string header in req.Headers) {
TestUtilities.TestLogger.InfoFormat("{0}: {1}", header, req.Headers[header]);
}
TestUtilities.TestLogger.InfoFormat(handler.RequestEntityAsString);
Assert.AreEqual(req.ContentLength, handler.RequestEntityStream.Length);
posted = true;
return null;
};
request.PostMultipart(handler, parts);
Assert.IsTrue(posted, "HTTP POST never sent.");
}
}
}
|