public void TestChunkSize() { using (var service = new MockClientService(new BaseClientService.Initializer())) { var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestData)); var upload = new MockResumableUploadWithResponse <TestRequest, TestResponse> (service, stream, "text/plain"); // Negative chunk size. try { upload.ChunkSize = -1; Assert.Fail(); } catch (ArgumentOutOfRangeException) { // Expected. } // Less than the minimum. try { upload.ChunkSize = MockResumableUpload.MinimumChunkSize - 1; Assert.Fail(); } catch (ArgumentOutOfRangeException) { // Expected. } // Valid chunk size. upload.ChunkSize = MockResumableUpload.MinimumChunkSize; upload.ChunkSize = MockResumableUpload.MinimumChunkSize * 2; } }
public void TestUploadWithRequestAndResponseBody() { var body = new TestRequest() { Name = "test object", Description = "the description", }; var handler = new RequestResponseMessageHandler <TestRequest>() { ExpectedRequest = body, ExpectedResponse = new TestResponse { Name = "foo", Id = 100, Description = "bar", }, Serializer = new NewtonsoftJsonSerializer() }; using (var service = new MockClientService(new BaseClientService.Initializer() { HttpClientFactory = new MockHttpClientFactory(handler), GZipEnabled = false // TODO(peleyal): test with GZipEnabled as well })) { var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestData)); var upload = new MockResumableUploadWithResponse <TestRequest, TestResponse> (service, stream, "text/plain") { Body = body }; TestResponse response = null; int reponseReceivedCount = 0; upload.ResponseReceived += (r) => { response = r; reponseReceivedCount++; }; upload.Upload(); Assert.That(upload.ResponseBody, Is.EqualTo(handler.ExpectedResponse)); Assert.That(reponseReceivedCount, Is.EqualTo(1)); Assert.That(handler.Calls, Is.EqualTo(2)); } }
public void TestUploadWithRequestAndResponseBody() { using (var server = new MockResumableUploadServer()) { var stream = new MemoryStream(Encoding.UTF8.GetBytes(UploadTestData)); var serializer = new Google.Apis.Json.NewtonsoftJsonSerializer(); var body = new TestRequest() { Name = "test object", Description = "the description", }; server.ExpectRequest(context => { // Verify the initialization request Assert.That(context.Request.QueryString["uploadType"], Is.EqualTo("resumable")); Assert.That(context.Request.QueryString["alt"], Is.EqualTo("json")); Assert.That(context.Request.Headers["X-Upload-Content-Type"], Is.EqualTo("text/plain")); Assert.That(context.Request.Headers["X-Upload-Content-Length"], Is.EqualTo(stream.Length.ToString())); // Verify the request body Assert.That(context.Request.HasEntityBody); TestRequest req = serializer.Deserialize <TestRequest>(context.Request.InputStream); Assert.That(req, Is.Not.Null); Assert.That(req.Name, Is.EqualTo(body.Name)); Assert.That(req.Description, Is.EqualTo(body.Description)); // Give the location for the next chunk context.Response.Headers.Add(HttpResponseHeader.Location, server.UploadUri.ToString()); }); server.ExpectRequest(context => { // Verify the chunk-request Assert.That(context.Request.Url.AbsoluteUri, Is.EqualTo(server.UploadUri.ToString())); var range = String.Format("bytes 0-{0}/{1}", UploadTestData.Length - 1, UploadTestData.Length); Assert.That(context.Request.Headers["Content-Range"], Is.EqualTo(range)); // Send the response-body context.Response.ContentType = "application/json"; var testResponse = new TestResponse() { id = 123, Name = "foo", Description = "bar" }; serializer.Serialize(testResponse, context.Response.OutputStream); }); TestResponse response = null; int eventCount = 0; var upload = new MockResumableUploadWithResponse <TestRequest, TestResponse>(server, stream, "text/plain") { Body = body, }; upload.ResponseReceived += (r) => { response = r; eventCount++; }; upload.Upload(); Assert.That(upload.ResponseBody, Is.Not.Null); Assert.That(upload.ResponseBody.id, Is.EqualTo(123)); Assert.That(upload.ResponseBody.Name, Is.EqualTo("foo")); Assert.That(upload.ResponseBody.Description, Is.EqualTo("bar")); Assert.That(response, Is.SameAs(upload.ResponseBody)); Assert.That(eventCount, Is.EqualTo(1)); } }