github.com/NewMillennialGames/goa@v1.4.0/design/apidsl/response_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("Response", func() {
    12  	var name string
    13  	var dt DataType
    14  	var dsl func()
    15  
    16  	var res *ResponseDefinition
    17  
    18  	BeforeEach(func() {
    19  		dslengine.Reset()
    20  		name = ""
    21  		dsl = nil
    22  		dt = nil
    23  	})
    24  
    25  	JustBeforeEach(func() {
    26  		Resource("res", func() {
    27  			Action("action", func() {
    28  				if dt != nil {
    29  					Response(name, dt, dsl)
    30  				} else {
    31  					Response(name, dsl)
    32  				}
    33  			})
    34  		})
    35  		dslengine.Run()
    36  		if r, ok := Design.Resources["res"]; ok {
    37  			if a, ok := r.Actions["action"]; ok {
    38  				res = a.Responses[name]
    39  			}
    40  		}
    41  	})
    42  
    43  	Context("with no dsl and no name", func() {
    44  		It("produces an invalid action definition", func() {
    45  			Ω(res).ShouldNot(BeNil())
    46  			Ω(res.Validate()).Should(HaveOccurred())
    47  		})
    48  	})
    49  
    50  	Context("with no dsl", func() {
    51  		BeforeEach(func() {
    52  			name = "foo"
    53  		})
    54  
    55  		It("produces an invalid action definition", func() {
    56  			Ω(res).ShouldNot(BeNil())
    57  			Ω(res.Validate()).Should(HaveOccurred())
    58  		})
    59  	})
    60  
    61  	Context("with a status", func() {
    62  		const status = 201
    63  
    64  		BeforeEach(func() {
    65  			name = "foo"
    66  			dsl = func() {
    67  				Status(status)
    68  			}
    69  		})
    70  
    71  		It("produces a valid action definition and sets the status and parent", func() {
    72  			Ω(res).ShouldNot(BeNil())
    73  			Ω(res.Validate()).ShouldNot(HaveOccurred())
    74  			Ω(res.Status).Should(Equal(status))
    75  			Ω(res.Parent).ShouldNot(BeNil())
    76  		})
    77  	})
    78  
    79  	Context("with a type override", func() {
    80  		const status = 201
    81  
    82  		BeforeEach(func() {
    83  			name = "foo"
    84  			dsl = func() {
    85  				Status(status)
    86  			}
    87  			dt = HashOf(String, Any)
    88  		})
    89  
    90  		It("produces a response definition with the given type", func() {
    91  			Ω(res).ShouldNot(BeNil())
    92  			Ω(res.Type).Should(Equal(dt))
    93  			Ω(res.Validate()).ShouldNot(HaveOccurred())
    94  		})
    95  	})
    96  
    97  	Context("with a status and description", func() {
    98  		const status = 201
    99  		const description = "desc"
   100  
   101  		BeforeEach(func() {
   102  			name = "foo"
   103  			dsl = func() {
   104  				Status(status)
   105  				Description(description)
   106  			}
   107  		})
   108  
   109  		It("sets the status and description", func() {
   110  			Ω(res).ShouldNot(BeNil())
   111  			Ω(res.Validate()).ShouldNot(HaveOccurred())
   112  			Ω(res.Status).Should(Equal(status))
   113  			Ω(res.Description).Should(Equal(description))
   114  		})
   115  	})
   116  
   117  	Context("with a status and name override", func() {
   118  		const status = 201
   119  
   120  		BeforeEach(func() {
   121  			name = "foo"
   122  			dsl = func() {
   123  				Status(status)
   124  			}
   125  		})
   126  
   127  		It("sets the status and name", func() {
   128  			Ω(res).ShouldNot(BeNil())
   129  			Ω(res.Validate()).ShouldNot(HaveOccurred())
   130  			Ω(res.Status).Should(Equal(status))
   131  		})
   132  	})
   133  
   134  	Context("with a status and media type", func() {
   135  		const status = 201
   136  		const mediaType = "mt"
   137  
   138  		BeforeEach(func() {
   139  			name = "foo"
   140  			dsl = func() {
   141  				Status(status)
   142  				Media(mediaType)
   143  			}
   144  		})
   145  
   146  		It("sets the status and media type", func() {
   147  			Ω(res).ShouldNot(BeNil())
   148  			Ω(res.Validate()).ShouldNot(HaveOccurred())
   149  			Ω(res.Status).Should(Equal(status))
   150  			Ω(res.MediaType).Should(Equal(mediaType))
   151  		})
   152  	})
   153  
   154  	Context("with a status and headers", func() {
   155  		const status = 201
   156  		const headerName = "Location"
   157  
   158  		BeforeEach(func() {
   159  			name = "foo"
   160  			dsl = func() {
   161  				Status(status)
   162  				Headers(func() {
   163  					Header(headerName)
   164  				})
   165  			}
   166  		})
   167  
   168  		It("sets the status and headers", func() {
   169  			Ω(res).ShouldNot(BeNil())
   170  			Ω(res.Validate()).ShouldNot(HaveOccurred())
   171  			Ω(res.Status).Should(Equal(status))
   172  			Ω(res.Headers).ShouldNot(BeNil())
   173  			Ω(res.Headers.Type).Should(BeAssignableToTypeOf(Object{}))
   174  			o := res.Headers.Type.(Object)
   175  			Ω(o).Should(HaveLen(1))
   176  			Ω(o).Should(HaveKey(headerName))
   177  		})
   178  	})
   179  
   180  	Context("not from the goa default definitions", func() {
   181  		BeforeEach(func() {
   182  			name = "foo"
   183  		})
   184  
   185  		It("does not set the Standard flag", func() {
   186  			Ω(res.Standard).Should(BeFalse())
   187  		})
   188  	})
   189  
   190  	Context("from the goa default definitions", func() {
   191  		BeforeEach(func() {
   192  			name = "Created"
   193  		})
   194  
   195  		It("sets the Standard flag", func() {
   196  			Ω(res.Standard).Should(BeTrue())
   197  		})
   198  	})
   199  
   200  })