Exemplo n.º 1
0
        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));
                }
            });
        }
Exemplo n.º 2
0
        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);
                }
            });
        }