public async Task WriteMessageAsync_ResponseContainsContentId_IfHasContentIdInRequestChangeSet() { MemoryStream ms = new MemoryStream(); HttpContent content = new StringContent(String.Empty, Encoding.UTF8, "multipart/mixed"); content.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("boundary", Guid.NewGuid().ToString())); IODataResponseMessage odataResponse = ODataMessageWrapperHelper.Create(ms, content.Headers); var batchWriter = new ODataMessageWriter(odataResponse).CreateODataBatchWriter(); HttpResponseMessage response = new HttpResponseMessage { Content = new StringContent("any", Encoding.UTF8, "text/example") }; var request = new HttpRequestMessage(); var contentId = Guid.NewGuid().ToString(); request.SetODataContentId(contentId); response.RequestMessage = request; batchWriter.WriteStartBatch(); batchWriter.WriteStartChangeset(); await ODataBatchResponseItem.WriteMessageAsync(batchWriter, response, CancellationToken.None); batchWriter.WriteEndChangeset(); batchWriter.WriteEndBatch(); ms.Position = 0; string result = new StreamReader(ms).ReadToEnd(); Assert.Contains("any", result); Assert.Contains("text/example", result); Assert.Contains("Content-ID", result); Assert.Contains(contentId, result); }
public async Task WriteMessageAsync_WritesResponseMessage() { MemoryStream ms = new MemoryStream(); HttpContent content = new StringContent(String.Empty, Encoding.UTF8, "multipart/mixed"); content.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("boundary", Guid.NewGuid().ToString())); IODataResponseMessage odataResponse = ODataMessageWrapperHelper.Create(ms, content.Headers); var batchWriter = new ODataMessageWriter(odataResponse).CreateODataBatchWriter(); HttpResponseMessage response = new HttpResponseMessage() { Content = new StringContent("example content", Encoding.UTF8, "text/example") }; response.Headers.Add("customHeader", "bar"); batchWriter.WriteStartBatch(); await ODataBatchResponseItem.WriteMessageAsync(batchWriter, response, CancellationToken.None); batchWriter.WriteEndBatch(); ms.Position = 0; string result = new StreamReader(ms).ReadToEnd(); Assert.Contains("example content", result); Assert.Contains("text/example", result); Assert.Contains("customHeader", result); Assert.Contains("bar", result); }
public void WriteMessageAsync_WritesResponseMessage() { MemoryStream ms = new MemoryStream(); HttpContent content = new StringContent(String.Empty, Encoding.UTF8, "multipart/mixed"); content.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("boundary", Guid.NewGuid().ToString())); IODataResponseMessage odataResponse = new ODataMessageWrapper(ms, content.Headers); var batchWriter = new ODataMessageWriter(odataResponse).CreateODataBatchWriter(); HttpResponseMessage response = new HttpResponseMessage() { Content = new StringContent("example content", Encoding.UTF8, "text/example") }; response.Headers.Add("customHeader", "bar"); batchWriter.WriteStartBatch(); ODataBatchResponseItem.WriteMessageAsync(batchWriter, response).Wait(); batchWriter.WriteEndBatch(); ms.Position = 0; string result = new StreamReader(ms).ReadToEnd(); Assert.Contains("example content", result); Assert.Contains("text/example", result); Assert.Contains("customHeader", result); Assert.Contains("bar", result); }
public void WriteMessageAsync_ResponseContainsContentId_IfHasContentIdInRequestChangeSet() { MemoryStream ms = new MemoryStream(); HttpContent content = new StringContent(String.Empty, Encoding.UTF8, "multipart/mixed"); content.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("boundary", Guid.NewGuid().ToString())); IODataResponseMessage odataResponse = new ODataMessageWrapper(ms, content.Headers); var batchWriter = new ODataMessageWriter(odataResponse).CreateODataBatchWriter(); HttpResponseMessage response = new HttpResponseMessage { Content = new StringContent("any", Encoding.UTF8, "text/example") }; var request = new HttpRequestMessage(); var contentId = Guid.NewGuid().ToString(); request.SetODataContentId(contentId); response.RequestMessage = request; batchWriter.WriteStartBatch(); batchWriter.WriteStartChangeset(); ODataBatchResponseItem.WriteMessageAsync(batchWriter, response, CancellationToken.None).Wait(); batchWriter.WriteEndChangeset(); batchWriter.WriteEndBatch(); ms.Position = 0; string result = new StreamReader(ms).ReadToEnd(); Assert.Contains("any", result); Assert.Contains("text/example", result); Assert.Contains("Content-ID", result); Assert.Contains(contentId, result); }
public void WriteMessage_SynchronouslyWritesResponseMessage_Throws() { HeaderDictionary headers = new HeaderDictionary { { "Content-Type", $"multipart/mixed;charset=utf-8;boundary={Guid.NewGuid()}" }, }; MemoryStream ms = new MemoryStream(); IODataResponseMessage odataResponse = ODataMessageWrapperHelper.Create(ms, headers); HeaderDictionary responseHeaders = new HeaderDictionary { { "customHeader", "bar" } }; HttpResponse response = CreateResponse("example content", responseHeaders, "text/example"); // Act ODataBatchWriter batchWriter = new ODataMessageWriter(odataResponse).CreateODataBatchWriter(); batchWriter.WriteStartBatch(); // Assert Action test = () => ODataBatchResponseItem.WriteMessageAsync(batchWriter, response.HttpContext).Wait(); ODataException exception = ExceptionAssert.Throws <ODataException>(test); Assert.Equal("An asynchronous operation was called on a synchronous batch writer. Calls on a batch writer instance must be either all synchronous or all asynchronous.", exception.Message); }
public void WriteMessageAsync_SynchronousResponseContainsContentId_IfHasContentIdInRequestChangeSet() { // Arrange HeaderDictionary headers = new HeaderDictionary { { "Content-Type", $"multipart/mixed;charset=utf-8;boundary={Guid.NewGuid()}" }, }; MemoryStream ms = new MemoryStream(); IODataResponseMessage odataResponse = ODataMessageWrapperHelper.Create(ms, headers); string contentId = Guid.NewGuid().ToString(); HttpResponse httpResponse = CreateResponse("any", new HeaderDictionary(), "text/example;charset=utf-8"); httpResponse.HttpContext.Request.SetODataContentId(contentId); // Act ODataBatchWriter batchWriter = new ODataMessageWriter(odataResponse).CreateODataBatchWriter(); batchWriter.WriteStartBatch(); batchWriter.WriteStartChangeset(); // Assert Action test = () => ODataBatchResponseItem.WriteMessageAsync(batchWriter, httpResponse.HttpContext).Wait(); ODataException exception = ExceptionAssert.Throws <ODataException>(test); Assert.Equal("An asynchronous operation was called on a synchronous batch writer. Calls on a batch writer instance must be either all synchronous or all asynchronous.", exception.Message); }