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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. "github.com/go-macaron/binding"
  10. "github.com/stretchr/testify/assert"
  11. "gopkg.in/macaron.v1"
  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. }
  26. )
  27. func performValidationTest(t *testing.T, testCase validationTestCase) {
  28. httpRecorder := httptest.NewRecorder()
  29. m := macaron.Classic()
  30. m.Post(testRoute, binding.Validate(testCase.data), func(actual binding.Errors) {
  31. // see https://github.com/stretchr/testify/issues/435
  32. if actual == nil {
  33. actual = binding.Errors{}
  34. }
  35. assert.Equal(t, testCase.expectedErrors, actual)
  36. })
  37. req, err := http.NewRequest("POST", testRoute, nil)
  38. if err != nil {
  39. panic(err)
  40. }
  41. m.ServeHTTP(httpRecorder, req)
  42. switch httpRecorder.Code {
  43. case http.StatusNotFound:
  44. panic("Routing is messed up in test fixture (got 404): check methods and paths")
  45. case http.StatusInternalServerError:
  46. panic("Something bad happened on '" + testCase.description + "'")
  47. }
  48. }