OLD | NEW |
1 /* | 1 /* |
2 Copyright 2012 Google Inc | 2 Copyright 2012 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 /// Gets or sets the expected request object. Server checks that the
initialization request contains that· | 110 /// Gets or sets the expected request object. Server checks that the
initialization request contains that· |
111 /// object in the request. | 111 /// object in the request. |
112 /// </summary> | 112 /// </summary> |
113 public TRequest ExpectedRequest { get; set; } | 113 public TRequest ExpectedRequest { get; set; } |
114 | 114 |
115 /// <summary> | 115 /// <summary> |
116 /// Gets or sets the expected response object which server returns a
s a response for the upload request. | 116 /// Gets or sets the expected response object which server returns a
s a response for the upload request. |
117 /// </summary> | 117 /// </summary> |
118 public object ExpectedResponse { get; set; } | 118 public object ExpectedResponse { get; set; } |
119 | 119 |
120 /// <summary>Gets or sets the Serializer which is used to serialize
and deserialize objects.</summary> | 120 /// <summary>Gets or sets the serializer which is used to serialize
and deserialize objects.</summary> |
121 public ISerializer Serializer { get; set; } | 121 public ISerializer Serializer { get; set; } |
122 | 122 |
123 protected override async Task<HttpResponseMessage> SendAsyncCore(Htt
pRequestMessage request, | 123 protected override async Task<HttpResponseMessage> SendAsyncCore(Htt
pRequestMessage request, |
124 CancellationToken cancellationToken) | 124 CancellationToken cancellationToken) |
125 { | 125 { |
126 var response = new HttpResponseMessage(); | 126 var response = new HttpResponseMessage(); |
127 switch (Calls) | 127 switch (Calls) |
128 { | 128 { |
129 case 1: | 129 case 1: |
130 { | 130 { |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 private class MultipleChunksMessageHandler : BaseMockMessageHandler | 303 private class MultipleChunksMessageHandler : BaseMockMessageHandler |
304 { | 304 { |
305 public MemoryStream ReceivedData = new MemoryStream(); | 305 public MemoryStream ReceivedData = new MemoryStream(); |
306 | 306 |
307 /// <summary>The cancellation token we are going to use to cancel a
request.</summary> | 307 /// <summary>The cancellation token we are going to use to cancel a
request.</summary> |
308 public CancellationTokenSource CancellationTokenSource { get; set; } | 308 public CancellationTokenSource CancellationTokenSource { get; set; } |
309 | 309 |
310 /// <summary>The request index we are going to cancel.</summary> | 310 /// <summary>The request index we are going to cancel.</summary> |
311 public int CancelRequestNum { get; set; } | 311 public int CancelRequestNum { get; set; } |
312 | 312 |
313 // on the 4th request - server returns error (if supportedError isn'
t none) | 313 // On the 4th request - server returns error (if supportedError isn'
t none) |
314 // on the 5th request - server returns 308 with "Range" header is "b
ytes 0-299" (depends on supportedError) | 314 // On the 5th request - server returns 308 with "Range" header is "b
ytes 0-299" (depends on supportedError) |
315 internal const int ErrorOnCall = 4; | 315 internal const int ErrorOnCall = 4; |
316 | 316 |
| 317 // When we resuming an upload, there should be 3 more calls after th
e failures. |
| 318 // Uploading 3 more chunks: 200-299, 300-399, 400-453. |
| 319 internal const int CallAfterResume = 3; |
| 320 |
317 /// <summary> | 321 /// <summary> |
318 /// Gets or sets the number of bytes the server reads when error occ
urred. The default value is <c>0</c>, | 322 /// Gets or sets the number of bytes the server reads when error occ
urred. The default value is <c>0</c>, |
319 /// meaning that on server error it won't read any bytes from the st
ream. | 323 /// meaning that on server error it won't read any bytes from the st
ream. |
320 /// </summary> | 324 /// </summary> |
321 public int ReadBytesOnError { get; set; } | 325 public int ReadBytesOnError { get; set; } |
322 | 326 |
323 private ServerError supportedError; | 327 private ServerError supportedError; |
324 | 328 |
325 private bool knownSize; | 329 private bool knownSize; |
326 private int len; | 330 private int len; |
327 private int chunkSize; | 331 private int chunkSize; |
328 private bool alwaysFailFromFirstError; | 332 private bool alwaysFailFromFirstError; |
329 | 333 |
330 private int bytesRecieved = 0; | 334 private int bytesRecieved = 0; |
331 | 335 |
332 private string uploadSize; | 336 private string uploadSize; |
333 | 337 |
| 338 /// <summary>Gets or sets the call number after resuming.</summary> |
| 339 public int ResumeFromCall { get; set; } |
| 340 |
| 341 /// <summary>Get or sets indication if the first call after resuming
should fail or not.</summary> |
| 342 public bool ErrorOnResume { get; set; } |
| 343 |
334 public MultipleChunksMessageHandler(bool knownSize, ServerError supp
ortedError, int len, int chunkSize, | 344 public MultipleChunksMessageHandler(bool knownSize, ServerError supp
ortedError, int len, int chunkSize, |
335 bool alwaysFailFromFirstError = false) | 345 bool alwaysFailFromFirstError = false) |
336 { | 346 { |
337 this.knownSize = knownSize; | 347 this.knownSize = knownSize; |
338 this.supportedError = supportedError; | 348 this.supportedError = supportedError; |
339 this.len = len; | 349 this.len = len; |
340 this.chunkSize = chunkSize; | 350 this.chunkSize = chunkSize; |
341 this.alwaysFailFromFirstError = alwaysFailFromFirstError; | 351 this.alwaysFailFromFirstError = alwaysFailFromFirstError; |
342 uploadSize = knownSize ? UploadTestData.Length.ToString() : "*"; | 352 uploadSize = knownSize ? UploadTestData.Length.ToString() : "*"; |
343 } | 353 } |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 else | 420 else |
411 { | 421 { |
412 throw new Exception("ERROR"); | 422 throw new Exception("ERROR"); |
413 } | 423 } |
414 | 424 |
415 var bytes = await request.Content.ReadAsByteArrayAsync()
; | 425 var bytes = await request.Content.ReadAsByteArrayAsync()
; |
416 var read = Math.Min(ReadBytesOnError, bytes.Length); | 426 var read = Math.Min(ReadBytesOnError, bytes.Length); |
417 ReceivedData.Write(bytes, 0, read); | 427 ReceivedData.Write(bytes, 0, read); |
418 bytesRecieved += read; | 428 bytesRecieved += read; |
419 } | 429 } |
420 else if (Calls >= ErrorOnCall && alwaysFailFromFirstError) | 430 else if ((Calls >= ErrorOnCall && alwaysFailFromFirstError &
& ResumeFromCall == 0) || |
| 431 (Calls == ResumeFromCall && ErrorOnResume)) |
421 { | 432 { |
422 if (supportedError == ServerError.Exception) | 433 if (supportedError == ServerError.Exception) |
423 { | 434 { |
424 throw new Exception("ERROR"); | 435 throw new Exception("ERROR"); |
425 } | 436 } |
| 437 |
426 Assert.That(request.Content.Headers.GetValues("Content-R
ange").First(), Is.EqualTo( | 438 Assert.That(request.Content.Headers.GetValues("Content-R
ange").First(), Is.EqualTo( |
427 string.Format(@"bytes */{0}", uploadSize))); | 439 string.Format(@"bytes */{0}", uploadSize))); |
428 response.StatusCode = HttpStatusCode.ServiceUnavailable; | 440 response.StatusCode = HttpStatusCode.ServiceUnavailable; |
429 } | 441 } |
430 else if (Calls == ErrorOnCall + 1 && supportedError != Serve
rError.None) | 442 else if ((Calls == ErrorOnCall + 1 && supportedError != Serv
erError.None) || |
| 443 (Calls == ResumeFromCall && !ErrorOnResume) || |
| 444 (Calls == ResumeFromCall + 1 && ErrorOnResume)) |
431 { | 445 { |
432 Assert.That(request.Content.Headers.GetValues("Content-R
ange").First(), Is.EqualTo( | 446 Assert.That(request.Content.Headers.GetValues("Content-R
ange").First(), Is.EqualTo( |
433 string.Format(@"bytes */{0}", uploadSize))); | 447 string.Format(@"bytes */{0}", uploadSize))); |
434 if (bytesRecieved != len) | 448 if (bytesRecieved != len) |
435 { | 449 { |
436 response.StatusCode = (HttpStatusCode)308; | 450 response.StatusCode = (HttpStatusCode)308; |
437 } | 451 } |
438 response.Headers.Add("Range", "bytes 0-" + (bytesRecieve
d - 1)); | 452 response.Headers.Add("Range", "bytes 0-" + (bytesRecieve
d - 1)); |
439 } | 453 } |
440 else | 454 else |
(...skipping 15 matching lines...) Expand all Loading... |
456 } | 470 } |
457 } | 471 } |
458 | 472 |
459 #endregion | 473 #endregion |
460 | 474 |
461 #region ResumableUpload instances | 475 #region ResumableUpload instances |
462 | 476 |
463 private class MockResumableUpload : ResumableUpload<object> | 477 private class MockResumableUpload : ResumableUpload<object> |
464 { | 478 { |
465 public MockResumableUpload(IClientService service, Stream stream, st
ring contentType) | 479 public MockResumableUpload(IClientService service, Stream stream, st
ring contentType) |
466 : this(service, "path", "PUT", stream, contentType) | 480 : this(service, "path", "PUT", stream, contentType) { } |
467 { | |
468 } | |
469 | 481 |
470 public MockResumableUpload(IClientService service, string path, stri
ng method, Stream stream, | 482 public MockResumableUpload(IClientService service, string path, stri
ng method, Stream stream, |
471 string contentType) | 483 string contentType) |
472 : base(service, path, method, stream, contentType) | 484 : base(service, path, method, stream, contentType) { } |
473 { | |
474 } | |
475 } | 485 } |
476 | 486 |
477 /// <summary> | 487 /// <summary> |
478 /// A resumable upload class which gets a specific request object and re
turns a specific response object. | 488 /// A resumable upload class which gets a specific request object and re
turns a specific response object. |
479 /// </summary> | 489 /// </summary> |
480 /// <typeparam name="TRequest"></typeparam> | 490 /// <typeparam name="TRequest"></typeparam> |
481 /// <typeparam name="TResponse"></typeparam> | 491 /// <typeparam name="TResponse"></typeparam> |
482 private class MockResumableUploadWithResponse<TRequest, TResponse> : Res
umableUpload<TRequest, TResponse> | 492 private class MockResumableUploadWithResponse<TRequest, TResponse> : Res
umableUpload<TRequest, TResponse> |
483 { | 493 { |
484 public MockResumableUploadWithResponse(IClientService service, | 494 public MockResumableUploadWithResponse(IClientService service, |
485 Stream stream, string contentType) | 495 Stream stream, string contentType) |
486 : base(service, "path", "POST", stream, contentType) | 496 : base(service, "path", "POST", stream, contentType) { } |
487 { | |
488 } | |
489 } | 497 } |
490 | 498 |
491 /// <summary>A resumable upload class which contains query and path para
meters.</summary> | 499 /// <summary>A resumable upload class which contains query and path para
meters.</summary> |
492 private class MockResumableWithParameters : ResumableUpload<object> | 500 private class MockResumableWithParameters : ResumableUpload<object> |
493 { | 501 { |
494 public MockResumableWithParameters(IClientService service, string pa
th, string method, | 502 public MockResumableWithParameters(IClientService service, string pa
th, string method, |
495 Stream stream, string contentType) | 503 Stream stream, string contentType) |
496 : base(service, path, method, stream, contentType) | 504 : base(service, path, method, stream, contentType) |
497 { | 505 { |
498 } | 506 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
559 } | 567 } |
560 | 568 |
561 #endregion | 569 #endregion |
562 | 570 |
563 /// <summary>Tests uploading a single chunk.</summary> | 571 /// <summary>Tests uploading a single chunk.</summary> |
564 [Test] | 572 [Test] |
565 public void TestUploadSingleChunk() | 573 public void TestUploadSingleChunk() |
566 { | 574 { |
567 var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestData)
); | 575 var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestData)
); |
568 var handler = new SingleChunkMessageHandler() | 576 var handler = new SingleChunkMessageHandler() |
569 { | 577 { |
570 StreamLength = stream.Length | 578 StreamLength = stream.Length |
571 }; | 579 }; |
572 using (var service = new MockClientService(new BaseClientService.Ini
tializer() | 580 using (var service = new MockClientService(new BaseClientService.Ini
tializer() |
573 { | 581 { |
574 HttpClientFactory = new MockHttpClientFactory(handler) | 582 HttpClientFactory = new MockHttpClientFactory(handler) |
575 })) | 583 })) |
576 { | 584 { |
577 | 585 |
578 var upload = new MockResumableUpload(service, "", "POST", stream
, "text/plain"); | 586 var upload = new MockResumableUpload(service, "", "POST", stream
, "text/plain"); |
579 // Chunk size is bigger than the data we are sending. | 587 // Chunk size is bigger than the data we are sending. |
580 upload.chunkSize = UploadTestData.Length + 10; | 588 upload.chunkSize = UploadTestData.Length + 10; |
581 upload.Upload(); | 589 upload.Upload(); |
582 } | 590 } |
583 | 591 |
584 Assert.That(handler.Calls, Is.EqualTo(2)); | 592 Assert.That(handler.Calls, Is.EqualTo(2)); |
585 } | 593 } |
586 | 594 |
587 /// <summary>Tests uploading a single chunk.</summary> | 595 /// <summary>Tests uploading a single chunk.</summary> |
588 [Test] | 596 [Test] |
589 public void TestUploadSingleChunk_ExactChunkSize() | 597 public void TestUploadSingleChunk_ExactChunkSize() |
590 { | 598 { |
591 var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestData)
); | 599 var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestData)
); |
592 var handler = new SingleChunkMessageHandler() | 600 var handler = new SingleChunkMessageHandler() |
593 { | 601 { |
594 StreamLength = stream.Length | 602 StreamLength = stream.Length |
595 }; | 603 }; |
596 using (var service = new MockClientService(new BaseClientService.Ini
tializer() | 604 using (var service = new MockClientService(new BaseClientService.Ini
tializer() |
597 { | 605 { |
598 HttpClientFactory = new MockHttpClientFactory(handler) | 606 HttpClientFactory = new MockHttpClientFactory(handler) |
599 })) | 607 })) |
600 { | 608 { |
601 var upload = new MockResumableUpload(service, "", "POST", stream
, "text/plain"); | 609 var upload = new MockResumableUpload(service, "", "POST", stream
, "text/plain"); |
602 // Chunk size is the exact size we are sending. | 610 // Chunk size is the exact size we are sending. |
603 upload.chunkSize = UploadTestData.Length; | 611 upload.chunkSize = UploadTestData.Length; |
604 upload.Upload(); | 612 upload.Upload(); |
605 } | 613 } |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
721 /// Tests that upload fails when server returns an error which the clien
t can't handle (not 5xx). | 729 /// Tests that upload fails when server returns an error which the clien
t can't handle (not 5xx). |
722 /// </summary> | 730 /// </summary> |
723 [Test] | 731 [Test] |
724 public void TestChunkUpload_NotFound_KnownSize() | 732 public void TestChunkUpload_NotFound_KnownSize() |
725 { | 733 { |
726 // we expect 4 calls: 1 initial request + 3 chunks (0-99, 100-199, 2
00-299) [on the 3rd chunk, the client· | 734 // we expect 4 calls: 1 initial request + 3 chunks (0-99, 100-199, 2
00-299) [on the 3rd chunk, the client· |
727 // receives 4xx error. The client can't recover from it, so the uplo
ad stops | 735 // receives 4xx error. The client can't recover from it, so the uplo
ad stops |
728 SubtestTestChunkUpload(true, 4, ServerError.NotFound); | 736 SubtestTestChunkUpload(true, 4, ServerError.NotFound); |
729 } | 737 } |
730 | 738 |
731 /// <summary> | 739 /// <summary>Tests a single upload request.</summary> |
732 /// Tests a single upload request | |
733 /// </summary> | |
734 /// <param name="knownSize">Defines if the stream size is known</param> | 740 /// <param name="knownSize">Defines if the stream size is known</param> |
735 /// <param name="expectedCalls">How many HTTP calls should be made to th
e server</param> | 741 /// <param name="expectedCalls">How many HTTP calls should be made to th
e server</param> |
736 /// <param name="error">Defines the type of error this test tests. The d
efault value is none</param> | 742 /// <param name="error">Defines the type of error this test tests. The d
efault value is none</param> |
737 /// <param name="chunkSize">Defines the size of a chunk</param> | 743 /// <param name="chunkSize">Defines the size of a chunk</param> |
738 /// <param name="readBytesOnError">How many bytes the server reads when
it returns 5xx</param> | 744 /// <param name="readBytesOnError">How many bytes the server reads when
it returns 5xx</param> |
739 private void SubtestTestChunkUpload(bool knownSize, int expectedCalls, S
erverError error = ServerError.None, | 745 private void SubtestTestChunkUpload(bool knownSize, int expectedCalls, S
erverError error = ServerError.None, |
740 int chunkSize = 100, int readBytesOnError = 0) | 746 int chunkSize = 100, int readBytesOnError = 0) |
741 { | 747 { |
742 // If an error isn't supported by the media upload (4xx) - the uploa
d fails. | 748 // If an error isn't supported by the media upload (4xx) - the uploa
d fails. |
743 // Otherwise, we simulate server 503 error or exception, as followin
g: | 749 // Otherwise, we simulate server 503 error or exception, as followin
g: |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
817 upload.chunkSize = chunkSize; | 823 upload.chunkSize = chunkSize; |
818 upload.Upload(); | 824 upload.Upload(); |
819 | 825 |
820 Assert.That(payload, Is.EqualTo(handler.ReceivedData.ToArray()))
; | 826 Assert.That(payload, Is.EqualTo(handler.ReceivedData.ToArray()))
; |
821 // 1 initialization request and 2 uploads requests. | 827 // 1 initialization request and 2 uploads requests. |
822 Assert.That(handler.Calls, Is.EqualTo(3)); | 828 Assert.That(handler.Calls, Is.EqualTo(3)); |
823 } | 829 } |
824 } | 830 } |
825 | 831 |
826 /// <summary>Test helper to test a fail uploading by with the given serv
er error.</summary> | 832 /// <summary>Test helper to test a fail uploading by with the given serv
er error.</summary> |
827 private void SubtestChunkUploadFail(ServerError error) | 833 /// <param name="error">The error kind.</param> |
| 834 /// <param name="resume">Whether we should resume uploading the stream a
fter the failure.</param> |
| 835 /// <param name="errorOnResume">Whether the first call after resuming sh
ould fail.</param> |
| 836 private void SubtestChunkUploadFail(ServerError error, bool resume = fal
se, bool errorOnResume = false) |
828 { | 837 { |
829 int chunkSize = 100; | 838 int chunkSize = 100; |
830 var payload = Encoding.UTF8.GetBytes(UploadTestData); | 839 var payload = Encoding.UTF8.GetBytes(UploadTestData); |
831 | 840 |
832 var handler = new MultipleChunksMessageHandler(true, error, payload.
Length, | 841 var handler = new MultipleChunksMessageHandler(true, error, payload.
Length, chunkSize, true); |
833 chunkSize, true); | |
834 using (var service = new MockClientService(new BaseClientService.Ini
tializer() | 842 using (var service = new MockClientService(new BaseClientService.Ini
tializer() |
835 { | 843 { |
836 HttpClientFactory = new MockHttpClientFactory(handler) | 844 HttpClientFactory = new MockHttpClientFactory(handler) |
837 })) | 845 })) |
838 { | 846 { |
839 var stream = new MemoryStream(payload); | 847 var stream = new MemoryStream(payload); |
840 var upload = new MockResumableUpload(service, stream, "text/plai
n"); | 848 var upload = new MockResumableUpload(service, stream, "text/plai
n"); |
841 upload.chunkSize = chunkSize; | 849 upload.chunkSize = chunkSize; |
842 | 850 |
843 IUploadProgress lastProgressStatus = null; | 851 IUploadProgress lastProgressStatus = null; |
844 upload.ProgressChanged += (p) => | 852 upload.ProgressChanged += (p) => |
845 { | 853 { |
846 lastProgressStatus = p; | 854 lastProgressStatus = p; |
847 }; | 855 }; |
848 upload.Upload(); | 856 upload.Upload(); |
849 | 857 |
| 858 // Upload should fail. |
850 var exepctedCalls = MultipleChunksMessageHandler.ErrorOnCall + | 859 var exepctedCalls = MultipleChunksMessageHandler.ErrorOnCall + |
851 service.HttpClient.MessageHandler.NumTries - 1; | 860 service.HttpClient.MessageHandler.NumTries - 1; |
852 Assert.That(handler.Calls, Is.EqualTo(exepctedCalls)); | 861 Assert.That(handler.Calls, Is.EqualTo(exepctedCalls)); |
853 Assert.NotNull(lastProgressStatus); | 862 Assert.NotNull(lastProgressStatus); |
854 Assert.NotNull(lastProgressStatus.Exception); | 863 Assert.NotNull(lastProgressStatus.Exception); |
855 Assert.That(lastProgressStatus.Status, Is.EqualTo(UploadStatus.F
ailed)); | 864 Assert.That(lastProgressStatus.Status, Is.EqualTo(UploadStatus.F
ailed)); |
| 865 |
| 866 if (resume) |
| 867 { |
| 868 // Hack the handler, so when calling the resume method the u
pload should succeeded. |
| 869 handler.ResumeFromCall = exepctedCalls + 1; |
| 870 handler.ErrorOnResume = errorOnResume; |
| 871 |
| 872 upload.Resume(); |
| 873 |
| 874 // The first request after resuming is to query the server w
here the media upload was interrupted. |
| 875 // If errorOnResume is true, the server's first response wil
l be 503. |
| 876 exepctedCalls += MultipleChunksMessageHandler.CallAfterResum
e + 1 + (errorOnResume ? 1 : 0); |
| 877 Assert.That(handler.Calls, Is.EqualTo(exepctedCalls)); |
| 878 Assert.NotNull(lastProgressStatus); |
| 879 Assert.Null(lastProgressStatus.Exception); |
| 880 Assert.That(lastProgressStatus.Status, Is.EqualTo(UploadStat
us.Completed)); |
| 881 Assert.That(payload, Is.EqualTo(handler.ReceivedData.ToArray
())); |
| 882 } |
856 } | 883 } |
857 } | 884 } |
858 | 885 |
859 /// <summary> | 886 /// <summary> |
860 /// Tests failed uploading media (server returns 5xx responses all the t
ime from some request). | 887 /// Tests failed uploading media (server returns 5xx responses all the t
ime from some request). |
861 /// </summary> | 888 /// </summary> |
862 [Test] | 889 [Test] |
863 public void TestChunkUploadFail_ServerUnavailable() | 890 public void TestChunkUploadFail_ServerUnavailable() |
864 { | 891 { |
865 SubtestChunkUploadFail(ServerError.ServerUnavailable); | 892 SubtestChunkUploadFail(ServerError.ServerUnavailable); |
866 } | 893 } |
867 | 894 |
868 /// <summary> | 895 /// <summary>Tests the resume method.</summary> |
869 /// Tests failed uploading media (exception is thrown all the time from
some request). | 896 [Test] |
870 /// </summary> | 897 public void TestResumeAfterFail() |
| 898 { |
| 899 SubtestChunkUploadFail(ServerError.ServerUnavailable, true); |
| 900 } |
| 901 |
| 902 /// <summary>Tests the resume method. The first call after resuming retu
rns server unavailable.</summary> |
| 903 [Test] |
| 904 public void TestResumeAfterFail_FirstCallAfterResumeIsServerUnavailable(
) |
| 905 { |
| 906 SubtestChunkUploadFail(ServerError.ServerUnavailable, true, true); |
| 907 } |
| 908 |
| 909 /// <summary>Tests failed uploading media (exception is thrown all the t
ime from some request).</summary> |
871 [Test] | 910 [Test] |
872 public void TestChunkUploadFail_Exception() | 911 public void TestChunkUploadFail_Exception() |
873 { | 912 { |
874 SubtestChunkUploadFail(ServerError.Exception); | 913 SubtestChunkUploadFail(ServerError.Exception); |
875 } | 914 } |
876 | 915 |
877 /// <summary>Tests uploading media when canceling a request in the middl
e.</summary> | 916 /// <summary>Tests uploading media when canceling a request in the middl
e.</summary> |
878 [Test] | 917 [Test] |
879 public void TestChunkUploadFail_Cancel() | 918 public void TestChunkUploadFail_Cancel() |
880 { | 919 { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
951 } | 990 } |
952 | 991 |
953 /// <summary>Tests uploading media with query and path parameters on the
initialization request.</summary> | 992 /// <summary>Tests uploading media with query and path parameters on the
initialization request.</summary> |
954 [Test] | 993 [Test] |
955 public void TestUploadWithQueryAndPathParameters() | 994 public void TestUploadWithQueryAndPathParameters() |
956 { | 995 { |
957 var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestData)
); | 996 var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestData)
); |
958 | 997 |
959 const int id = 123; | 998 const int id = 123; |
960 var handler = new SingleChunkMessageHandler() | 999 var handler = new SingleChunkMessageHandler() |
961 { | 1000 { |
962 PathParameters = "testPath/" + id.ToString(), | 1001 PathParameters = "testPath/" + id.ToString(), |
963 QueryParameters = "&queryA=valuea&queryB=VALUEB&time=2002-02
-25T12%3A57%3A32.777Z", | 1002 QueryParameters = "&queryA=valuea&queryB=VALUEB&time=2002-02-25T
12%3A57%3A32.777Z", |
964 StreamLength = stream.Length | 1003 StreamLength = stream.Length |
965 }; | 1004 }; |
966 | 1005 |
967 using (var service = new MockClientService(new BaseClientService.Ini
tializer() | 1006 using (var service = new MockClientService(new BaseClientService.Ini
tializer() |
968 { | 1007 { |
969 HttpClientFactory = new MockHttpClientFactory(handler) | 1008 HttpClientFactory = new MockHttpClientFactory(handler) |
970 })) | 1009 })) |
971 { | 1010 { |
972 var upload = new MockResumableWithParameters(service, "testPath/
{id}", "POST", stream, "text/plain") | 1011 var upload = new MockResumableWithParameters(service, "testPath/
{id}", "POST", stream, "text/plain") |
973 { | 1012 { |
974 Id = id, | 1013 Id = id, |
975 QueryA = "valuea", | 1014 QueryA = "valuea", |
976 QueryB = "VALUEB", | 1015 QueryB = "VALUEB", |
977 MinTime = new DateTime(2002, 2, 25, 12, 57, 32, 777, DateTim
eKind.Utc) | 1016 MinTime = new DateTime(2002, 2, 25, 12, 57, 32, 777, DateTim
eKind.Utc) |
978 }; | 1017 }; |
979 upload.Upload(); | 1018 upload.Upload(); |
980 | 1019 |
981 Assert.That(handler.Calls, Is.EqualTo(2)); | 1020 Assert.That(handler.Calls, Is.EqualTo(2)); |
982 } | 1021 } |
983 } | 1022 } |
984 | 1023 |
985 /// <summary>Tests an upload with JSON request and response body.</summa
ry> | 1024 /// <summary>Tests an upload with JSON request and response body.</summa
ry> |
986 [Test] | 1025 [Test] |
987 public void TestUploadWithRequestAndResponseBody() | 1026 public void TestUploadWithRequestAndResponseBody() |
988 { | 1027 { |
989 var body = new TestRequest() | 1028 var body = new TestRequest() |
990 { | 1029 { |
991 Name = "test object", | 1030 Name = "test object", |
992 Description = "the description", | 1031 Description = "the description", |
993 }; | 1032 }; |
994 | 1033 |
995 var handler = new RequestResponseMessageHandler<TestRequest>() | 1034 var handler = new RequestResponseMessageHandler<TestRequest>() |
996 { | 1035 { |
997 ExpectedRequest = body, | 1036 ExpectedRequest = body, |
998 ExpectedResponse = new TestResponse | 1037 ExpectedResponse = new TestResponse |
999 { | 1038 { |
1000 Name = "foo", | 1039 Name = "foo", |
1001 Id = 100, | 1040 Id = 100, |
1002 Description = "bar", | 1041 Description = "bar", |
1003 }, | 1042 }, |
1004 Serializer = new NewtonsoftJsonSerializer() | 1043 Serializer = new NewtonsoftJsonSerializer() |
1005 }; | 1044 }; |
1006 | 1045 |
1007 using (var service = new MockClientService(new BaseClientService.Ini
tializer() | 1046 using (var service = new MockClientService(new BaseClientService.Ini
tializer() |
1008 { | 1047 { |
1009 HttpClientFactory = new MockHttpClientFactory(handler), | 1048 HttpClientFactory = new MockHttpClientFactory(handler), |
1010 GZipEnabled = false // TODO(peleyal): test with GZipEnabled
as well | 1049 GZipEnabled = false // TODO(peleyal): test with GZipEnabled
as well |
1011 })) | 1050 })) |
1012 { | 1051 { |
1013 var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestD
ata)); | 1052 var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestD
ata)); |
1014 var upload = new MockResumableUploadWithResponse<TestRequest, Te
stResponse> | 1053 var upload = new MockResumableUploadWithResponse<TestRequest, Te
stResponse> |
1015 (service, stream, "text/plain") | 1054 (service, stream, "text/plain") |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1061 // Expected. | 1100 // Expected. |
1062 } | 1101 } |
1063 | 1102 |
1064 // Valid chunk size. | 1103 // Valid chunk size. |
1065 upload.ChunkSize = MockResumableUpload.MinimumChunkSize; | 1104 upload.ChunkSize = MockResumableUpload.MinimumChunkSize; |
1066 upload.ChunkSize = MockResumableUpload.MinimumChunkSize * 2; | 1105 upload.ChunkSize = MockResumableUpload.MinimumChunkSize * 2; |
1067 } | 1106 } |
1068 } | 1107 } |
1069 } | 1108 } |
1070 } | 1109 } |
OLD | NEW |