コード例 #1
0
        public void PlainSerializersBasicTest()
        {
            CombinatorialEngine engine = CombinatorialEngine.FromDimensions(
                new Dimension("ValidMime", new object[] { true, false }),
                new Dimension("SpecificMime", new object[] { true, false }),
                new Dimension("TypeData", TypeData.Values));
            Hashtable table = new Hashtable();
            while (engine.Next(table))
            {
                bool validMime = (bool)table["ValidMime"];
                bool specificMime = (bool)table["SpecificMime"];
                TypeData typeData = (TypeData)table["TypeData"];

                // If ValidMime is false, whether it's specific or not doesn't matter.
                if (!validMime && specificMime)
                {
                    continue;
                }

                if (!typeData.IsTypeSupported)
                {
                    continue;
                }

                using (TestWebRequest request = TestWebRequest.CreateForLocation(WebServerLocation.InProcess))
                {
                    Type valueType = typeData.ClrType;
                    Type entityType = typeof(TypedEntity<,>).MakeGenericType(typeof(int), valueType);
                    CustomDataContextSetup dataContextSetup = new CustomDataContextSetup(entityType);
                    dataContextSetup.Id = 1;
                    dataContextSetup.MemberValue = typeData.NonNullValue;

                    Type serviceType = dataContextSetup.DataServiceType;
                    request.DataServiceType = serviceType;
                    request.RequestUriString = "/Values(1)/Member/$value";
                    if (validMime)
                    {
                        request.Accept = TypeData.FindForType(valueType).DefaultContentType;
                    }
                    else
                    {
                        request.Accept = "application/unexpected";
                    }

                    try
                    {
                        request.SendRequest();
                        if (!validMime)
                        {
                            Assert.Fail("Request should have failed.");
                        }
                    }
                    catch (WebException)
                    {
                        if (!validMime)
                        {
                            continue;
                        }
                        throw;
                    }

                    string expectedType = request.Accept;
                    Assert.AreEqual(expectedType, TestUtil.GetMediaType(request.ResponseContentType));
                    
                    Stream stream = request.GetResponseStream();
                    if (valueType == typeof(byte[]))
                    {
                        byte[] bytes = (byte[])dataContextSetup.MemberValue;
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            Assert.AreEqual(bytes[i], (byte)stream.ReadByte());
                        }
                    }
                    else
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            typeData.VerifyAreEqual(dataContextSetup.MemberValue, typeData.ValueFromXmlText(reader.ReadToEnd(), request.Accept), request.Accept);
                        }
                    }
                }
            }
        }
コード例 #2
0
        public void RequestUriNamedKeyTest()
        {
            CombinatorialEngine engine = CombinatorialEngine.FromDimensions(
                new Dimension("TypeData", TypeData.Values),
                new Dimension("UseSmallCasing", new bool[] { true, false }),
                new Dimension("UseDoublePostfix", new bool[] { true, false }));

            bool syntaxErrorTested = false;
            TestUtil.RunCombinatorialEngineFail(engine, delegate(Hashtable values)
            {
                TypeData typeData = (TypeData)values["TypeData"];
                if (!typeData.IsTypeSupportedAsKey)
                {
                    return;
                }

                // TODO: when System.Uri handles '/' correctly, re-enable.
                if (typeData.ClrType == typeof(System.Xml.Linq.XElement))
                {
                    return;
                }

                Type entityType = typeof(DoubleKeyTypedEntity<,,>).MakeGenericType(typeData.ClrType, typeData.ClrType, typeof(int));
                CustomDataContextSetup setup = new CustomDataContextSetup(entityType);
                object idValue = typeData.NonNullValue;
                if (idValue is byte[])
                {
                    // idValue = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
                    return;
                }

                bool useSmallCasing = (bool)values["UseSmallCasing"];
                bool useDoublePostFix = (bool)values["UseDoublePostfix"];

                if (!(idValue is double) && useDoublePostFix)
                {
                    return;
                }

                string valueAsString = TypeData.FormatForKey(idValue, useSmallCasing, useDoublePostFix);

                TestUtil.ClearMetadataCache();
                using (TestWebRequest request = TestWebRequest.CreateForLocation(WebServerLocation.InProcess))
                {
                    Trace.WriteLine("Running with value: [" + valueAsString + "] for [" + typeData.ToString() + "]");
                    setup.Id = idValue;
                    setup.SecondId = idValue;
                    setup.MemberValue = 1;

                    request.DataServiceType = setup.DataServiceType;
                    request.Accept = "application/atom+xml,application/xml";
                    request.RequestUriString = "/Values(FirstKey=" + valueAsString + ",SecondKey=" + valueAsString + ")";
                    request.SendRequest();
                    string response = request.GetResponseStreamAsText();
                    TestUtil.AssertContains(response, "/Values(FirstKey=");
                    TestUtil.AssertContains(response, ",SecondKey=");
                    if (!syntaxErrorTested)
                    {
                        syntaxErrorTested = true;
                        VerifyRequestSyntaxError(request,
                            "/Values(" + valueAsString + "," + valueAsString + ")");
                        VerifyRequestSyntaxError(request,
                            "/Values(SecondKey == " + valueAsString + " , FirstKey = " + valueAsString + " )");
                        VerifyRequestSyntaxError(request,
                            "/Values(ASecondKey = " + valueAsString + " , FirstKey = " + valueAsString + " )");
                        VerifyRequestSyntaxError(request,
                            "/Values(SecondKey = " + valueAsString + ")");
                        VerifyRequestSyntaxError(request,
                            "/Values(SecondKey = " + valueAsString + ",,FirstKey=" + valueAsString + ")");
                        VerifyRequestSyntaxError(request,
                            "/Values(SecondKey)");
                        VerifyRequestSyntaxError(request,
                            "/Values(SecondKey=,FirstKey=)");
                        VerifyRequestSyntaxError(request,
                            "/Values(SecondKey = " + valueAsString + ",FirstKey=" + valueAsString +
                            ",ThirdKey=" + valueAsString + ")");
                    }
                }
                setup.Cleanup();
            });
        }