Ejemplo n.º 1
0
        private static IEnumerable<ContentTypeMessageHeaderTestCase> CreateContentTypeHeaderTestCases(ODataPayloadKind payloadKind)
        {

            IEnumerable<ContentTypeMessageHeaderTestCase> testCases;
            switch (payloadKind)
            {
                case ODataPayloadKind.Feed:
                case ODataPayloadKind.EntityReferenceLinks:
                    testCases = new ContentTypeMessageHeaderTestCase[]
                    {
                        new ContentTypeMessageHeaderTestCase
                        {
                            ContentTypeHeaderValue = "application/atom+xml;type=feed",
                            ExpectedEncoding = null,
                            ExpectedFormat = ODataFormat.Atom
                        },
                        new ContentTypeMessageHeaderTestCase
                        {
                            ContentTypeHeaderValue = "application/atom+xml",
                            ExpectedEncoding = null,
                            ExpectedFormat = ODataFormat.Atom
                        },
                        AppJsonLightTestCase,
                    };

                    break;
                case ODataPayloadKind.Entry:
                    testCases = new ContentTypeMessageHeaderTestCase[]
                    {
                        new ContentTypeMessageHeaderTestCase
                        {
                            ContentTypeHeaderValue = "application/atom+xml;type=entry",
                            ExpectedEncoding = null,
                            ExpectedFormat = ODataFormat.Atom
                        },
                        new ContentTypeMessageHeaderTestCase
                        {
                            ContentTypeHeaderValue = "application/atom+xml",
                            ExpectedEncoding = null,
                            ExpectedFormat = ODataFormat.Atom
                        },
                        AppJsonLightTestCase,
                    };

                    break;
                case ODataPayloadKind.Property:
                    testCases = new ContentTypeMessageHeaderTestCase[]
                    {
                        AppXmlTestCase,
                        TextXmlTestCase,
                        AppJsonLightTestCase,
                    };

                    break;
                case ODataPayloadKind.EntityReferenceLink:
                    testCases = new ContentTypeMessageHeaderTestCase[]
                    {
                        AppXmlTestCase,
                        TextXmlTestCase,
                        AppJsonLightTestCase,
                    };

                    break;
                case ODataPayloadKind.Value:
                    testCases = new ContentTypeMessageHeaderTestCase[]
                    {
                        new ContentTypeMessageHeaderTestCase
                        {
                            ContentTypeHeaderValue = "text/plain",
                            ExpectedEncoding = TestMediaTypeUtils.Latin1Encoding,
                            ExpectedFormat = ODataFormat.RawValue
                        },
                    };

                    break;
                case ODataPayloadKind.BinaryValue:
                    testCases = new ContentTypeMessageHeaderTestCase[]
                    {
                        new ContentTypeMessageHeaderTestCase
                        {
                            ContentTypeHeaderValue = "application/octet-stream",
                            ExpectedEncoding = null,
                            ExpectedFormat = ODataFormat.RawValue
                        },
                    };

                    break;
                case ODataPayloadKind.Collection:
                    testCases = new ContentTypeMessageHeaderTestCase[]
                    {
                        AppXmlTestCase,
                        TextXmlTestCase,
                        AppJsonLightTestCase,
                    };

                    break;
                case ODataPayloadKind.ServiceDocument:
                    testCases = new ContentTypeMessageHeaderTestCase[]
                    {
                        new ContentTypeMessageHeaderTestCase
                        {
                            ContentTypeHeaderValue = "application/atomsvc+xml",
                            ExpectedEncoding = null,
                            ExpectedFormat = ODataFormat.Atom
                        },
                        AppJsonLightTestCase,
                    };

                    break;
                case ODataPayloadKind.MetadataDocument:
                    testCases = new ContentTypeMessageHeaderTestCase[]
                    {
                        new ContentTypeMessageHeaderTestCase
                        {
                            ContentTypeHeaderValue = "application/xml",
                            ExpectedEncoding = null,
                            ExpectedFormat = ODataFormat.Metadata
                        },
                    };

                    break;
                case ODataPayloadKind.Error:
                    testCases = new ContentTypeMessageHeaderTestCase[]
                    {
                        AppXmlTestCase,
                        AppJsonLightTestCase,
                    };

                    break;
                case ODataPayloadKind.Batch:
                    testCases = new ContentTypeMessageHeaderTestCase[]
                    {
                        new ContentTypeMessageHeaderTestCase
                        {
                            ContentTypeHeaderValue = "multipart/mixed;boundary=MyBoundaryString",
                            ExpectedEncoding = null,
                            ExpectedFormat = ODataFormat.Batch
                        },
                    };
                    break;
                case ODataPayloadKind.Unsupported:
                default:
                    return Enumerable.Empty<ContentTypeMessageHeaderTestCase>();
            }

            // Add some other parameters that should not affect us
            string[] additionalParameters = new string[] { ";", ";a=b", ";a=b;c=d", ";a=b;q=0.5" };
            var extraParameterTestCases = testCases.SelectMany(
                testCase => additionalParameters.Select(
                    additionalParameter => new ContentTypeMessageHeaderTestCase(testCase)
                    {
                        ContentTypeHeaderValue = testCase.ContentTypeHeaderValue + additionalParameter
                    }));

            // Add some test cases that change the encoding
            var customEncodings = new[]
                {
                    new { CharSet = ";charset=iso-8859-1", Encoding = TestMediaTypeUtils.Latin1Encoding },
#if !SILVERLIGHT && !WINDOWS_PHONE   // Encoding not supported on these platforms
                    new { CharSet = ";charset=ibm850", Encoding = TestMediaTypeUtils.Ibm850Encoding}
#endif
                };
            var encodingTestCases = testCases.SelectMany(
                testCase => customEncodings.Select(
                    customEncoding => new ContentTypeMessageHeaderTestCase(testCase)
                    {
                        ContentTypeHeaderValue = testCase.ContentTypeHeaderValue + customEncoding.CharSet,
                        ExpectedEncoding = customEncoding.Encoding
                    }));

            // Add some error test cases
            var errorTestCases = new ContentTypeMessageHeaderTestCase[]
            {
                new ContentTypeMessageHeaderTestCase
                {
                    ContentTypeHeaderValue = "abc/pqr",
                    ExpectedException = tc => ODataExpectedExceptions.ODataContentTypeException(
                        "MediaTypeUtils_CannotDetermineFormatFromContentType", 
                        TestMediaTypeUtils.GetSupportedMediaTypes(payloadKind, /*includeAppJson*/true), 
                        "abc/pqr"),
                },
            };

            return testCases.Concat(extraParameterTestCases).Concat(encodingTestCases).Concat(errorTestCases);
        }
Ejemplo n.º 2
0
 public ContentTypeMessageHeaderTestCase(ContentTypeMessageHeaderTestCase other)
 {
     this.ContentTypeHeaderValue = other.ContentTypeHeaderValue;
     this.ExpectedException = other.ExpectedException;
     this.ExpectedFormat = other.ExpectedFormat;
     this.ExpectedEncoding = other.ExpectedEncoding;
 }