Ejemplo n.º 1
0
        public void Can_write_ValidateRequestAttribute()
        {
            var nativeTypes = appHost.AssertPlugin <NativeTypesFeature>();
            var gen         = nativeTypes.DefaultGenerator;
            var attr        = new ValidateRequestAttribute("HasRole('Accounts')")
            {
                ErrorCode = "ExCode",
                Message   = "'Id' Is Required",
            };
            var metaAttr = gen.ToAttribute(attr);

            string argValue(string name) => metaAttr.Args.First(x => x.Name == name).Value;

            Assert.That(metaAttr.Name, Is.EqualTo("ValidateRequest"));
            Assert.That(metaAttr.Args.Count, Is.EqualTo(3));
            Assert.That(argValue(nameof(ValidateRequestAttribute.Validator)), Is.EqualTo("HasRole('Accounts')"));
            Assert.That(argValue(nameof(ValidateRequestAttribute.ErrorCode)), Is.EqualTo("ExCode"));
            Assert.That(argValue(nameof(ValidateRequestAttribute.Message)), Is.EqualTo("'Id' Is Required"));

            var csharp = new CSharpGenerator(new MetadataTypesConfig {
                DefaultNamespaces = new List <string> {
                    "ServiceStack"
                }
            });
            var src = csharp.GetCode(new MetadataTypes {
                Types = new List <MetadataType> {
                    new MetadataType {
                        Name       = "TheType",
                        Attributes = new List <MetadataAttribute> {
                            metaAttr,
                        }
                    }
                }
            }, new BasicRequest(), appHost.TryResolve <INativeTypesMetadata>());

            src.Print();

            Assert.That(src, Does.Contain(
                            "[ValidateRequest(\"HasRole('Accounts')\", ErrorCode=\"ExCode\", Message=\"'Id' Is Required\")]"));
        }