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

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