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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. "fmt"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. "github.com/go-macaron/binding"
  11. "github.com/stretchr/testify/assert"
  12. "gopkg.in/macaron.v1"
  13. )
  14. const (
  15. testRoute = "/test"
  16. )
  17. type (
  18. validationTestCase struct {
  19. description string
  20. data interface{}
  21. expectedErrors binding.Errors
  22. }
  23. handlerFunc func(interface{}, ...interface{}) macaron.Handler
  24. modeler interface {
  25. Model() string
  26. }
  27. TestForm struct {
  28. BranchName string `form:"BranchName" binding:"GitRefName"`
  29. URL string `form:"ValidUrl" binding:"ValidUrl"`
  30. }
  31. )
  32. func performValidationTest(t *testing.T, testCase validationTestCase) {
  33. httpRecorder := httptest.NewRecorder()
  34. m := macaron.Classic()
  35. m.Post(testRoute, binding.Validate(testCase.data), func(actual binding.Errors) {
  36. assert.Equal(t, fmt.Sprintf("%+v", testCase.expectedErrors), fmt.Sprintf("%+v", 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. }