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