Skip to content

Commit

Permalink
Merge pull request #733 from jskeet/upload-error
Browse files Browse the repository at this point in the history
Propagate errors from the initial upload request more appropriately.
  • Loading branch information
jskeet committed Apr 22, 2016
2 parents 418e02b + 72210b4 commit 20c650b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
45 changes: 45 additions & 0 deletions Src/Support/GoogleApis.Tests/Apis/Upload/ResumableUploadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

namespace Google.Apis.Tests.Apis.Upload
{
// TODO: Consider rewriting to use an actual HttpListener like MediaDownloaderTest.
[TestFixture]
class ResumableUploadTest
{
Expand Down Expand Up @@ -212,6 +213,31 @@ private class SingleChunkMessageHandler : BaseMockMessageHandler
}
}

private class FailedInitializationMessageHandler : BaseMockMessageHandler
{
private readonly HttpStatusCode status;
private readonly byte[] content;
private readonly string contentType;

public FailedInitializationMessageHandler(HttpStatusCode status, byte[] content, string contentType)
{
this.status = status;
this.content = content;
this.contentType = contentType;
}

protected override Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
{
var response = new HttpResponseMessage();
Assert.That(request.RequestUri.Query, Is.EqualTo("?uploadType=resumable"));
Assert.That(request.Headers.GetValues("X-Upload-Content-Type").First(),
Is.EqualTo("text/plain"));
response.StatusCode = status;
response.Content = new ByteArrayContent(content);
return Task.FromResult(response);
}
}

/// <summary>
/// A handler which demonstrate a server which reads partial data (e.g. on the first upload request the client
/// sends X bytes, but the server actually read only Y of them)
Expand Down Expand Up @@ -1144,5 +1170,24 @@ public void TestChunkSize()
upload.ChunkSize = MockResumableUpload.MinimumChunkSize * 2;
}
}

[Test]
public void InitializationRequestFails()
{
string errorText = "Missing foobar";
var handler = new FailedInitializationMessageHandler(
HttpStatusCode.BadRequest, Encoding.UTF8.GetBytes(errorText), "text/plain; charset=utf-8");
using (var service = new MockClientService(new BaseClientService.Initializer()
{
HttpClientFactory = new MockHttpClientFactory(handler)
}))
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestData));
var upload = new MockResumableUpload(service, stream, "text/plain", 100);
var progress = upload.Upload();
var exception = (GoogleApiException)progress.Exception;
Assert.AreEqual(errorText, exception.Message);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ public async Task<IUploadProgress> UploadAsync(CancellationToken cancellationTok
{
Logger.Error(ex, "MediaUpload - Exception occurred while initializing the upload");
UpdateProgress(new ResumableUploadProgress(ex, BytesServerReceived));
return Progress;
}

return await UploadCoreAsync(cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -491,7 +492,12 @@ private async Task<Uri> InitializeUpload(CancellationToken cancellationToken)
{
HttpRequestMessage request = CreateInitializeRequest();
var response = await Service.HttpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);
return response.EnsureSuccessStatusCode().Headers.Location;

if (!response.IsSuccessStatusCode)
{
throw await MediaApiErrorHandling.ExceptionForResponseAsync(Service, response).ConfigureAwait(false);
}
return response.Headers.Location;
}

/// <summary>
Expand Down

0 comments on commit 20c650b

Please sign in to comment.