Example #1
0
 public void WriteOperationsOnRequestsShouldThrow()
 {
     using (MemoryStream stream = new MemoryStream())
     {
         var    context    = this.CreateJsonLightOutputContext(stream, writingResponse: false);
         var    serializer = new ODataJsonLightResourceSerializer(context);
         Action test       = () => serializer.WriteOperations(new ODataOperation[] { new ODataAction {
                                                                                         Metadata = new Uri("#foo", UriKind.Relative)
                                                                                     } }, /*isAction*/ true);
         test.Throws <ODataException>(ErrorStrings.WriterValidationUtils_OperationInRequest("#foo"));
     }
 }
Example #2
0
        private async Task <string> SetupJsonLightResourceSerializerAndRunTestAsync(Func <ODataJsonLightResourceSerializer, Task> func)
        {
            ODataJsonLightOutputContext jsonLightOutputContext = this.CreateJsonLightOutputContext();
            var jsonLightResourceSerializer = new ODataJsonLightResourceSerializer(jsonLightOutputContext);

            await jsonLightResourceSerializer.AsynchronousJsonWriter.StartObjectScopeAsync();

            await func(jsonLightResourceSerializer);

            await jsonLightResourceSerializer.JsonLightOutputContext.FlushAsync();

            await jsonLightResourceSerializer.AsynchronousJsonWriter.FlushAsync();

            this.stream.Position = 0;

            return(await new StreamReader(this.stream).ReadToEndAsync());
        }
Example #3
0
        private string SerializeJsonFragment(Action <ODataJsonLightResourceSerializer> writeWithSerializer)
        {
            string result;

            using (MemoryStream stream = new MemoryStream())
            {
                var context    = this.CreateJsonLightOutputContext(stream);
                var serializer = new ODataJsonLightResourceSerializer(context);
                serializer.JsonWriter.StartObjectScope();
                writeWithSerializer(serializer);
                serializer.JsonWriter.Flush();

                stream.Seek(0, SeekOrigin.Begin);
                result = new StreamReader(stream).ReadToEnd();
            }

            return(result);
        }
Example #4
0
        private void WriteOperationsAndValidatePayload(IEnumerable <ODataOperation> operations, string expectedPayload, bool isAction = true, bool setMetadataDocumentUri = true, bool writeMetadataAnnotation = false)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                var context    = this.CreateJsonLightOutputContext(stream, /*writingResponse*/ true, setMetadataDocumentUri);
                var serializer = new ODataJsonLightResourceSerializer(context);
                serializer.JsonWriter.StartObjectScope();
                if (writeMetadataAnnotation)
                {
                    serializer.WriteContextUriProperty(ODataPayloadKind.ServiceDocument);
                }

                serializer.WriteOperations(operations, isAction);
                serializer.JsonWriter.EndObjectScope();
                context.Flush();
                stream.Position = 0;
                string actualPayload = (new StreamReader(stream)).ReadToEnd();
                Assert.Equal(expectedPayload, actualPayload);
            }
        }