You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

binding_test.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package validation
  5. import (
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. "gitea.com/macaron/binding"
  10. "gitea.com/macaron/macaron"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. const (
  14. testRoute = "/test"
  15. )
  16. type (
  17. validationTestCase struct {
  18. description string
  19. data interface{}
  20. expectedErrors binding.Errors
  21. }
  22. TestForm struct {
  23. BranchName string `form:"BranchName" binding:"GitRefName"`
  24. URL string `form:"ValidUrl" binding:"ValidUrl"`
  25. GlobPattern string `form:"GlobPattern" binding:"GlobPattern"`
  26. }
  27. )
  28. func performValidationTest(t *testing.T, testCase validationTestCase) {
  29. httpRecorder := httptest.NewRecorder()
  30. m := macaron.Classic()
  31. m.Post(testRoute, binding.Validate(testCase.data), func(actual binding.Errors) {
  32. // see https://github.com/stretchr/testify/issues/435
  33. if actual == nil {
  34. actual = binding.Errors{}
  35. }
  36. assert.Equal(t, testCase.expectedErrors, actual)
  37. })
  38. req, err := http.NewRequest("POST", testRoute, nil)
  39. if err != nil {
  40. panic(err)
  41. }
  42. m.ServeHTTP(httpRecorder, req)
  43. switch httpRecorder.Code {
  44. case http.StatusNotFound:
  45. panic("Routing is messed up in test fixture (got 404): check methods and paths")
  46. case http.StatusInternalServerError:
  47. panic("Something bad happened on '" + testCase.description + "'")
  48. }
  49. }