public void ContentTypeHeaderParsingErrorTest()
        {
            var testCases = new ContentTypeTestCase[]
            {
                new ContentTypeTestCase
                {
                    ContentType       = null,
                    ExpectedException = ODataExpectedExceptions.ODataContentTypeException("ODataMessageReader_NoneOrEmptyContentTypeHeader")
                },
                new ContentTypeTestCase
                {
                    ContentType       = string.Empty,
                    ExpectedException = ODataExpectedExceptions.ODataContentTypeException("ODataMessageReader_NoneOrEmptyContentTypeHeader")
                },
                new ContentTypeTestCase
                {
                    ContentType       = ";foo=bar",
                    ExpectedException = ODataExpectedExceptions.ODataContentTypeException("HttpUtils_MediaTypeRequiresSlash", ";foo=bar")
                },
                new ContentTypeTestCase
                {
                    ContentType       = "application/*",
                    ExpectedException = ODataExpectedExceptions.ODataContentTypeException("ODataMessageReader_WildcardInContentType", "application/*")
                },
                new ContentTypeTestCase
                {
                    ContentType       = "*/*",
                    ExpectedException = ODataExpectedExceptions.ODataContentTypeException("ODataMessageReader_WildcardInContentType", "*/*")
                },
                new ContentTypeTestCase
                {
                    ContentType       = "application/json, application/xml",
                    ExpectedException = ODataExpectedExceptions.ODataContentTypeException("MediaTypeUtils_NoOrMoreThanOneContentTypeSpecified", "application/json, application/xml")
                },
            };

            this.CombinatorialEngineProvider.RunCombinations(
                testCases,
                this.ReaderTestConfigurationProvider.AllFormatConfigurations.Where(tc => tc.Format != ODataFormat.Json),
                (testCase, testConfiguration) =>
            {
                // create a message reader and call GetFormat; this should fail with the expected error message
                TestMessage testMessage = TestReaderUtils.CreateInputMessageFromStream(new TestStream(), testConfiguration);
                testMessage.SetHeader(Microsoft.OData.Core.ODataConstants.ContentTypeHeader, testCase.ContentType);
                testMessage.SetHeader(Microsoft.OData.Core.ODataConstants.ContentLengthHeader, testCase.ContentLength.ToString());

                TestExceptionUtils.ExpectedException(
                    this.Assert,
                    () =>
                {
                    using (ODataMessageReaderTestWrapper messageReader = TestReaderUtils.CreateMessageReader(testMessage, null, testConfiguration))
                    {
                        ODataFormat actualFormat = messageReader.DetectPayloadKind().Single().Format;
                    }
                },
                    testCase.ExpectedException,
                    this.ExceptionVerifier);
            });
        }
Example #2
0
            protected override TestMessage CreateInputMessage(ReaderTestConfiguration testConfiguration)
            {
                TestMessage testMessage = base.CreateInputMessage(testConfiguration);

                testMessage.SetHeader(ODataConstants.ContentTypeHeader, contentType);
                return(testMessage);
            }
Example #3
0
        /// <summary>
        /// Called to create the input message for the reader test and sets the specified value for the Content-Type header.
        /// </summary>
        /// <param name="testConfiguration">The test configuration.</param>
        /// <returns>The newly created test message to use.</returns>
        protected override TestMessage CreateInputMessage(ReaderTestConfiguration testConfiguration)
        {
            TestMessage testMessage = base.CreateInputMessage(testConfiguration);

            // overwrite the content-type header of the message
            testMessage.SetHeader(ODataConstants.ContentTypeHeader, this.ContentType);

            return(testMessage);
        }
Example #4
0
            /// <summary>
            /// Called to create the input message for the reader test. Replaces the default DSV header with the
            /// value specified in the constructor.
            /// </summary>
            /// <param name="testConfiguration">The test configuration.</param>
            /// <returns>The newly created test message to use.</returns>
            protected override TestMessage CreateInputMessage(ReaderTestConfiguration testConfiguration)
            {
                TestMessage testMessage = base.CreateInputMessage(testConfiguration);

                // now overwrite the DSV header
                testMessage.SetHeader(Microsoft.OData.ODataConstants.ODataVersionHeader, this.DataServiceVersion);

                return(testMessage);
            }
        public void BatchContentTypeHeaderParsingTest()
        {
            IEnumerable <ContentTypeTestCase> testCases = new ContentTypeTestCase[]
            {
                // correct batch content type
                new ContentTypeTestCase
                {
                    ContentType    = "multipart/mixed;boundary=--aa_bb_cc--",
                    ExpectedFormat = ODataFormat.Batch,
                },

                // missing batch boundary
                new ContentTypeTestCase
                {
                    ContentType       = "multipart/mixed",
                    ExpectedFormat    = ODataFormat.Batch,
                    ExpectedException = ODataExpectedExceptions.ODataException("MediaTypeUtils_BoundaryMustBeSpecifiedForBatchPayloads", "multipart/mixed", "boundary")
                },

                // multiple batch boundary parameters
                new ContentTypeTestCase
                {
                    ContentType       = "multipart/mixed;boundary=boundary1;boundary=boundary2",
                    ExpectedFormat    = ODataFormat.Batch,
                    ExpectedException = ODataExpectedExceptions.ODataException("MediaTypeUtils_BoundaryMustBeSpecifiedForBatchPayloads", "multipart/mixed;boundary=boundary1;boundary=boundary2", "boundary")
                },

                // invalid batch content types
                new ContentTypeTestCase
                {
                    ContentType       = "multipart/bar",
                    ExpectedFormat    = ODataFormat.Batch,
                    ExpectedException = ODataExpectedExceptions.ODataContentTypeException("MediaTypeUtils_CannotDetermineFormatFromContentType", TestMediaTypeUtils.GetSupportedMediaTypes(ODataPayloadKind.Batch), "multipart/bar")
                },
                new ContentTypeTestCase
                {
                    ContentType       = "foo/mixed",
                    ExpectedFormat    = ODataFormat.Batch,
                    ExpectedException = ODataExpectedExceptions.ODataContentTypeException("MediaTypeUtils_CannotDetermineFormatFromContentType", TestMediaTypeUtils.GetSupportedMediaTypes(ODataPayloadKind.Batch), "foo/mixed")
                },
                new ContentTypeTestCase
                {
                    ContentType       = "abc/pqr",
                    ExpectedFormat    = ODataFormat.Batch,
                    ExpectedException = ODataExpectedExceptions.ODataContentTypeException("MediaTypeUtils_CannotDetermineFormatFromContentType", TestMediaTypeUtils.GetSupportedMediaTypes(ODataPayloadKind.Batch), "abc/pqr")
                },
                new ContentTypeTestCase
                {
                    ContentType       = ApplicationJson,
                    ExpectedFormat    = ODataFormat.Batch,
                    ExpectedException = ODataExpectedExceptions.ODataContentTypeException("MediaTypeUtils_CannotDetermineFormatFromContentType", TestMediaTypeUtils.GetSupportedMediaTypes(ODataPayloadKind.Batch), ApplicationJson)
                }
            };

            this.CombinatorialEngineProvider.RunCombinations(
                testCases,
                this.ReaderTestConfigurationProvider.DefaultFormatConfigurations,
                (testCase, testConfiguration) =>
            {
                // create a message reader and call GetFormat; this should fail with the expected error message
                TestMessage testMessage = TestReaderUtils.CreateInputMessageFromStream(new TestStream(), testConfiguration);
                testMessage.SetHeader(Microsoft.OData.Core.ODataConstants.ContentTypeHeader, testCase.ContentType);

                TestExceptionUtils.ExpectedException(
                    this.Assert,
                    () =>
                {
                    using (ODataMessageReaderTestWrapper messageReader = TestReaderUtils.CreateMessageReader(testMessage, null, testConfiguration))
                    {
                        messageReader.CreateODataBatchReader();
                        ODataFormat actualFormat = ODataUtils.GetReadFormat(messageReader.MessageReader);
                        this.Assert.AreEqual(testCase.ExpectedFormat, actualFormat, "Formats don't match.");
                    }
                },
                    testCase.ExpectedException,
                    this.ExceptionVerifier);
            });
        }
        public void FeedTestJson()
        {
            var stream = new MemoryStream();
            var testMessage = new TestMessage() { Stream = stream };
            testMessage.SetHeader("Content-Type", "application/json");

            var converter = new SodaToODataConverter(testMessage, new Uri("http://fake"), new Uri("http://data.cityofchicago.org/views/z8bn-74gv"));
            var payload = new JsonPayload(JsonText);
            converter.ConvertFeed(new Uri("/SomethingOData", UriKind.Relative), new Uri("/SomethingSoda", UriKind.Relative), payload);

            var text = Encoding.UTF8.GetString(stream.ToArray());
            Console.WriteLine(text);
        }