github.com/brycereitano/goa@v0.0.0-20170315073847-8ffa6c85e265/design/api_test.go (about)

     1  package design_test
     2  
     3  import (
     4  	"github.com/goadesign/goa/design"
     5  	"github.com/goadesign/goa/dslengine"
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  )
     9  
    10  var _ = Describe("CanonicalIdentifier", func() {
    11  	var id string
    12  	var canonical string
    13  
    14  	JustBeforeEach(func() {
    15  		canonical = design.CanonicalIdentifier(id)
    16  	})
    17  
    18  	Context("with a canonical identifier", func() {
    19  		BeforeEach(func() {
    20  			id = "application/json"
    21  		})
    22  
    23  		It("returns it", func() {
    24  			Ω(canonical).Should(Equal(id))
    25  		})
    26  	})
    27  
    28  	Context("with a non canonical identifier", func() {
    29  		BeforeEach(func() {
    30  			id = "application/json+xml; foo=bar"
    31  		})
    32  
    33  		It("canonicalizes it", func() {
    34  			Ω(canonical).Should(Equal("application/json; foo=bar"))
    35  		})
    36  	})
    37  })
    38  
    39  var _ = Describe("ExtractWildcards", func() {
    40  	var path string
    41  	var wcs []string
    42  
    43  	JustBeforeEach(func() {
    44  		wcs = design.ExtractWildcards(path)
    45  	})
    46  
    47  	Context("with a path with no wildcard", func() {
    48  		BeforeEach(func() {
    49  			path = "/foo"
    50  		})
    51  
    52  		It("returns the empty slice", func() {
    53  			Ω(wcs).Should(HaveLen(0))
    54  		})
    55  	})
    56  
    57  	Context("with a path with wildcards", func() {
    58  		BeforeEach(func() {
    59  			path = "/a/:foo/:bar/b/:baz/c"
    60  		})
    61  
    62  		It("extracts them", func() {
    63  			Ω(wcs).Should(Equal([]string{"foo", "bar", "baz"}))
    64  		})
    65  	})
    66  })
    67  
    68  var _ = Describe("MediaTypeRoot", func() {
    69  	var root design.MediaTypeRoot
    70  
    71  	BeforeEach(func() {
    72  		design.Design.MediaTypes = make(map[string]*design.MediaTypeDefinition)
    73  		root = design.MediaTypeRoot{}
    74  	})
    75  
    76  	It("has a non empty DSL name", func() {
    77  		Ω(root.DSLName()).ShouldNot(BeEmpty())
    78  	})
    79  
    80  	It("depends on the goa API design root", func() {
    81  		Ω(root.DependsOn()).Should(Equal([]dslengine.Root{design.Design}))
    82  	})
    83  
    84  	It("iterates over the generated media types when it's empty", func() {
    85  		var sets []dslengine.DefinitionSet
    86  		it := func(s dslengine.DefinitionSet) error {
    87  			sets = append(sets, s)
    88  			return nil
    89  		}
    90  		root.IterateSets(it)
    91  		Ω(sets).Should(HaveLen(1))
    92  		Ω(sets[0]).Should(BeEmpty())
    93  	})
    94  
    95  	It("iterates over the generated media types", func() {
    96  		var sets []dslengine.DefinitionSet
    97  		it := func(s dslengine.DefinitionSet) error {
    98  			sets = append(sets, s)
    99  			return nil
   100  		}
   101  		root["foo"] = &design.MediaTypeDefinition{Identifier: "application/json"}
   102  		root.IterateSets(it)
   103  		Ω(sets).Should(HaveLen(1))
   104  		Ω(sets[0]).Should(HaveLen(1))
   105  		Ω(sets[0][0]).Should(Equal(root["foo"]))
   106  	})
   107  
   108  	It("iterates over the generated media types in order", func() {
   109  		var sets []dslengine.DefinitionSet
   110  		it := func(s dslengine.DefinitionSet) error {
   111  			sets = append(sets, s)
   112  			return nil
   113  		}
   114  		root["foo"] = &design.MediaTypeDefinition{Identifier: "application/json"}
   115  		root["bar"] = &design.MediaTypeDefinition{Identifier: "application/xml"}
   116  		root.IterateSets(it)
   117  		Ω(sets).Should(HaveLen(1))
   118  		Ω(sets[0]).Should(HaveLen(2))
   119  		Ω(sets[0][0]).Should(Equal(root["foo"]))
   120  		Ω(sets[0][1]).Should(Equal(root["bar"]))
   121  	})
   122  })