Пример #1
0
        public async Task WriteBatchRequestWithContentIdNullAsync()
        {
            var result = await SetupJsonLightBatchWriterAndRunTestAsync(
                async (jsonLightBatchWriter) =>
            {
                await jsonLightBatchWriter.WriteStartBatchAsync();

                var operationRequestMessage = await jsonLightBatchWriter.CreateOperationRequestMessageAsync(
                    "POST", new Uri($"{ServiceUri}/Customers"), /*contentId*/ null);

                using (var messageWriter = new ODataMessageWriter(operationRequestMessage))
                {
                    var jsonLightWriter = await messageWriter.CreateODataResourceWriterAsync(this.customerEntitySet, this.customerEntityType);

                    var customerResource = CreateCustomerResource(1);
                    await jsonLightWriter.WriteStartAsync(customerResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                await jsonLightBatchWriter.WriteEndBatchAsync();
            });

            Assert.StartsWith("{\"requests\":[{\"id\":\"", // id is a random Guid
                              result);
            Assert.EndsWith("\"," +
                            "\"method\":\"POST\"," +
                            "\"url\":\"http://tempuri.org/Customers\"," +
                            "\"headers\":{\"odata-version\":\"4.0\",\"content-type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\"}, " +
                            "\"body\" :{\"Id\":1,\"Name\":\"Customer 1\",\"Type\":\"Retail\"}}]}",
                            result);
        }
Пример #2
0
        public async Task WriteBatchResponseWithChangesetAsync()
        {
            var result = await SetupJsonLightBatchWriterAndRunTestAsync(
                async (jsonLightBatchWriter) =>
            {
                await jsonLightBatchWriter.WriteStartBatchAsync();
                await jsonLightBatchWriter.WriteStartChangesetAsync("69028f2c-f57b-4850-89f0-b7e5e002d4bc");

                var operationResponseMessage = await jsonLightBatchWriter.CreateOperationResponseMessageAsync("1");

                using (var messageWriter = new ODataMessageWriter(operationResponseMessage, this.settings, this.model))
                {
                    var jsonLightWriter = await messageWriter.CreateODataResourceWriterAsync(this.customerEntitySet, this.customerEntityType);

                    var customerResource = CreateCustomerResource(1);
                    await jsonLightWriter.WriteStartAsync(customerResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                await jsonLightBatchWriter.WriteEndChangesetAsync();
                await jsonLightBatchWriter.WriteEndBatchAsync();
            },
                /*writingRequest*/ false);

            Assert.Equal("{\"responses\":[{" +
                         "\"id\":\"1\"," +
                         "\"atomicityGroup\":\"69028f2c-f57b-4850-89f0-b7e5e002d4bc\"," +
                         "\"status\":0," +
                         "\"headers\":{\"odata-version\":\"4.0\",\"content-type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\"}, " +
                         "\"body\" :{\"@odata.context\":\"http://tempuri.org/$metadata#Customers/$entity\",\"Id\":1,\"Name\":\"Customer 1\",\"Type\":\"Retail\"}}]}",
                         result);
        }
Пример #3
0
        public async Task WriteBatchRequestWithRelativeUriAsync()
        {
            this.settings.BaseUri = new Uri(ServiceUri);

            var result = await SetupJsonLightBatchWriterAndRunTestAsync(
                async (jsonLightBatchWriter) =>
            {
                await jsonLightBatchWriter.WriteStartBatchAsync();

                var operationRequestMessage = await jsonLightBatchWriter.CreateOperationRequestMessageAsync(
                    "POST", new Uri("/odata/Customers", UriKind.Relative), "1", BatchPayloadUriOption.RelativeUri);

                using (var messageWriter = new ODataMessageWriter(operationRequestMessage))
                {
                    var jsonLightWriter = await messageWriter.CreateODataResourceWriterAsync(this.customerEntitySet, this.customerEntityType);

                    var customerResource = CreateCustomerResource(1);
                    await jsonLightWriter.WriteStartAsync(customerResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                await jsonLightBatchWriter.WriteEndBatchAsync();
            });

            Assert.Equal(
                "{\"requests\":[{" +
                "\"id\":\"1\"," +
                "\"method\":\"POST\"," +
                "\"url\":\"odata/Customers\"," +
                "\"headers\":{\"odata-version\":\"4.0\",\"content-type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\"}, " +
                "\"body\" :{\"Id\":1,\"Name\":\"Customer 1\",\"Type\":\"Retail\"}}]}",
                result);
        }
Пример #4
0
        public async Task WriteBatchRequestAsync_ThrowsExceptionForInvalidTransitionFromOperationStreamDisposed()
        {
            var exception = await Assert.ThrowsAsync <ODataException>(
                () => SetupJsonLightBatchWriterAndRunTestAsync(
                    async(jsonLightBatchWriter) =>
            {
                await jsonLightBatchWriter.WriteStartBatchAsync();

                var operationRequestMessage = await jsonLightBatchWriter.CreateOperationRequestMessageAsync(
                    "POST", new Uri($"{ServiceUri}/Customers"), "1");

                using (var messageWriter = new ODataMessageWriter(operationRequestMessage))
                {
                    var jsonLightWriter = await messageWriter.CreateODataResourceWriterAsync(this.customerEntitySet, this.customerEntityType);

                    var customerResource = CreateCustomerResource(1);
                    await jsonLightWriter.WriteStartAsync(customerResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                // Try to start writing batch after operation stream disposed
                await jsonLightBatchWriter.WriteStartBatchAsync();
            }));

            Assert.Equal(Strings.ODataBatchWriter_InvalidTransitionFromOperationContentStreamDisposed, exception.Message);
        }
        public async Task WriteBatchAsync()
        {
            var result = await SetupJsonLightOutputContextAndRunTestAsync(
                async (jsonLightOutputContext) =>
            {
                var batchWriter = await jsonLightOutputContext.CreateODataBatchWriterAsync();

                await batchWriter.WriteStartBatchAsync();
                var operationRequestMessage = await batchWriter.CreateOperationRequestMessageAsync(
                    "POST", new Uri($"{ServiceUri}/Orders"), "1");

                using (var messageWriter = new ODataMessageWriter(operationRequestMessage))
                {
                    var resourceWriter = await messageWriter.CreateODataResourceWriterAsync(this.orderEntitySet, this.orderEntityType);
                    var orderResource  = CreateOrderResource();

                    await resourceWriter.WriteStartAsync(orderResource);
                    await resourceWriter.WriteEndAsync();
                }

                await batchWriter.WriteEndBatchAsync();
            },
                /*writingResponse*/ false);

            Assert.Equal(
                "{\"requests\":[{" +
                "\"id\":\"1\"," +
                "\"method\":\"POST\"," +
                "\"url\":\"http://tempuri.org/Orders\"," +
                "\"headers\":{\"odata-version\":\"4.0\",\"content-type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\"}, " +
                "\"body\" :{\"Id\":1,\"Amount\":13}}]}",
                result);
        }
Пример #6
0
        protected override async Task <Stream> WriteEntryContentAsync(string method, string collection, string commandText, IDictionary <string, object> entryData, bool resultRequired)
        {
            var message = IsBatch
                ? await CreateBatchOperationMessageAsync(method, collection, entryData, commandText, resultRequired).ConfigureAwait(false)
                : new ODataRequestMessage();

            if (method == RestVerbs.Get || method == RestVerbs.Delete)
            {
                return(null);
            }

            var entityType = _model.FindDeclaredType(
                _session.Metadata.GetQualifiedTypeName(collection)) as IEdmEntityType;
            var model = (method == RestVerbs.Patch || method == RestVerbs.Merge) ? new EdmDeltaModel(_model, entityType, entryData.Keys) : _model;

            using (var messageWriter = new ODataMessageWriter(message, GetWriterSettings(), model))
            {
                var contentId        = _deferredBatchWriter?.Value.GetContentId(entryData, null);
                var entityCollection = _session.Metadata.NavigateToCollection(collection);
                var entryDetails     = _session.Metadata.ParseEntryDetails(entityCollection.Name, entryData, contentId);

                var entryWriter = await messageWriter.CreateODataResourceWriterAsync().ConfigureAwait(false);

                var entry = CreateODataEntry(entityType.FullName(), entryDetails.Properties, null);

                RegisterRootEntry(entry);
                await WriteEntryPropertiesAsync(entryWriter, entry, entryDetails.Links).ConfigureAwait(false);

                UnregisterRootEntry(entry);

                return(IsBatch ? null : await message.GetStreamAsync().ConfigureAwait(false));
            }
        }
Пример #7
0
        public async Task WriteBatchRequestWithChangesetAndDependsOnIdsAsync()
        {
            var result = await SetupJsonLightBatchWriterAndRunTestAsync(
                async (jsonLightBatchWriter) =>
            {
                await jsonLightBatchWriter.WriteStartBatchAsync();
                await jsonLightBatchWriter.WriteStartChangesetAsync("69028f2c-f57b-4850-89f0-b7e5e002d4bc");

                var operationRequestMessage1 = await jsonLightBatchWriter.CreateOperationRequestMessageAsync(
                    "POST", new Uri($"{ServiceUri}/Customers"), "1");

                using (var messageWriter1 = new ODataMessageWriter(operationRequestMessage1))
                {
                    var jsonLightWriter = await messageWriter1.CreateODataResourceWriterAsync(this.customerEntitySet, this.customerEntityType);

                    var customerResource = CreateCustomerResource(1);
                    await jsonLightWriter.WriteStartAsync(customerResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                // Operation request depends on the previous (Content ID: 1)
                var dependsOnIds = new List <string> {
                    "1"
                };
                var operationRequestMessage2 = await jsonLightBatchWriter.CreateOperationRequestMessageAsync(
                    "POST", new Uri($"{ServiceUri}/Orders"), "2", BatchPayloadUriOption.AbsoluteUri, dependsOnIds);

                using (var messageWriter2 = new ODataMessageWriter(operationRequestMessage2))
                {
                    var jsonLightWriter = await messageWriter2.CreateODataResourceWriterAsync(this.orderEntitySet, this.orderEntityType);

                    var orderResource = CreateOrderResource(1);
                    await jsonLightWriter.WriteStartAsync(orderResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                await jsonLightBatchWriter.WriteEndChangesetAsync();
                await jsonLightBatchWriter.WriteEndBatchAsync();
            });

            Assert.Equal(
                "{\"requests\":[" +
                "{\"id\":\"1\"," +
                "\"atomicityGroup\":\"69028f2c-f57b-4850-89f0-b7e5e002d4bc\"," +
                "\"method\":\"POST\"," +
                "\"url\":\"http://tempuri.org/Customers\"," +
                "\"headers\":{\"odata-version\":\"4.0\",\"content-type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\"}, " +
                "\"body\" :{\"Id\":1,\"Name\":\"Customer 1\",\"Type\":\"Retail\"}}," +
                "{\"id\":\"2\"," +
                "\"atomicityGroup\":\"69028f2c-f57b-4850-89f0-b7e5e002d4bc\"," +
                "\"dependsOn\":[\"1\"]," +
                "\"method\":\"POST\"," +
                "\"url\":\"http://tempuri.org/Orders\"," +
                "\"headers\":{\"odata-version\":\"4.0\",\"content-type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\"}, " +
                "\"body\" :{\"Id\":1,\"CustomerId\":1,\"Amount\":13}}]}",
                result);
        }
Пример #8
0
        public async Task WriteResponseMessageAsync()
        {
            var result = await SetupRawOutputContextAndRunTestAsync(
                async (rawOutputContext) =>
            {
                var asynchronousWriter     = await rawOutputContext.CreateODataAsynchronousWriterAsync();
                var responseMessage        = await asynchronousWriter.CreateResponseMessageAsync();
                responseMessage.StatusCode = 200;
                responseMessage.SetHeader(ODataConstants.ContentTypeHeader, MimeConstants.MimeApplicationJson);

                var writerSettings = new ODataMessageWriterSettings
                {
                    Version = ODataVersion.V4,
                    EnableMessageStreamDisposal = false
                };

                writerSettings.SetServiceDocumentUri(new Uri(ServiceUri));

                using (var messageWriter = new ODataMessageWriter(responseMessage, writerSettings, this.model))
                {
                    var jsonLightWriter  = await messageWriter.CreateODataResourceWriterAsync(this.customerEntitySet, this.customerEntityType);
                    var customerResponse = new ODataResource
                    {
                        TypeName   = "NS.Customer",
                        Properties = new List <ODataProperty>
                        {
                            new ODataProperty
                            {
                                Name              = "Id",
                                Value             = 1,
                                SerializationInfo = new ODataPropertySerializationInfo {
                                    PropertyKind = ODataPropertyKind.Key
                                }
                            },
                            new ODataProperty {
                                Name = "Name", Value = "Customer 1"
                            }
                        }
                    };

                    await jsonLightWriter.WriteStartAsync(customerResponse);
                    await jsonLightWriter.WriteEndAsync();
                }
            });

            var expected = @"HTTP/1.1 200 OK
Content-Type: application/json
OData-Version: 4.0

{""@odata.context"":""http://tempuri.org/$metadata#Customers/$entity"",""Id"":1,""Name"":""Customer 1""}";

            Assert.Equal(expected, result);
        }
Пример #9
0
        private async Task WriteEntity(IEdmEntitySet entitySet, ODataResource entry, Stream stream)
        {
            IODataResponseMessage responseMessage = new Infrastructure.OeInMemoryMessage(stream, null);

            using (ODataMessageWriter messageWriter = new ODataMessageWriter(responseMessage, _settings, _model.GetEdmModel(entitySet)))
            {
                ODataUtils.SetHeadersForPayload(messageWriter, ODataPayloadKind.Resource);
                ODataWriter writer = await messageWriter.CreateODataResourceWriterAsync(entitySet, entitySet.EntityType());

                await writer.WriteStartAsync(entry);

                await writer.WriteEndAsync();
            }
        }
        public async Task WriteMultipartMixedBatchRequestWithGroupIdForChangesetNotSpecifiedAsync()
        {
            var result = await SetupMultipartMixedBatchWriterAndRunTestAsync(
                async (multipartMixedBatchWriter) =>
            {
                await multipartMixedBatchWriter.WriteStartBatchAsync();
                await multipartMixedBatchWriter.WriteStartChangesetAsync();

                var operationRequestMessage = await multipartMixedBatchWriter.CreateOperationRequestMessageAsync(
                    "POST", new Uri($"{ServiceUri}/Orders"), "1");

                using (var messageWriter = new ODataMessageWriter(operationRequestMessage))
                {
                    var jsonLightWriter = await messageWriter.CreateODataResourceWriterAsync(this.orderEntitySet, this.orderEntityType);

                    var orderResource = CreateOrderResource();
                    await jsonLightWriter.WriteStartAsync(orderResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                await multipartMixedBatchWriter.WriteEndChangesetAsync();
                await multipartMixedBatchWriter.WriteEndBatchAsync();
            });

            var expectedPart1 = @"--batch_aed653ab
Content-Type: multipart/mixed; boundary=changeset_"; // atomicityGroup is a random Guid
            var expectedPart2 = @"
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

POST http://tempuri.org/Orders HTTP/1.1
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{""Id"":1,""Amount"":13}
--changeset_";                                       // atomicityGroup is a random Guid
            var expectedPart3 = @"--
--batch_aed653ab--
";

            Assert.StartsWith(expectedPart1, result);
            Assert.Contains(expectedPart2, result);
            Assert.EndsWith(expectedPart3, result);
        }
Пример #11
0
        public async Task WriteResponseMessageAsync()
        {
            var result = await SetupAsynchronousWriterAndRunTestAsync(
                async (asynchronousWriter) =>
            {
                var responseMessage        = await asynchronousWriter.CreateResponseMessageAsync();
                responseMessage.StatusCode = 200;
                responseMessage.SetHeader(ODataConstants.ContentTypeHeader, MimeConstants.MimeApplicationJson);

                var writerSettings = new ODataMessageWriterSettings
                {
                    Version = ODataVersion.V4,
                    EnableMessageStreamDisposal = false
                };

                writerSettings.SetServiceDocumentUri(new Uri(ServiceDocumentUri));

                using (var messageWriter = new ODataMessageWriter(responseMessage, writerSettings, this.userModel))
                {
                    var jsonLightWriter = await messageWriter.CreateODataResourceWriterAsync(this.singleton, this.testType);
                    var testResponse    = new ODataResource
                    {
                        TypeName   = "NS.Test",
                        Properties = new List <ODataProperty>
                        {
                            new ODataProperty {
                                Name = "Id", Value = 1
                            }
                        }
                    };

                    await jsonLightWriter.WriteStartAsync(testResponse);
                    await jsonLightWriter.WriteEndAsync();
                }
            });

            var expected = @"HTTP/1.1 200 OK
Content-Type: application/json
OData-Version: 4.0

{""@odata.context"":""http://host/service/$metadata#MySingleton"",""Id"":1}";

            Assert.Equal(expected, result);
        }
        public async Task WriteMultipartMixedBatchRequestWithRelativeUriAsync()
        {
            this.settings.BaseUri = new Uri(ServiceUri);

            var result = await SetupMultipartMixedBatchWriterAndRunTestAsync(
                async (multipartMixedBatchWriter) =>
            {
                await multipartMixedBatchWriter.WriteStartBatchAsync();

                var operationRequestMessage = await multipartMixedBatchWriter.CreateOperationRequestMessageAsync(
                    "POST", new Uri("/Orders", UriKind.Relative), /*contentId*/ null, BatchPayloadUriOption.RelativeUri);

                using (var messageWriter = new ODataMessageWriter(operationRequestMessage))
                {
                    var jsonLightWriter = await messageWriter.CreateODataResourceWriterAsync(this.orderEntitySet, this.orderEntityType);

                    var orderResource = CreateOrderResource();
                    await jsonLightWriter.WriteStartAsync(orderResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                await multipartMixedBatchWriter.WriteEndBatchAsync();
            });

            var expected = @"--batch_aed653ab
Content-Type: application/http
Content-Transfer-Encoding: binary

POST Orders HTTP/1.1
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{""Id"":1,""Amount"":13}
--batch_aed653ab--
";

            Assert.Equal(expected, result);
        }
        public async Task WriteMultipartMixedBatchResponseAsync()
        {
            var result = await SetupMultipartMixedBatchWriterAndRunTestAsync(
                async (multipartMixedBatchWriter) =>
            {
                await multipartMixedBatchWriter.WriteStartBatchAsync();

                var operationResponseMessage        = await multipartMixedBatchWriter.CreateOperationResponseMessageAsync("1");
                operationResponseMessage.StatusCode = 200;

                using (var messageWriter = new ODataMessageWriter(operationResponseMessage, this.settings))
                {
                    var jsonLightWriter = await messageWriter.CreateODataResourceWriterAsync(this.orderEntitySet, this.orderEntityType);

                    var orderResource = CreateOrderResource();
                    await jsonLightWriter.WriteStartAsync(orderResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                await multipartMixedBatchWriter.WriteEndBatchAsync();
            },
                writingRequest : false);

            var expected = @"--batch_aed653ab
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 200 OK
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{""@odata.context"":""http://tempuri.org/$metadata#Orders/$entity"",""Id"":1,""Amount"":13}
--batch_aed653ab--
";

            Assert.Equal(expected, result);
        }
        public async Task WriteMultipartMixedBatchRequestAsync()
        {
            var result = await SetupMultipartMixedBatchWriterAndRunTestAsync(
                async (multipartMixedBatchWriter) =>
            {
                await multipartMixedBatchWriter.WriteStartBatchAsync();

                var operationRequestMessage = await multipartMixedBatchWriter.CreateOperationRequestMessageAsync(
                    "POST", new Uri($"{ServiceUri}/Orders"), "1");

                using (var messageWriter = new ODataMessageWriter(operationRequestMessage))
                {
                    var jsonLightWriter = await messageWriter.CreateODataResourceWriterAsync(this.orderEntitySet, this.orderEntityType);

                    var orderResource = CreateOrderResource();
                    await jsonLightWriter.WriteStartAsync(orderResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                await multipartMixedBatchWriter.WriteEndBatchAsync();
            },
                writingRequest : true);

            var expected = @"--batch_aed653ab
Content-Type: application/http
Content-Transfer-Encoding: binary

POST http://tempuri.org/Orders HTTP/1.1
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{""Id"":1,""Amount"":13}
--batch_aed653ab--
";

            Assert.Equal(expected, result);
        }
        public async Task WriteMultipartMixedBatchRequestWithDependsOnIdsAsync()
        {
            var result = await SetupMultipartMixedBatchWriterAndRunTestAsync(
                async (multipartMixedBatchWriter) =>
            {
                await multipartMixedBatchWriter.WriteStartBatchAsync();
                await multipartMixedBatchWriter.WriteStartChangesetAsync("ec3a8d4f");

                var operationRequestMessage1 = await multipartMixedBatchWriter.CreateOperationRequestMessageAsync(
                    "POST", new Uri($"{ServiceUri}/Customers"), "1");

                using (var messageWriter1 = new ODataMessageWriter(operationRequestMessage1))
                {
                    var jsonLightWriter = await messageWriter1.CreateODataResourceWriterAsync(this.customerEntitySet, this.customerEntityType);

                    var customerResource = CreateCustomerResource();
                    await jsonLightWriter.WriteStartAsync(customerResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                // Operation request depends on the previous (Content ID: 1)
                var dependsOnIds = new List <string> {
                    "1"
                };
                var operationRequestMessage2 = await multipartMixedBatchWriter.CreateOperationRequestMessageAsync(
                    "POST", new Uri($"{ServiceUri}/Orders"), "2", BatchPayloadUriOption.AbsoluteUri, dependsOnIds);

                using (var messageWriter1 = new ODataMessageWriter(operationRequestMessage2))
                {
                    var jsonLightWriter = await messageWriter1.CreateODataResourceWriterAsync(this.orderEntitySet, this.orderEntityType);

                    var orderResource = CreateOrderResource();
                    await jsonLightWriter.WriteStartAsync(orderResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                await multipartMixedBatchWriter.WriteEndChangesetAsync();
                await multipartMixedBatchWriter.WriteEndBatchAsync();
            });

            var expected = @"--batch_aed653ab
Content-Type: multipart/mixed; boundary=changeset_ec3a8d4f

--changeset_ec3a8d4f
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

POST http://tempuri.org/Customers HTTP/1.1
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{""Id"":1,""Name"":""Customer 1""}
--changeset_ec3a8d4f
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 2

POST http://tempuri.org/Orders HTTP/1.1
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{""Id"":1,""Amount"":13}
--changeset_ec3a8d4f--
--batch_aed653ab--
";

            Assert.Equal(expected, result);
        }
        public async Task WriteMultipartMixedBatchResponseWithChangesetAsync()
        {
            var result = await SetupMultipartMixedBatchWriterAndRunTestAsync(
                async (multipartMixedBatchWriter) =>
            {
                await multipartMixedBatchWriter.WriteStartBatchAsync();
                await multipartMixedBatchWriter.WriteStartChangesetAsync("ec3a8d4f");

                var operationResponseMessage1        = await multipartMixedBatchWriter.CreateOperationResponseMessageAsync("1");
                operationResponseMessage1.StatusCode = 200;

                using (var messageWriter1 = new ODataMessageWriter(operationResponseMessage1, this.settings))
                {
                    var jsonLightWriter = await messageWriter1.CreateODataResourceWriterAsync(this.orderEntitySet, this.orderEntityType);

                    var orderResource = CreateOrderResource();
                    await jsonLightWriter.WriteStartAsync(orderResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                await multipartMixedBatchWriter.WriteEndChangesetAsync();
                await multipartMixedBatchWriter.WriteStartChangesetAsync("f46c46e2");

                var operationResponseMessage2        = await multipartMixedBatchWriter.CreateOperationResponseMessageAsync("2");
                operationResponseMessage2.StatusCode = 200;

                using (var messageWriter2 = new ODataMessageWriter(operationResponseMessage2, this.settings))
                {
                    var jsonLightWriter = await messageWriter2.CreateODataResourceWriterAsync(this.customerEntitySet, this.customerEntityType);

                    var customerResource = CreateCustomerResource();
                    await jsonLightWriter.WriteStartAsync(customerResource);
                    await jsonLightWriter.WriteEndAsync();
                }

                await multipartMixedBatchWriter.WriteEndChangesetAsync();
                await multipartMixedBatchWriter.WriteEndBatchAsync();
            },
                writingRequest : false);

            var expected = @"--batch_aed653ab
Content-Type: multipart/mixed; boundary=changesetresponse_ec3a8d4f

--changesetresponse_ec3a8d4f
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 1

HTTP/1.1 200 OK
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{""@odata.context"":""http://tempuri.org/$metadata#Orders/$entity"",""Id"":1,""Amount"":13}
--changesetresponse_ec3a8d4f--
--batch_aed653ab
Content-Type: multipart/mixed; boundary=changesetresponse_f46c46e2

--changesetresponse_f46c46e2
Content-Type: application/http
Content-Transfer-Encoding: binary
Content-ID: 2

HTTP/1.1 200 OK
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{""@odata.context"":""http://tempuri.org/$metadata#Customers/$entity"",""Id"":1,""Name"":""Customer 1""}
--changesetresponse_f46c46e2--
--batch_aed653ab--
";

            Assert.Equal(expected, result);
        }
Пример #17
0
        public async Task WriteInStreamError_APIsShouldYieldSameResult()
        {
            var nestedWriterSettings = new ODataMessageWriterSettings
            {
                Version = ODataVersion.V4,
                EnableMessageStreamDisposal = false
            };

            nestedWriterSettings.SetServiceDocumentUri(new Uri(ServiceUri));

            var asyncException = await Assert.ThrowsAsync <ODataException>(async() =>
            {
                IODataResponseMessage asyncResponseMessage = new InMemoryMessage {
                    StatusCode = 200, Stream = this.asyncStream
                };
                using (var messageWriter = new ODataMessageWriter(asyncResponseMessage, writerSettings))
                {
                    // Call to CreateODataAsynchronousWriterAsync triggers setting of output in-stream error listener
                    var asynchronousWriter     = await messageWriter.CreateODataAsynchronousWriterAsync();
                    var responseMessage        = await asynchronousWriter.CreateResponseMessageAsync();
                    responseMessage.StatusCode = 200;

                    // Next section added is to demonstrate that what was already written is flushed to the buffer before exception is thrown
                    using (var nestedMessageWriter = new ODataMessageWriter(responseMessage, nestedWriterSettings))
                    {
                        var writer = await nestedMessageWriter.CreateODataResourceWriterAsync();
                    }

                    await messageWriter.WriteErrorAsync(
                        new ODataError {
                        ErrorCode = "NRE", Message = "Object reference not set to an instance of an object."
                    },
                        /*includeDebugInformation*/ true);
                }
            });

            this.asyncStream.Position = 0;
            var asyncResult = await new StreamReader(this.asyncStream).ReadToEndAsync();

            var syncException = await Assert.ThrowsAsync <ODataException>(
                () => TaskUtils.GetTaskForSynchronousOperation(() =>
            {
                IODataResponseMessage syncResponseMessage = new InMemoryMessage {
                    StatusCode = 200, Stream = this.syncStream
                };
                using (var messageWriter = new ODataMessageWriter(syncResponseMessage, writerSettings))
                {
                    // Call to CreateODataAsynchronousWriterAsync triggers setting of output in-stream error listener
                    var asynchronousWriter     = messageWriter.CreateODataAsynchronousWriter();
                    var responseMessage        = asynchronousWriter.CreateResponseMessage();
                    responseMessage.StatusCode = 200;

                    // Next section is added to demonstrate that what was already written is flushed to the buffer before exception is thrown
                    using (var nestedMessageWriter = new ODataMessageWriter(responseMessage, nestedWriterSettings))
                    {
                        var writer = nestedMessageWriter.CreateODataResourceWriter();
                    }

                    messageWriter.WriteError(
                        new ODataError {
                        ErrorCode = "NRE", Message = "Object reference not set to an instance of an object."
                    },
                        /*includeDebugInformation*/ true);
                }
            }));

            this.syncStream.Position = 0;
            var syncResult = await new StreamReader(this.syncStream).ReadToEndAsync();

            var expected = @"HTTP/1.1 200 OK
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

";

            Assert.Equal(Strings.ODataAsyncWriter_CannotWriteInStreamErrorForAsync, asyncException.Message);
            Assert.Equal(Strings.ODataAsyncWriter_CannotWriteInStreamErrorForAsync, syncException.Message);
            Assert.Equal(expected, asyncResult);
            Assert.Equal(expected, syncResult);
        }
Пример #18
0
        public async Task WriteResponseMessage_APIsShouldYieldSameResult()
        {
            var nestedWriterSettings = new ODataMessageWriterSettings
            {
                Version = ODataVersion.V4,
                EnableMessageStreamDisposal = false
            };

            nestedWriterSettings.SetServiceDocumentUri(new Uri(ServiceUri));
            var customerResponse = new ODataResource
            {
                TypeName   = "NS.Customer",
                Properties = new List <ODataProperty>
                {
                    new ODataProperty
                    {
                        Name              = "Id",
                        Value             = 1,
                        SerializationInfo = new ODataPropertySerializationInfo {
                            PropertyKind = ODataPropertyKind.Key
                        }
                    },
                    new ODataProperty {
                        Name = "Name", Value = "Customer 1"
                    }
                }
            };

            IODataResponseMessage asyncResponseMessage = new InMemoryMessage {
                StatusCode = 200, Stream = this.asyncStream
            };

            using (var messageWriter = new ODataMessageWriter(asyncResponseMessage, writerSettings))
            {
                var asynchronousWriter = await messageWriter.CreateODataAsynchronousWriterAsync();

                var responseMessage = await asynchronousWriter.CreateResponseMessageAsync();

                responseMessage.StatusCode = 200;

                using (var nestedMessageWriter = new ODataMessageWriter(responseMessage, nestedWriterSettings, this.model))
                {
                    var writer = await nestedMessageWriter.CreateODataResourceWriterAsync(this.customerEntitySet, this.customerEntityType);

                    await writer.WriteStartAsync(customerResponse);

                    await writer.WriteEndAsync();

                    await writer.FlushAsync();
                }

                await asynchronousWriter.FlushAsync();
            }

            this.asyncStream.Position = 0;
            var asyncResult = await new StreamReader(this.asyncStream).ReadToEndAsync();

            var syncResult = await TaskUtils.GetTaskForSynchronousOperation(() =>
            {
                IODataResponseMessage syncResponseMessage = new InMemoryMessage {
                    StatusCode = 200, Stream = this.syncStream
                };
                using (var messageWriter = new ODataMessageWriter(syncResponseMessage, writerSettings))
                {
                    var asynchronousWriter     = messageWriter.CreateODataAsynchronousWriter();
                    var responseMessage        = asynchronousWriter.CreateResponseMessage();
                    responseMessage.StatusCode = 200;

                    using (var nestedMessageWriter = new ODataMessageWriter(responseMessage, nestedWriterSettings, this.model))
                    {
                        var writer = nestedMessageWriter.CreateODataResourceWriter(this.customerEntitySet, this.customerEntityType);

                        writer.WriteStart(customerResponse);
                        writer.WriteEnd();
                        writer.Flush();
                    }

                    asynchronousWriter.Flush();
                }

                this.syncStream.Position = 0;
                return(new StreamReader(this.syncStream).ReadToEnd());
            });

            var expected = @"HTTP/1.1 200 OK
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

{""@odata.context"":""http://tempuri.org/$metadata#Customers/$entity"",""Id"":1,""Name"":""Customer 1""}";

            Assert.Equal(expected, asyncResult);
            Assert.Equal(expected, syncResult);
        }