github.com/zak-blake/goa@v1.4.1/design/apidsl/metadata_test.go (about) 1 package apidsl_test 2 3 import ( 4 . "github.com/goadesign/goa/design" 5 . "github.com/goadesign/goa/design/apidsl" 6 "github.com/goadesign/goa/dslengine" 7 . "github.com/onsi/ginkgo" 8 . "github.com/onsi/gomega" 9 ) 10 11 var _ = Describe("Metadata", func() { 12 var mtd *MediaTypeDefinition 13 var api *APIDefinition 14 var rd *ResourceDefinition 15 var metadataKey string 16 var metadataValue string 17 18 BeforeEach(func() { 19 dslengine.Reset() 20 }) 21 22 Context("with Metadata declaration", func() { 23 JustBeforeEach(func() { 24 api = API("Example API", func() { 25 Metadata(metadataKey, metadataValue) 26 BasicAuthSecurity("password") 27 }) 28 29 rd = Resource("Example Resource", func() { 30 Metadata(metadataKey, metadataValue) 31 Action("Example Action", func() { 32 Metadata(metadataKey, metadataValue) 33 Routing( 34 GET("/", func() { 35 Metadata(metadataKey, metadataValue) 36 }), 37 ) 38 Security("password", func() { 39 Metadata(metadataKey, metadataValue) 40 }) 41 }) 42 Response("Example Response", func() { 43 Metadata(metadataKey, metadataValue) 44 }) 45 }) 46 47 mtd = MediaType("Example MediaType", func() { 48 Metadata(metadataKey, metadataValue) 49 Attribute("Example Attribute", func() { 50 Metadata(metadataKey, metadataValue) 51 }) 52 }) 53 54 dslengine.Run() 55 }) 56 57 Context("with blank metadata string", func() { 58 BeforeEach(func() { 59 metadataKey = "" 60 metadataValue = "" 61 }) 62 63 It("has metadata", func() { 64 expected := dslengine.MetadataDefinition{"": {""}} 65 Ω(api.Metadata).To(Equal(expected)) 66 Ω(rd.Metadata).To(Equal(expected)) 67 Ω(rd.Actions["Example Action"].Metadata).To(Equal(expected)) 68 Ω(rd.Actions["Example Action"].Routes[0].Metadata).To(Equal(expected)) 69 Ω(rd.Actions["Example Action"].Security.Scheme.Metadata).To(Equal(expected)) 70 Ω(rd.Responses["Example Response"].Metadata).To(Equal(expected)) 71 Ω(mtd.Metadata).To(Equal(expected)) 72 73 var mtdAttribute *AttributeDefinition 74 mtdAttribute = mtd.Type.ToObject()["Example Attribute"] 75 Ω(mtdAttribute.Metadata).To(Equal(expected)) 76 }) 77 }) 78 Context("with valid metadata string", func() { 79 BeforeEach(func() { 80 metadataKey = "struct:tag:json" 81 metadataValue = "myName,omitempty" 82 }) 83 84 It("has metadata", func() { 85 expected := dslengine.MetadataDefinition{"struct:tag:json": {"myName,omitempty"}} 86 Ω(api.Metadata).To(Equal(expected)) 87 Ω(rd.Metadata).To(Equal(expected)) 88 Ω(rd.Actions["Example Action"].Metadata).To(Equal(expected)) 89 Ω(rd.Actions["Example Action"].Routes[0].Metadata).To(Equal(expected)) 90 Ω(rd.Actions["Example Action"].Security.Scheme.Metadata).To(Equal(expected)) 91 Ω(rd.Responses["Example Response"].Metadata).To(Equal(expected)) 92 Ω(mtd.Metadata).To(Equal(expected)) 93 94 var mtdAttribute *AttributeDefinition 95 mtdAttribute = mtd.Type.ToObject()["Example Attribute"] 96 Ω(mtdAttribute.Metadata).To(Equal(expected)) 97 }) 98 }) 99 Context("with unicode metadata string", func() { 100 BeforeEach(func() { 101 metadataKey = "abc123一二三" 102 metadataValue = "˜µ≤≈ç√" 103 }) 104 105 It("has metadata", func() { 106 expected := dslengine.MetadataDefinition{"abc123一二三": {"˜µ≤≈ç√"}} 107 Ω(api.Metadata).To(Equal(expected)) 108 Ω(rd.Metadata).To(Equal(expected)) 109 Ω(rd.Actions["Example Action"].Metadata).To(Equal(expected)) 110 Ω(rd.Actions["Example Action"].Routes[0].Metadata).To(Equal(expected)) 111 Ω(rd.Actions["Example Action"].Security.Scheme.Metadata).To(Equal(expected)) 112 Ω(rd.Responses["Example Response"].Metadata).To(Equal(expected)) 113 Ω(mtd.Metadata).To(Equal(expected)) 114 115 var mtdAttribute *AttributeDefinition 116 mtdAttribute = mtd.Type.ToObject()["Example Attribute"] 117 Ω(mtdAttribute.Metadata).To(Equal(expected)) 118 }) 119 }) 120 121 }) 122 123 Context("with no Metadata declaration", func() { 124 JustBeforeEach(func() { 125 api = API("Example API", func() {}) 126 127 rd = Resource("Example Resource", func() { 128 Action("Example Action", func() { 129 }) 130 Response("Example Response", func() { 131 }) 132 }) 133 134 mtd = MediaType("Example MediaType", func() { 135 Attribute("Example Attribute", func() { 136 }) 137 }) 138 139 dslengine.Run() 140 }) 141 It("has no metadata", func() { 142 Ω(api.Metadata).To(BeNil()) 143 Ω(rd.Metadata).To(BeNil()) 144 Ω(mtd.Metadata).To(BeNil()) 145 146 var mtdAttribute *AttributeDefinition 147 mtdAttribute = mtd.Type.ToObject()["Example Attribute"] 148 Ω(mtdAttribute.Metadata).To(BeNil()) 149 }) 150 }) 151 152 })