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.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. TestForm struct {
  24. BranchName string `form:"BranchName" binding:"GitRefName"`
  25. URL string `form:"ValidUrl" binding:"ValidUrl"`
  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. assert.Equal(t, fmt.Sprintf("%+v", testCase.expectedErrors), fmt.Sprintf("%+v", actual))
  33. })
  34. req, err := http.NewRequest("POST", testRoute, nil)
  35. if err != nil {
  36. panic(err)
  37. }
  38. m.ServeHTTP(httpRecorder, req)
  39. switch httpRecorder.Code {
  40. case http.StatusNotFound:
  41. panic("Routing is messed up in test fixture (got 404): check methods and paths")
  42. case http.StatusInternalServerError:
  43. panic("Something bad happened on '" + testCase.description + "'")
  44. }
  45. }