Пример #1
0
        public async Task WriteStartAsync_ThrowsExceptionForWriterInCanWriteParameterState()
        {
            var exception = await Assert.ThrowsAsync <ODataException>(
                async() =>
            {
                var jsonLightOutputContext   = CreateJsonLightOutputContext(new MemoryStream(), writingResponse: false, synchronous: false);
                var jsonLightParameterWriter = new ODataJsonLightParameterWriter(jsonLightOutputContext, operation: null);

                await jsonLightParameterWriter.WriteStartAsync();
                await jsonLightParameterWriter.WriteStartAsync();
            });

            Assert.Equal(Strings.ODataParameterWriterCore_CannotWriteStart, exception.Message);
        }
        /// <summary>
        /// Sets up an ODataJsonLightParameterWriter,
        /// then runs the given test code asynchronously,
        /// then flushes and reads the stream back as a string for customized verification.
        /// </summary>
        private async Task <string> SetupJsonLightParameterWriterAndRunTestAsync(
            Func <ODataJsonLightParameterWriter, Task> func,
            IEdmOperation edmOperation = null,
            bool writingResponse       = true)
        {
            var stream = new MemoryStream();

            var jsonLightOutputContext = CreateJsonLightOutputContext(
                stream,
                /*writingResponse*/ writingResponse,
                /*synchronous*/ false);

            var jsonLightParameterWriter = new ODataJsonLightParameterWriter(
                jsonLightOutputContext,
                edmOperation);

            await jsonLightParameterWriter.WriteStartAsync();

            await func(jsonLightParameterWriter);

            await jsonLightParameterWriter.WriteEndAsync();

            stream.Position = 0;
            return(await new StreamReader(stream).ReadToEndAsync());
        }
Пример #3
0
        public void AsyncShouldWriteParameterPayloadInRequestWithoutModelAndWithoutFunctionImport()
        {
            Action <ODataJsonLightOutputContext> test = outputContext =>
            {
                var parameterWriter = new ODataJsonLightParameterWriter(outputContext, operation: null);
                parameterWriter.WriteStartAsync().Wait();
                parameterWriter.WriteValueAsync("primitive", Guid.Empty).Wait();
                parameterWriter.WriteValueAsync("complex", new ODataComplexValue {
                    Properties = new[] { new ODataProperty {
                                             Name = "prop1", Value = 1
                                         } }
                }).Wait();
                var collectionWriter = parameterWriter.CreateCollectionWriterAsync("collection").Result;
                collectionWriter.WriteStartAsync(new ODataCollectionStart()).Wait();
                collectionWriter.WriteItemAsync("item1").Wait();
                collectionWriter.WriteEndAsync().Wait();
                parameterWriter.WriteEndAsync().Wait();
                parameterWriter.FlushAsync().Wait();
            };

            WriteAndValidate(test, "{\"primitive\":\"00000000-0000-0000-0000-000000000000\",\"complex\":{\"prop1\":1},\"collection\":[\"item1\"]}", writingResponse: false, synchronous: false);
        }