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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 /// <summary> Base mock handler which contains the upload Uri. </summary
> | 68 /// <summary> Base mock handler which contains the upload Uri. </summary
> |
69 private abstract class BaseMockMessageHandler : CountableMessageHandler | 69 private abstract class BaseMockMessageHandler : CountableMessageHandler |
70 { | 70 { |
71 /// <summary> The upload Uri for uploading the media. </summary> | 71 /// <summary> The upload Uri for uploading the media. </summary> |
72 protected static Uri uploadUri = new Uri("http://upload.com"); | 72 protected static Uri uploadUri = new Uri("http://upload.com"); |
73 } | 73 } |
74 | 74 |
75 /// <summary> A handler which handles uploading an empty file. </summary
> | 75 /// <summary> A handler which handles uploading an empty file. </summary
> |
76 private class EmptyFileMessageHandler : BaseMockMessageHandler | 76 private class EmptyFileMessageHandler : BaseMockMessageHandler |
77 { | 77 { |
78 protected override async Task<HttpResponseMessage> SendAsyncCore(Htt
pRequestMessage request, | 78 protected override Task<HttpResponseMessage> SendAsyncCore(HttpReque
stMessage request, |
79 CancellationToken cancellationToken) | 79 CancellationToken cancellationToken) |
80 { | 80 { |
81 var response = new HttpResponseMessage(); | 81 var response = new HttpResponseMessage(); |
82 switch (Calls) | 82 switch (Calls) |
83 { | 83 { |
84 case 1: | 84 case 1: |
85 // First call is initialization | 85 // First call is initialization |
86 Assert.That(request.RequestUri.Query, Is.EqualTo("?uploa
dType=resumable")); | 86 Assert.That(request.RequestUri.Query, Is.EqualTo("?uploa
dType=resumable")); |
87 Assert.That(request.Headers.GetValues("X-Upload-Content-
Type").First(), | 87 Assert.That(request.Headers.GetValues("X-Upload-Content-
Type").First(), |
88 Is.EqualTo("text/plain")); | 88 Is.EqualTo("text/plain")); |
89 Assert.That(request.Headers.GetValues("X-Upload-Content-
Length").First(), Is.EqualTo("0")); | 89 Assert.That(request.Headers.GetValues("X-Upload-Content-
Length").First(), Is.EqualTo("0")); |
90 | 90 |
91 response.Headers.Location = uploadUri; | 91 response.Headers.Location = uploadUri; |
92 break; | 92 break; |
93 case 2: | 93 case 2: |
94 // Receiving an empty stream | 94 // Receiving an empty stream |
95 Assert.That(request.RequestUri, Is.EqualTo(uploadUri)); | 95 Assert.That(request.RequestUri, Is.EqualTo(uploadUri)); |
96 var range = String.Format("bytes */0"); | 96 var range = String.Format("bytes */0"); |
97 Assert.That(request.Content.Headers.GetValues("Content-R
ange").First(), Is.EqualTo(range)); | 97 Assert.That(request.Content.Headers.GetValues("Content-R
ange").First(), Is.EqualTo(range)); |
98 Assert.That(request.Content.Headers.ContentLength, Is.Eq
ualTo(0)); | 98 Assert.That(request.Content.Headers.ContentLength, Is.Eq
ualTo(0)); |
99 break; | 99 break; |
100 } | 100 } |
101 | 101 |
102 return response; | 102 TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompleti
onSource<HttpResponseMessage>(); |
| 103 tcs.SetResult(response); |
| 104 return tcs.Task; |
103 } | 105 } |
104 } | 106 } |
105 | 107 |
106 /// <summary> A handler which handles a request object on the initializa
tion request. </summary> | 108 /// <summary> A handler which handles a request object on the initializa
tion request. </summary> |
107 private class RequestResponseMessageHandler<TRequest> : BaseMockMessageH
andler | 109 private class RequestResponseMessageHandler<TRequest> : BaseMockMessageH
andler |
108 { | 110 { |
109 /// <summary>· | 111 /// <summary>· |
110 /// Gets or sets the expected request object. Server checks that the
initialization request contains that· | 112 /// Gets or sets the expected request object. Server checks that the
initialization request contains that· |
111 /// object in the request. | 113 /// object in the request. |
112 /// </summary> | 114 /// </summary> |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 { | 167 { |
166 /// <summary> Gets or sets the expected stream length. </summary> | 168 /// <summary> Gets or sets the expected stream length. </summary> |
167 public long StreamLength { get; set; } | 169 public long StreamLength { get; set; } |
168 | 170 |
169 /// <summary> Gets or sets the query parameters which should be part
of the initialize request. </summary> | 171 /// <summary> Gets or sets the query parameters which should be part
of the initialize request. </summary> |
170 public string QueryParameters { get; set; } | 172 public string QueryParameters { get; set; } |
171 | 173 |
172 /// <summary> Gets or sets the path parameters which should be part
of the initialize request. </summary> | 174 /// <summary> Gets or sets the path parameters which should be part
of the initialize request. </summary> |
173 public string PathParameters { get; set; } | 175 public string PathParameters { get; set; } |
174 | 176 |
175 protected override async Task<HttpResponseMessage> SendAsyncCore(Htt
pRequestMessage request, | 177 protected override Task<HttpResponseMessage> SendAsyncCore(HttpReque
stMessage request, |
176 CancellationToken cancellationToken) | 178 CancellationToken cancellationToken) |
177 { | 179 { |
178 var response = new HttpResponseMessage(); | 180 var response = new HttpResponseMessage(); |
179 var range = string.Empty; | 181 var range = string.Empty; |
180 switch (Calls) | 182 switch (Calls) |
181 { | 183 { |
182 case 1: | 184 case 1: |
183 if (PathParameters == null) | 185 if (PathParameters == null) |
184 { | 186 { |
185 Assert.That(request.RequestUri.AbsolutePath, Is.Equa
lTo("/")); | 187 Assert.That(request.RequestUri.AbsolutePath, Is.Equa
lTo("/")); |
(...skipping 13 matching lines...) Expand all Loading... |
199 break; | 201 break; |
200 | 202 |
201 case 2: | 203 case 2: |
202 Assert.That(request.RequestUri, Is.EqualTo(uploadUri)); | 204 Assert.That(request.RequestUri, Is.EqualTo(uploadUri)); |
203 range = String.Format("bytes 0-{0}/{1}", StreamLength -
1, StreamLength); | 205 range = String.Format("bytes 0-{0}/{1}", StreamLength -
1, StreamLength); |
204 Assert.That(request.Content.Headers.GetValues("Content-R
ange").First(), Is.EqualTo(range)); | 206 Assert.That(request.Content.Headers.GetValues("Content-R
ange").First(), Is.EqualTo(range)); |
205 Assert.That(request.Content.Headers.ContentLength, Is.Eq
ualTo(StreamLength)); | 207 Assert.That(request.Content.Headers.ContentLength, Is.Eq
ualTo(StreamLength)); |
206 break; | 208 break; |
207 } | 209 } |
208 | 210 |
209 return response; | 211 TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompleti
onSource<HttpResponseMessage>(); |
| 212 tcs.SetResult(response); |
| 213 return tcs.Task; |
210 } | 214 } |
211 } | 215 } |
212 | 216 |
213 /// <summary>· | 217 /// <summary>· |
214 /// A handler which demonstrate a server which reads partial data (e.g.
on the first upload request the client | 218 /// A handler which demonstrate a server which reads partial data (e.g.
on the first upload request the client |
215 /// sends X bytes, but the server actually read only Y of them) | 219 /// sends X bytes, but the server actually read only Y of them) |
216 /// </summary> | 220 /// </summary> |
217 private class ReadPartialMessageHandler : BaseMockMessageHandler | 221 private class ReadPartialMessageHandler : BaseMockMessageHandler |
218 { | 222 { |
219 /// <summary> Received stream which contains the data that the serve
r reads. </summary> | 223 /// <summary> Received stream which contains the data that the serve
r reads. </summary> |
(...skipping 812 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1032 var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestD
ata)); | 1036 var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestD
ata)); |
1033 var upload = new MockResumableUploadWithResponse<TestRequest, Te
stResponse> | 1037 var upload = new MockResumableUploadWithResponse<TestRequest, Te
stResponse> |
1034 (service, stream, "text/plain"); | 1038 (service, stream, "text/plain"); |
1035 | 1039 |
1036 // negative chunk size | 1040 // negative chunk size |
1037 try | 1041 try |
1038 { | 1042 { |
1039 upload.ChunkSize = -1; | 1043 upload.ChunkSize = -1; |
1040 Assert.Fail(); | 1044 Assert.Fail(); |
1041 } | 1045 } |
1042 catch (ArgumentOutOfRangeException ex) | 1046 catch (ArgumentOutOfRangeException) |
1043 { | 1047 { |
1044 // expected | 1048 // expected |
1045 } | 1049 } |
1046 | 1050 |
1047 // less than the minimum | 1051 // less than the minimum |
1048 try | 1052 try |
1049 { | 1053 { |
1050 upload.ChunkSize = MockResumableUpload.MinimumChunkSize - 1; | 1054 upload.ChunkSize = MockResumableUpload.MinimumChunkSize - 1; |
1051 Assert.Fail(); | 1055 Assert.Fail(); |
1052 } | 1056 } |
1053 catch (ArgumentOutOfRangeException ex) | 1057 catch (ArgumentOutOfRangeException) |
1054 { | 1058 { |
1055 // expected | 1059 // expected |
1056 } | 1060 } |
1057 | 1061 |
1058 // valid chunk size | 1062 // valid chunk size |
1059 upload.ChunkSize = MockResumableUpload.MinimumChunkSize; | 1063 upload.ChunkSize = MockResumableUpload.MinimumChunkSize; |
1060 upload.ChunkSize = MockResumableUpload.MinimumChunkSize * 2; | 1064 upload.ChunkSize = MockResumableUpload.MinimumChunkSize * 2; |
1061 } | 1065 } |
1062 } | 1066 } |
1063 } | 1067 } |
1064 } | 1068 } |
OLD | NEW |