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 (
  25. urlParamNotFoundFmt = "could not find url param %s"
  26. urlParamErrUintConvFmt = "could not convert url parameter %s to uint, got %s"
  27. )
  28. var getURLUintParamErrTests = []getURLParamErrTest{
  29. {
  30. getURLParamTest: getURLParamTest{
  31. description: "should fail when not found",
  32. route: "/api",
  33. routeParams: map[string]string{},
  34. paramReq: "project_id",
  35. },
  36. expErrStr: fmt.Sprintf(urlParamNotFoundFmt, "project_id"),
  37. },
  38. {
  39. getURLParamTest: getURLParamTest{
  40. description: "should fail when not uint",
  41. route: "/api/notuint",
  42. routeParams: map[string]string{
  43. "project_id": "notuint",
  44. },
  45. paramReq: "project_id",
  46. },
  47. expErrStr: fmt.Sprintf(urlParamErrUintConvFmt, "project_id", "notuint"),
  48. },
  49. }
  50. func TestGetURLUintParamsErrors(t *testing.T) {
  51. for _, test := range getURLUintParamErrTests {
  52. r := httptest.NewRequest("POST", test.route, nil)
  53. // set the context for testing
  54. r = apitest.WithURLParams(t, r, test.routeParams)
  55. _, err := requestutils.GetURLParamUint(r, types.URLParam(test.paramReq))
  56. if err == nil {
  57. t.Fatalf("[ %s ] did not return an error when error was expected", test.description)
  58. }
  59. assert.EqualError(t, err, test.expErrStr)
  60. var expErrTarget apierrors.RequestError
  61. assert.ErrorAs(t, err, &expErrTarget)
  62. }
  63. }
  64. type getURLParamStringTest struct {
  65. getURLParamTest
  66. expStr string
  67. }
  68. func TestGetURLParamString(t *testing.T) {
  69. r := httptest.NewRequest("POST", "/api/test", nil)
  70. // set the context for testing
  71. rctx := chi.NewRouteContext()
  72. routeParams := &chi.RouteParams{}
  73. routeParams.Add("name", "test")
  74. rctx.URLParams = *routeParams
  75. r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
  76. res, err := requestutils.GetURLParamString(r, "name")
  77. if err != nil {
  78. t.Fatalf("[ GetURLParamString ] returneed an error when no error was expected, %v", err.Error())
  79. }
  80. assert.Equal(t, "test", res)
  81. }
  82. func TestGetURLParamUint(t *testing.T) {
  83. r := httptest.NewRequest("POST", "/api/1", nil)
  84. // set the context for testing
  85. rctx := chi.NewRouteContext()
  86. routeParams := &chi.RouteParams{}
  87. routeParams.Add("name", "1")
  88. rctx.URLParams = *routeParams
  89. r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
  90. res, err := requestutils.GetURLParamUint(r, "name")
  91. if err != nil {
  92. t.Fatalf("[ GetURLParamUint ] returneed an error when no error was expected, %v", err.Error())
  93. }
  94. assert.Equal(t, uint(1), res)
  95. }