public void SerializeResponseBodyAcceptTypeWithCharsetTest()
        {
            // Verifies that the correct handling of 'charset' parameters in accept header media types.
            TestServiceHost host = new TestServiceHost();

            host.RequestPathInfo = "/Customers(0)/Name/$value";
            DataService <CustomDataContext> context = new OpenWebDataService <CustomDataContext>();

            const string TextPlain = "text/plain";
            var          tests     = TestUtil.CreateDictionary <string>(
                TextPlain, TextPlain,
                // valid charset of accept header should not be used for conneg
                TextPlain + ";charset=UTF-8", TextPlain,
                // invalid charset of accept header should not be used for conneg
                TextPlain + ";charset=aabbcc", TextPlain,
                // non-charset parameter should be used for matching
                TextPlain + ";some=value", null,
                // charset and non-charset parameters; non-charset one should be used for matching
                TextPlain + ";charset=utf-8;some=value", null
                );

            TestUtil.RunCombinations(tests, (test) =>
            {
                host.ClearResponse();
                context.AttachHost(host);
                string acceptTypes  = test.Key;
                string result       = test.Value;
                Exception exception = TestUtil.RunCatching(delegate()
                {
                    Trace.WriteLine("Running query with accept type: " + acceptTypes);
                    host.RequestAccept = acceptTypes;
                    context.ProcessRequest();
                });
                TestUtil.AssertExceptionExpected(exception, result == null);
                if (result != null)
                {
                    Assert.AreEqual(result, TestUtil.GetMediaType(host.ResponseContentType));
                }
            });
        }
Beispiel #2
0
        public void DataServiceHostServiceRootUriTest()
        {
            // Edit link "../Product(1)" incorrectly produced when base uri is http://service.svc using IDSH
            // Make sure service root URIs always end in '/' when using an IDataServiceHost/IDataServiceHost2
            //
            // Verifies that the service root URI for a IDSH is always terminated in a '/'
            // NOTE: if the service URI would not be properly terminated in a '/', the created edit link in the resulting
            //       entry would be incorrect.
            Uri[] uris = new Uri[]
            {
                new Uri("http://host"),
                new Uri("http://host/a/b"),
                new Uri("http://host/a/b.svc"),
                new Uri("http://host/a/b/c.svc")
            };

            TestUtil.RunCombinations(uris, (uri) =>
            {
                TestServiceHost host = new TestServiceHost(uri)
                {
                    RequestAccept   = "application/atom+xml",
                    RequestPathInfo = "/Customers(0)",
                };

                DataService <CustomDataContext> context = new OpenWebDataService <CustomDataContext>();
                context.AttachHost(host);

                Exception exception = TestUtil.RunCatching(delegate()
                {
                    context.ProcessRequest();
                });

                XmlDocument document = new XmlDocument(TestUtil.TestNameTable);
                host.ResponseStream.Seek(0, SeekOrigin.Begin);
                document.Load(host.ResponseStream);
                UnitTestsUtil.VerifyXPaths(document, "/atom:entry/atom:link[@rel='edit' and @href='Customers(0)']");

                TestUtil.AssertExceptionExpected(exception, false);
            });
        }
        public void SerializeResponseBodyAcceptTypeTest()
        {
            // Verifies that the correct Content-Type is selected at serialization time.
            TestServiceHost host = new TestServiceHost();

            host.RequestPathInfo = "/Customers";
            DataService <CustomDataContext> context = new OpenWebDataService <CustomDataContext>();

            const string AtomXml     = "application/atom+xml";
            const string AtomXmlFeed = "application/atom+xml;type=feed";
            const string charsetUtf8 = ";charset=utf-8";
            const string JsonLight   = "application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false";

            var testCases = new[]
            {
                // Success cases for all versions
                new { AcceptHeaderString = "application/atom+xml, application/json", Expectation = JsonLight + charsetUtf8 },
                new { AcceptHeaderString = "application/atom+xml, application/json;odata.metadata=minimal", Expectation = JsonLight + charsetUtf8 },
                new { AcceptHeaderString = "application/json, application/atom+xml", Expectation = JsonLight + charsetUtf8 },
                new { AcceptHeaderString = (string)null, Expectation = JsonLight + charsetUtf8 },
                new { AcceptHeaderString = "", Expectation = JsonLight + charsetUtf8 },
                new { AcceptHeaderString = " ", Expectation = JsonLight + charsetUtf8 },
                new { AcceptHeaderString = "*/*", Expectation = JsonLight + charsetUtf8 },
                new { AcceptHeaderString = AtomXml, Expectation = AtomXmlFeed + charsetUtf8 },
                new { AcceptHeaderString = "application/json", Expectation = JsonLight + charsetUtf8 },
                new { AcceptHeaderString = "application/xml,application/json;q=0.8", Expectation = JsonLight + charsetUtf8 },
                new { AcceptHeaderString = AtomXml + ",application/json;q=0.8", Expectation = AtomXmlFeed + charsetUtf8 },
                new { AcceptHeaderString = "application/json," + AtomXml + ";q=0.8", Expectation = JsonLight + charsetUtf8 },
                new { AcceptHeaderString = "text/xml,*/*", Expectation = JsonLight + charsetUtf8 },
                new { AcceptHeaderString = "application/xml,*/*", Expectation = JsonLight + charsetUtf8 },

                // Error cases for all versions
                new { AcceptHeaderString = "text/xml", Expectation = (string)null },
                new { AcceptHeaderString = "application/xml", Expectation = (string)null },
                new { AcceptHeaderString = "application/json;foo=bar", Expectation = (string)null },
                new { AcceptHeaderString = "application/json;odata.metadata=bla", Expectation = (string)null },
                new { AcceptHeaderString = "application/json;foo=bar", Expectation = (string)null },

                // Cases where V2 and V3 expected behaviors differ
                new { AcceptHeaderString = "application/json", Expectation = JsonLight + charsetUtf8 },
                new { AcceptHeaderString = "application/json;odata.metadata=minimal", Expectation = JsonLight + charsetUtf8 },
                new { AcceptHeaderString = "application/json, application/json;odata.metadata=minimal", Expectation = JsonLight + charsetUtf8 },
            };

            TestUtil.RunCombinations(
                testCases,
                new string[] { "4.0" },
                (testCase, requestMaxVersion) =>
            {
                host.ClearResponse();
                context.AttachHost(host);
                string acceptTypes = testCase.AcceptHeaderString;
                string result      = testCase.Expectation;

                Exception exception = TestUtil.RunCatching(delegate()
                {
                    Trace.WriteLine("Running query with accept type: " + acceptTypes);
                    host.RequestAccept     = acceptTypes;
                    host.RequestMaxVersion = requestMaxVersion;
                    context.ProcessRequest();
                });
                TestUtil.AssertExceptionExpected(exception, result == null);
                if (result != null)
                {
                    Assert.AreEqual(result, host.ResponseContentType);
                }
            });
        }