Esempio n. 1
0
        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);
        }
Esempio n. 2
0
 public void WriteMessageAsync_NullWriter_Throws()
 {
     Assert.ThrowsArgumentNull(
         () => ODataBatchResponseItem.WriteMessageAsync(null, new HttpResponseMessage(), CancellationToken.None)
         .Wait(),
         "writer");
 }
Esempio n. 3
0
        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 async Task WriteMessageAsync_AsynchronouslyWritesResponseMessage()
        {
            // 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);

            HeaderDictionary responseHeaders = new HeaderDictionary
            {
                { "customHeader", "bar" }
            };
            HttpResponse response = CreateResponse("example content", responseHeaders, "text/example");

            // Act
            ODataBatchWriter batchWriter = await new ODataMessageWriter(odataResponse).CreateODataBatchWriterAsync();
            await batchWriter.WriteStartBatchAsync();

            await ODataBatchResponseItem.WriteMessageAsync(batchWriter, response.HttpContext);

            await batchWriter.WriteEndBatchAsync();

            ms.Position = 0;
            string result = new StreamReader(ms).ReadToEnd();

            // Assert
            Assert.Contains("example content", result);
            Assert.Contains("text/example", result);
            Assert.Contains("customHeader", result);
            Assert.Contains("bar", 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);
        }
        public async Task WriteMessageAsync_NullWriter_Throws()
        {
            // Arrange
            HttpContext context = new Mock <HttpContext>().Object;

            // Act & Assert
            await ExceptionAssert.ThrowsArgumentNullAsync(() => ODataBatchResponseItem.WriteMessageAsync(null, context), "writer");
        }
Esempio n. 8
0
        public async Task WriteMessageAsync_NullResponse_Throws()
        {
            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(new MemoryStream(), content.Headers);
            ODataMessageWriter    messageWriter = new ODataMessageWriter(odataResponse);

            await ExceptionAssert.ThrowsArgumentNullAsync(
                () => ODataBatchResponseItem.WriteMessageAsync(messageWriter.CreateODataBatchWriter(), null, CancellationToken.None),
                "response");
        }
        public void WriteMessageAsync_NullResponse_Throws()
        {
            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(new MemoryStream(), content.Headers);
            ODataMessageWriter    messageWriter = new ODataMessageWriter(odataResponse);

            Assert.ThrowsArgumentNull(
                () => ODataBatchResponseItem.WriteMessageAsync(messageWriter.CreateODataBatchWriter(), null).Wait(),
                "response");
        }
        public async Task WriteMessageAsync_NullContext_Throws()
        {
            // Arrange
            HeaderDictionary headers = new HeaderDictionary
            {
                { "Content-Type", $"multipart/mixed;charset=utf-8;boundary={Guid.NewGuid()}" },
            };
            IODataResponseMessage odataResponse = ODataMessageWrapperHelper.Create(new MemoryStream(), headers);
            ODataMessageWriter    messageWriter = new ODataMessageWriter(odataResponse);

            // Act & Assert
            await ExceptionAssert.ThrowsArgumentNullAsync(
                () => ODataBatchResponseItem.WriteMessageAsync(messageWriter.CreateODataBatchWriter(), null), "context");
        }
        public async Task WriteMessageAsync_ResponseContainsContentId_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 = await new ODataMessageWriter(odataResponse).CreateODataBatchWriterAsync();
            await batchWriter.WriteStartBatchAsync();

            await batchWriter.WriteStartChangesetAsync();

            await ODataBatchResponseItem.WriteMessageAsync(batchWriter, httpResponse.HttpContext);

            await batchWriter.WriteEndChangesetAsync();

            await batchWriter.WriteEndBatchAsync();

            ms.Position = 0;
            string result = new StreamReader(ms).ReadToEnd();

            // Assert
            Assert.Contains("any", result);
            Assert.Contains("text/example", result);
            Assert.Contains("Content-ID", result);
            Assert.Contains(contentId, result);
        }
Esempio n. 12
0
 public async Task WriteMessageAsync_NullWriter_Throws()
 {
     await ExceptionAssert.ThrowsArgumentNullAsync(
         () => ODataBatchResponseItem.WriteMessageAsync(null, new HttpResponseMessage(), CancellationToken.None),
         "writer");
 }