url_param_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package requestutils_test
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/go-chi/chi"
  8. "github.com/porter-dev/porter/api/server/shared/apierrors"
  9. "github.com/porter-dev/porter/api/server/shared/apitest"
  10. "github.com/porter-dev/porter/api/server/shared/requestutils"
  11. "github.com/porter-dev/porter/api/types"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. type getURLParamTest struct {
  15. description string
  16. route string
  17. routeParams map[string]string
  18. paramReq string
  19. }
  20. type getURLParamErrTest struct {
  21. getURLParamTest
  22. expErrStr string
  23. }
  24. const urlParamNotFoundFmt = "could not find url param %s"
  25. const urlParamErrUintConvFmt = "could not convert url parameter %s to uint, got %s"
  26. var getURLUintParamErrTests = []getURLParamErrTest{
  27. {
  28. getURLParamTest: getURLParamTest{
  29. description: "should fail when not found",
  30. route: "/api",
  31. routeParams: map[string]string{},
  32. paramReq: "project_id",
  33. },
  34. expErrStr: fmt.Sprintf(urlParamNotFoundFmt, "project_id"),
  35. },
  36. {
  37. getURLParamTest: getURLParamTest{
  38. description: "should fail when not uint",
  39. route: "/api/notuint",
  40. routeParams: map[string]string{
  41. "project_id": "notuint",
  42. },
  43. paramReq: "project_id",
  44. },
  45. expErrStr: fmt.Sprintf(urlParamErrUintConvFmt, "project_id", "notuint"),
  46. },
  47. }
  48. func TestGetURLUintParamsErrors(t *testing.T) {
  49. for _, test := range getURLUintParamErrTests {
  50. r := httptest.NewRequest("POST", test.route, nil)
  51. // set the context for testing
  52. r = apitest.WithURLParams(t, r, test.routeParams)
  53. _, err := requestutils.GetURLParamUint(r, types.URLParam(test.paramReq))
  54. if err == nil {
  55. t.Fatalf("[ %s ] did not return an error when error was expected", test.description)
  56. }
  57. assert.EqualError(t, err, test.expErrStr)
  58. var expErrTarget apierrors.RequestError
  59. assert.ErrorAs(t, err, &expErrTarget)
  60. }
  61. }
  62. type getURLParamStringTest struct {
  63. getURLParamTest
  64. expStr string
  65. }
  66. func TestGetURLParamString(t *testing.T) {
  67. r := httptest.NewRequest("POST", "/api/test", nil)
  68. // set the context for testing
  69. rctx := chi.NewRouteContext()
  70. routeParams := &chi.RouteParams{}
  71. routeParams.Add("name", "test")
  72. rctx.URLParams = *routeParams
  73. r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
  74. res, err := requestutils.GetURLParamString(r, "name")
  75. if err != nil {
  76. t.Fatalf("[ GetURLParamString ] returneed an error when no error was expected, %v", err.Error())
  77. }
  78. assert.Equal(t, "test", res)
  79. }
  80. func TestGetURLParamUint(t *testing.T) {
  81. r := httptest.NewRequest("POST", "/api/1", nil)
  82. // set the context for testing
  83. rctx := chi.NewRouteContext()
  84. routeParams := &chi.RouteParams{}
  85. routeParams.Add("name", "1")
  86. rctx.URLParams = *routeParams
  87. r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
  88. res, err := requestutils.GetURLParamUint(r, "name")
  89. if err != nil {
  90. t.Fatalf("[ GetURLParamUint ] returneed an error when no error was expected, %v", err.Error())
  91. }
  92. assert.Equal(t, uint(1), res)
  93. }