public void MessageReaderConstructorArgumentValidationTest()
        {
            TestRequestMessage requestMessage = new TestRequestMessage(new MemoryStream());
            TestResponseMessage responseMessage = new TestResponseMessage(new MemoryStream());

            this.CombinatorialEngineProvider.RunCombinations(
                settingsActionTestCases,
                (settingsAction) =>
                {
                    TestMessage message = settingsAction.Response 
                        ? (TestMessage)responseMessage 
                        : (TestMessage)requestMessage;

                    // Verify that relative BaseUri will fail
                    this.Assert.ExpectedException(
                        () => settingsAction.Action(message, new ODataMessageReaderSettings { BaseUri = new Uri("foo", UriKind.Relative) }),
                        ODataExpectedExceptions.ODataException("ReaderValidationUtils_MessageReaderSettingsBaseUriMustBeNullOrAbsolute", "foo/"),
                        this.ExceptionVerifier);

                    // Verify the None UndeclaredPropertyBehaviorKinds works on both request and response.
                    this.Assert.ExpectedException(
                        () => settingsAction.Action(message, new ODataMessageReaderSettings { UndeclaredPropertyBehaviorKinds = ODataUndeclaredPropertyBehaviorKinds.None }),
                        null,
                        this.ExceptionVerifier);

                    // Verify the IgnoreUndeclaredValueProperty UndeclaredPropertyBehaviorKinds fails on requests.
                    this.Assert.ExpectedException(
                        () => settingsAction.Action(message, new ODataMessageReaderSettings { UndeclaredPropertyBehaviorKinds = ODataUndeclaredPropertyBehaviorKinds.IgnoreUndeclaredValueProperty }),
                        settingsAction.Response ? null : ODataExpectedExceptions.ODataException("ReaderValidationUtils_UndeclaredPropertyBehaviorKindSpecifiedOnRequest"),
                        this.ExceptionVerifier);

                    // Verify the ReportUndeclaredLinkProperty UndeclaredPropertyBehaviorKinds fails on requests.
                    this.Assert.ExpectedException(
                        () => settingsAction.Action(message, new ODataMessageReaderSettings { UndeclaredPropertyBehaviorKinds = ODataUndeclaredPropertyBehaviorKinds.ReportUndeclaredLinkProperty }),
                        settingsAction.Response ? null : ODataExpectedExceptions.ODataException("ReaderValidationUtils_UndeclaredPropertyBehaviorKindSpecifiedOnRequest"),
                        this.ExceptionVerifier);

                    // Verify the IgnoreUndeclaredValueProperty | ReportUndeclaredLinkProperty UndeclaredPropertyBehaviorKinds fails on requests.
                    this.Assert.ExpectedException(
                        () => settingsAction.Action(message, new ODataMessageReaderSettings { UndeclaredPropertyBehaviorKinds = ODataUndeclaredPropertyBehaviorKinds.IgnoreUndeclaredValueProperty | ODataUndeclaredPropertyBehaviorKinds.ReportUndeclaredLinkProperty }),
                        settingsAction.Response ? null : ODataExpectedExceptions.ODataException("ReaderValidationUtils_UndeclaredPropertyBehaviorKindSpecifiedOnRequest"),
                        this.ExceptionVerifier);
                });
        }
Beispiel #2
0
        /// <summary>
        /// Helper method to create a test message from its content.
        /// </summary>
        /// <param name="messageContent">Stream with the content of the message.</param>
        /// <param name="testConfiguration">The test configuration.</param>
        /// <param name="payloadKind">The payload kind to use to compute and set the content type header; or null if no content type header should be set.</param>
        /// <param name="customContentTypeHeader">A custom content type header to be used in the message.</param>
        /// <param name="urlResolver">Url resolver to add to the test message created.</param>
        /// <returns>Newly created test message.</returns>
        public static TestMessage CreateInputMessageFromStream(
            TestStream messageContent, 
            ReaderTestConfiguration testConfiguration,
            ODataPayloadKind? payloadKind,
            string customContentTypeHeader,
            IODataUrlResolver urlResolver)
        {
            TestMessage message;
            if (testConfiguration.IsRequest)
            {
                if (urlResolver != null)
                {
                    message = new TestRequestMessageWithUrlResolver(messageContent, urlResolver, testConfiguration.Synchronous ? TestMessageFlags.NoAsynchronous : TestMessageFlags.NoSynchronous);
                }
                else
                {
                    message = new TestRequestMessage(messageContent, testConfiguration.Synchronous ? TestMessageFlags.NoAsynchronous : TestMessageFlags.NoSynchronous);
                }
            }
            else
            {
                if (urlResolver != null)
                {
                    message = new TestResponseMessageWithUrlResolver(messageContent, urlResolver, testConfiguration.Synchronous ? TestMessageFlags.NoAsynchronous : TestMessageFlags.NoSynchronous);
                }
                else
                {
                    message = new TestResponseMessage(messageContent, testConfiguration.Synchronous ? TestMessageFlags.NoAsynchronous : TestMessageFlags.NoSynchronous);
                }
            }

            // set the OData-Version header
            message.SetHeader(ODataConstants.ODataVersionHeader, testConfiguration.Version.ToText());

            if (customContentTypeHeader != null)
            {
                message.SetHeader(ODataConstants.ContentTypeHeader, customContentTypeHeader);
            }
            else if (payloadKind.HasValue)
            {
                message.SetContentType(testConfiguration.Format, payloadKind.Value);
            }

            return message;
        }
        public void ServiceDocumentNoWhitespaceTest()
        {
            var testCases = new[]
            {
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                        "<collection href='http://odata.org/Products'>" +
                            "<atom:title>Products</atom:title>" +
                        "</collection>" +
                        "<collection href='http://odata.org/Orders'>" +
                            "<atom:title>Orders</atom:title>" +
                        "</collection>" +
                    "</workspace>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                        "<collection href='http://odata.org/Products'>" +
                            "<atom:title>Products</atom:title>" +
                        "</collection>" +
                    "</workspace>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                        "<collection href='http://odata.org/Products'>" +
                        "</collection>" +
                        "<collection href='http://odata.org/Orders'>" +
                        "</collection>" +
                    "</workspace>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                        "<collection href='http://odata.org/Products'>" +
                        "</collection>" +
                    "</workspace>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                        "<collection href='http://odata.org/Products'/>" +
                        "<collection href='http://odata.org/Orders'/>" +
                    "</workspace>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                        "<collection href='http://odata.org/Products'/>" +
                    "</workspace>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                    "</workspace>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace/>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                        "<collection href='http://odata.org/Products'>" +
                            "<atom:title>Products</atom:title>" +
                            "<accept>image/png</accept>" +
                            "<categories href='http://somecategories.com'/>" +
                        "</collection>" +
                    "</workspace>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                        "<collection href='http://odata.org/Products'>" +
                            "<accept>image/png</accept>" +
                            "<categories href='http://somecategories.com'/>" +
                            "<atom:title>Products</atom:title>" +
                        "</collection>" +
                    "</workspace>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                        "<collection href='http://odata.org/Products'>" +
                            "<accept>image/png</accept>" +
                            "<categories href='http://somecategories.com'/>" +
                            "<atom:title>Products</atom:title>" +
                        "</collection>" +
                        "<collection href='http://odata.org/Orders'>" +
                            "<accept>image/png</accept>" +
                            "<categories href='http://somecategories.com'/>" +
                            "<atom:title>Orders</atom:title>" +
                        "</collection>" +
                    "</workspace>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                        "<collection href='http://odata.org/Products'>" +
                            "<cf:cf xmlns:cf='uri'/>" +
                        "</collection>" +
                        "<collection href='http://odata.org/Orders'>" +
                            "<cf:cf xmlns:cf='uri'/>" +
                        "</collection>" +
                    "</workspace>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                        "<collection href='http://odata.org/Products'>" +
                            "<cf:cf xmlns:cf='uri'/>" +
                        "</collection>" +
                    "</workspace>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                        "<collection href='http://odata.org/Products'>" +
                            "<cf:cf xmlns:cf='uri'/>" +
                        "</collection>" +
                        "<cf:cf xmlns:cf='uri'/>" +
                        "<collection href='http://odata.org/Orders'>" +
                            "<cf:cf xmlns:cf='uri'/>" +
                        "</collection>" +
                        "<cf:cf xmlns:cf='uri'/>" +
                    "</workspace>" +
                "</service>",
                "<service xmlns:app='http://www.w3.org/2007/app' " +
                     "xmlns='http://www.w3.org/2007/app' xmlns:atom='http://www.w3.org/2005/Atom'>" +
                     "<workspace>" +
                        "<cf:cf xmlns:cf='uri'/>" +
                    "</workspace>" +
                "</service>",
            };

            this.CombinatorialEngineProvider.RunCombinations(
                testCases,
                testCase =>
                {
                    // Just validate that the input strings above don't include any whitespaces.
                    using (XmlReader xmlReader = XmlReader.Create(new StringReader(testCase)))
                    {
                        while (xmlReader.Read())
                        {
                            this.Assert.AreNotEqual(XmlNodeType.Whitespace, xmlReader.NodeType, "No whitespaces please");
                            this.Assert.AreNotEqual(XmlNodeType.SignificantWhitespace, xmlReader.NodeType, "No whitespaces please");
                        }
                    }

                    // Run the payload through the reader and make sure it doesn't fail.
                    TestResponseMessage message = new TestResponseMessage(new MemoryStream(Encoding.UTF8.GetBytes(testCase)));
                    message.SetContentType(ODataFormat.Atom, ODataPayloadKind.ServiceDocument);
                    ODataMessageReaderSettings settings = new ODataMessageReaderSettings { EnableAtom = true };

                    using (ODataMessageReader messageReader = new ODataMessageReader(message, settings))
                    {
                        messageReader.ReadServiceDocument();
                    }
                });
        }
        public void PublicSetOnProperties()
        {
            var requestMessage = new TestRequestMessage(new MemoryStream());
            var exception = TestExceptionUtils.RunCatching(() => requestMessage.Url = new Uri("http://www.odata.org"));
            ExceptionUtilities.Assert(exception == null, "No exception was expected. Exception was thrown {0}", exception);

            var responseMessage= new TestResponseMessage(new MemoryStream());
            exception = TestExceptionUtils.RunCatching(() => responseMessage.StatusCode = 5);
            ExceptionUtilities.Assert(exception == null, "No exception was expected. Exception was thrown {0}", exception);
        }
 /// <summary>
 /// Creates an <see cref="ODataMessageReader"/> from the specified <paramref name="memoryStream"/>.
 /// </summary>
 /// <param name="memoryStream">The <see cref="MemoryStream"/> used as input for the message reader.</param>
 /// <returns>An <see cref="ODataMessageReader"/> instance.</returns>
 private ODataMessageReader CreateMessageReader(MemoryStream memoryStream)
 {
     // Create a test message around the memory stream, set the content-type header and then create
     // the message reader
     if (this.IsResponse)
     {
         TestResponseMessage responseMessage = new TestResponseMessage(memoryStream, TestMessageFlags.NoAsynchronous);
         responseMessage.SetHeader(ODataConstants.ContentTypeHeader, "multipart/mixed; boundary=" + this.BatchBoundary);
         return new ODataMessageReader(responseMessage);
     }
     else
     {
         TestRequestMessage requestMessage = new TestRequestMessage(memoryStream, TestMessageFlags.NoAsynchronous);
         requestMessage.SetHeader(ODataConstants.ContentTypeHeader, "multipart/mixed; boundary=" + this.BatchBoundary);
         return new ODataMessageReader(requestMessage);
     }
 }