github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/api/uaa/internal/routing_test.go (about)

     1  package internal_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	. "code.cloudfoundry.org/cli/api/uaa/internal"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  )
    11  
    12  var _ = Describe("Routing", func() {
    13  	Describe("Route", func() {
    14  		var route Route
    15  
    16  		Describe("CreatePath", func() {
    17  			BeforeEach(func() {
    18  				route = Route{
    19  					Name:   "whatevz",
    20  					Method: "GET",
    21  					Path:   "/a/path/:param/with/:many_things/:many/in/:it",
    22  				}
    23  			})
    24  
    25  			It("should return a url with all :entries populated by the passed in hash", func() {
    26  				Expect(route.CreatePath(Params{
    27  					"param":       "1",
    28  					"many_things": "2",
    29  					"many":        "a space",
    30  					"it":          "4",
    31  				})).Should(Equal(`/a/path/1/with/2/a%20space/in/4`))
    32  			})
    33  
    34  			When("the hash is missing params", func() {
    35  				It("should error", func() {
    36  					_, err := route.CreatePath(Params{
    37  						"param": "1",
    38  						"many":  "2",
    39  						"it":    "4",
    40  					})
    41  					Expect(err).Should(HaveOccurred())
    42  				})
    43  			})
    44  
    45  			When("the hash has extra params", func() {
    46  				It("should totally not care", func() {
    47  					Expect(route.CreatePath(Params{
    48  						"param":       "1",
    49  						"many_things": "2",
    50  						"many":        "a space",
    51  						"it":          "4",
    52  						"donut":       "bacon",
    53  					})).Should(Equal(`/a/path/1/with/2/a%20space/in/4`))
    54  				})
    55  			})
    56  
    57  			Context("with a trailing slash", func() {
    58  				It("should work", func() {
    59  					route = Route{
    60  						Name:   "whatevz",
    61  						Method: "GET",
    62  						Path:   "/a/path/:param/",
    63  					}
    64  					Expect(route.CreatePath(Params{
    65  						"param": "1",
    66  					})).Should(Equal(`/a/path/1/`))
    67  				})
    68  			})
    69  		})
    70  	})
    71  
    72  	Describe("Router", func() {
    73  		var (
    74  			router    *Router
    75  			routes    []Route
    76  			resources map[string]string
    77  		)
    78  
    79  		JustBeforeEach(func() {
    80  			router = NewRouter(routes, resources)
    81  		})
    82  
    83  		Describe("CreateRequest", func() {
    84  			When("the route exists", func() {
    85  				var badRouteName, routeName string
    86  				BeforeEach(func() {
    87  					routeName = "banana"
    88  					badRouteName = "orange"
    89  
    90  					routes = []Route{
    91  						{Name: routeName, Resource: "exists", Path: "/very/good/:name", Method: http.MethodGet},
    92  						{Name: badRouteName, Resource: "fake-resource", Path: "/very/bad", Method: http.MethodGet},
    93  					}
    94  				})
    95  
    96  				When("the resource exists exists", func() {
    97  					BeforeEach(func() {
    98  						resources = map[string]string{
    99  							"exists": "https://foo.bar.baz/this/is",
   100  						}
   101  					})
   102  
   103  					It("returns a request", func() {
   104  						request, err := router.CreateRequest(routeName, Params{"name": "Henry the 8th"}, nil)
   105  						Expect(err).ToNot(HaveOccurred())
   106  						Expect(request.URL.String()).To(Equal("https://foo.bar.baz/this/is/very/good/Henry%2520the%25208th"))
   107  					})
   108  				})
   109  
   110  				When("the resource exists exists", func() {
   111  					It("returns an error", func() {
   112  						_, err := router.CreateRequest(badRouteName, nil, nil)
   113  						Expect(err).To(MatchError("No resource exists with the name fake-resource"))
   114  					})
   115  				})
   116  			})
   117  
   118  			When("the route does not exists exist", func() {
   119  				It("returns an error", func() {
   120  					_, err := router.CreateRequest("fake-route", nil, nil)
   121  					Expect(err).To(MatchError("No route exists with the name fake-route"))
   122  				})
   123  			})
   124  		})
   125  	})
   126  })