#402 multipart enhancement
allow specifying different filename when uploading from local path
This commit is contained in:
parent
d90e27c63b
commit
017153bf1c
@ -18,31 +18,36 @@ namespace Flurl.Test.Http
|
||||
var content = new CapturedMultipartContent()
|
||||
.AddString("string", "foo")
|
||||
.AddStringParts(new { part1 = 1, part2 = 2, part3 = (string)null }) // part3 should be excluded
|
||||
.AddFile("file", Path.Combine("path", "to", "image.jpg"), "image/jpeg")
|
||||
.AddFile("file1", Path.Combine("path", "to", "image1.jpg"), "image/jpeg")
|
||||
.AddFile("file2", Path.Combine("path", "to", "image2.jpg"), "image/jpeg", fileName: "new-name.jpg")
|
||||
.AddJson("json", new { foo = "bar" })
|
||||
.AddUrlEncoded("urlEnc", new { fizz = "buzz" });
|
||||
|
||||
Assert.AreEqual(6, content.Parts.Length);
|
||||
Assert.AreEqual(7, content.Parts.Length);
|
||||
|
||||
AssertStringPart<CapturedStringContent>(content.Parts[0], "string", "foo");
|
||||
AssertStringPart<CapturedStringContent>(content.Parts[1], "part1", "1");
|
||||
AssertStringPart<CapturedStringContent>(content.Parts[2], "part2", "2");
|
||||
AssertStringPart<CapturedJsonContent>(content.Parts[4], "json", "{\"foo\":\"bar\"}");
|
||||
AssertStringPart<CapturedUrlEncodedContent>(content.Parts[5], "urlEnc", "fizz=buzz");
|
||||
|
||||
Assert.AreEqual("file", content.Parts[3].Headers.ContentDisposition.Name);
|
||||
Assert.AreEqual("image.jpg", content.Parts[3].Headers.ContentDisposition.FileName);
|
||||
Assert.IsInstanceOf<FileContent>(content.Parts[3]);
|
||||
Assert.AreEqual("image/jpeg", content.Parts[3].Headers.ContentType?.MediaType);
|
||||
AssertFilePart(content.Parts[3], "file1", "image1.jpg", "image/jpeg");
|
||||
AssertFilePart(content.Parts[4], "file2", "new-name.jpg", "image/jpeg");
|
||||
AssertStringPart<CapturedJsonContent>(content.Parts[5], "json", "{\"foo\":\"bar\"}");
|
||||
AssertStringPart<CapturedUrlEncodedContent>(content.Parts[6], "urlEnc", "fizz=buzz");
|
||||
}
|
||||
|
||||
private void AssertStringPart<TContent>(HttpContent part, string name, string content) {
|
||||
private void AssertStringPart<TContent>(HttpContent part, string name, string content) {
|
||||
Assert.IsInstanceOf<TContent>(part);
|
||||
Assert.AreEqual(name, part.Headers.ContentDisposition.Name);
|
||||
Assert.AreEqual(content, (part as CapturedStringContent)?.Content);
|
||||
Assert.IsInstanceOf<TContent>(part);
|
||||
Assert.IsFalse(part.Headers.Contains("Content-Type")); // #392
|
||||
}
|
||||
|
||||
private void AssertFilePart(HttpContent part, string name, string fileName, string contentType) {
|
||||
Assert.IsInstanceOf<FileContent>(part);
|
||||
Assert.AreEqual(name, part.Headers.ContentDisposition.Name);
|
||||
Assert.AreEqual(fileName, part.Headers.ContentDisposition.FileName);
|
||||
Assert.AreEqual(contentType, part.Headers.ContentType?.MediaType);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void must_provide_required_args_to_builder() {
|
||||
var content = new CapturedMultipartContent();
|
||||
|
@ -105,9 +105,10 @@ namespace Flurl.Http.Content
|
||||
/// <param name="path">The local path to the file.</param>
|
||||
/// <param name="mediaType">The media type of the file.</param>
|
||||
/// <param name="bufferSize">The buffer size of the stream upload in bytes. Defaults to 4096.</param>
|
||||
/// <param name="fileName">The filename, added to the Content-Disposition header of the part. Defaults to local file name.</param>
|
||||
/// <returns>This CapturedMultipartContent instance (supports method chaining).</returns>
|
||||
public CapturedMultipartContent AddFile(string name, string path, string mediaType = null, int bufferSize = 4096) {
|
||||
var fileName = FileUtil.GetFileName(path);
|
||||
public CapturedMultipartContent AddFile(string name, string path, string mediaType = null, int bufferSize = 4096, string fileName = null) {
|
||||
fileName = fileName ?? FileUtil.GetFileName(path);
|
||||
var content = new FileContent(path, bufferSize);
|
||||
if (mediaType != null)
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
|
||||
|
Loading…
x
Reference in New Issue
Block a user