Skip to content

Commit

Permalink
Allow a null content type for resumable uploads.
Browse files Browse the repository at this point in the history
This fixes issue #743.
  • Loading branch information
jskeet committed May 11, 2016
1 parent fceea69 commit 956f725
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
34 changes: 32 additions & 2 deletions Src/Support/GoogleApis.Tests/Apis/Upload/ResumableUploadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ private class SingleChunkMessageHandler : BaseMockMessageHandler
/// <summary>Gets or sets the path parameters which should be part of the initialize request.</summary>
public string PathParameters { get; set; }

public string ExpectedContentType { get; set; } = "text/plain";

protected override Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request,
CancellationToken cancellationToken)
{
Expand All @@ -191,8 +193,12 @@ private class SingleChunkMessageHandler : BaseMockMessageHandler
}
Assert.That(request.RequestUri.Query, Is.EqualTo("?uploadType=resumable" + QueryParameters));

Assert.That(request.Headers.GetValues("X-Upload-Content-Type").First(),
Is.EqualTo("text/plain"));
// HttpRequestMessage doesn't make it terrible easy to get a header value speculatively...
string actualContentType = request.Headers
.Where(h => h.Key == "X-Upload-Content-Type")
.Select(h => h.Value.FirstOrDefault())
.FirstOrDefault();
Assert.That(actualContentType, Is.EqualTo(ExpectedContentType));
Assert.That(request.Headers.GetValues("X-Upload-Content-Length").First(),
Is.EqualTo(StreamLength.ToString()));

Expand Down Expand Up @@ -609,6 +615,30 @@ public void TestUploadSingleChunk()
Assert.That(handler.Calls, Is.EqualTo(2));
}

[Test]
public void TestUploadNullContentType()
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestData));
var handler = new SingleChunkMessageHandler()
{
StreamLength = stream.Length,
ExpectedContentType = null
};
using (var service = new MockClientService(new BaseClientService.Initializer()
{
HttpClientFactory = new MockHttpClientFactory(handler)
}))
{

int chunkSize = UploadTestData.Length + 10;
var upload = new MockResumableUpload(service, "", "POST", stream, null, chunkSize);
// Chunk size is bigger than the data we are sending.
upload.Upload();
}

Assert.That(handler.Calls, Is.EqualTo(2));
}

/// <summary>Tests uploading a single chunk.</summary>
[Test]
public void TestUploadSingleChunk_ExactChunkSize()
Expand Down
18 changes: 11 additions & 7 deletions Src/Support/GoogleApis/Apis/[Media]/Upload/ResumableUpload.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public class ResumableUpload<TRequest>
/// <param name="path">The path for this media upload method.</param>
/// <param name="httpMethod">The HTTP method to start this upload.</param>
/// <param name="contentStream">The stream containing the content to upload.</param>
/// <param name="contentType">Content type of the content to be uploaded.</param>
/// <param name="contentType">Content type of the content to be uploaded. Some services
/// may allow this to be null; others require a content type to be specified and will
/// fail when the upload is started if the value is null.</param>
/// <remarks>
/// Caller is responsible for maintaining the <paramref name="contentStream"/> open until the upload is
/// completed.
Expand All @@ -106,11 +108,10 @@ public class ResumableUpload<TRequest>
protected ResumableUpload(IClientService service, string path, string httpMethod, Stream contentStream,
string contentType)
{
service.ThrowIfNull("service");
path.ThrowIfNull("path");
httpMethod.ThrowIfNullOrEmpty("httpMethod");
contentStream.ThrowIfNull("stream");
contentType.ThrowIfNull("contentType");
service.ThrowIfNull(nameof(service));
path.ThrowIfNull(nameof(path));
httpMethod.ThrowIfNullOrEmpty(nameof(httpMethod));
contentStream.ThrowIfNull(nameof(contentStream));

this.Service = service;
this.Path = path;
Expand Down Expand Up @@ -731,7 +732,10 @@ private HttpRequestMessage CreateInitializeRequest()
SetAllPropertyValues(builder);

HttpRequestMessage request = builder.CreateRequest();
request.Headers.Add(PayloadContentTypeHeader, ContentType);
if (ContentType != null)
{
request.Headers.Add(PayloadContentTypeHeader, ContentType);
}

// if the length is unknown at the time of this request, omit "X-Upload-Content-Length" header
if (StreamLength != UnknownSize)
Expand Down

0 comments on commit 956f725

Please sign in to comment.