Debian Codegraph @

misc_test.go

raw symbols SCIP
1 // Copyright 2014 Martini Authors
2 // Copyright 2014 The Macaron Authors
3 // Copyright 2024 The Forgejo Authors
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License"): you may
6 // not use this file except in compliance with the License. You may obtain
7 // a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 // License for the specific language governing permissions and limitations
15 // under the License.
16
17 package binding
18
19 import (
20 "fmt"
21 "net/http"
22 "net/http/httptest"
23 "strings"
24 "testing"
25
26 chi "github.com/go-chi/chi/v5"
27 "github.com/stretchr/testify/assert"
28 )
29
30 // When binding from Form data, testing the type of data to bind
31 // and converting a string into that type is tedious, so these tests
32 // cover all those cases.
33 func Test_SetWithProperType(t *testing.T) {
34 testInputs := map[string]string{
35 "successful": `integer=-1&integer8=-8&integer16=-16&integer32=-32&integer64=-64&uinteger=1&uinteger8=8&uinteger16=16&uinteger32=32&uinteger64=64&boolean_1=true&fl32_1=32.3232&fl64_1=-64.6464646464&str=string`,
36 "errorful": `integer=&integer8=asdf&integer16=--&integer32=&integer64=dsf&uinteger=&uinteger8=asdf&uinteger16=+&uinteger32= 32 &uinteger64=+%20+&boolean_1=&boolean_2=asdf&fl32_1=asdf&fl32_2=&fl64_1=&fl64_2=asdfstr`,
37 }
38
39 expectedOutputs := map[string]Everything{
40 "successful": {
41 Integer: -1,
42 Integer8: -8,
43 Integer16: -16,
44 Integer32: -32,
45 Integer64: -64,
46 Uinteger: 1,
47 Uinteger8: 8,
48 Uinteger16: 16,
49 Uinteger32: 32,
50 Uinteger64: 64,
51 Boolean1: true,
52 Fl32_1: 32.3232,
53 Fl64_1: -64.6464646464,
54 Str: "string",
55 },
56 "errorful": {},
57 }
58
59 for key, testCase := range testInputs {
60 httpRecorder := httptest.NewRecorder()
61 m := chi.NewRouter()
62
63 m.Post(testRoute, func(_ http.ResponseWriter, req *http.Request) {
64 var actual Everything
65 errs := Form(req, &actual)
66 assert.Equal(t, fmt.Sprintf("%+v", expectedOutputs[key]), fmt.Sprintf("%+v", actual))
67 if key == "errorful" {
68 assert.Len(t, errs, 10)
69 } else {
70 assert.Empty(t, errs)
71 }
72 })
73 req, err := http.NewRequest("POST", testRoute, strings.NewReader(testCase))
74 if err != nil {
75 panic(err)
76 }
77 req.Header.Set("Content-Type", formContentType)
78 m.ServeHTTP(httpRecorder, req)
79 }
80 }
81
82 // Each binder middleware should assert that the struct passed in is not
83 // a pointer (to avoid race conditions)
84 func Test_EnsureNotPointer(t *testing.T) {
85 shouldPanic := func() {
86 defer func() {
87 assert.NotNil(t, recover())
88 }()
89 ensureNotPointer(&Post{})
90 }
91
92 shouldNotPanic := func() {
93 defer func() {
94 assert.Nil(t, recover())
95 }()
96 ensureNotPointer(Post{})
97 }
98
99 shouldPanic()
100 shouldNotPanic()
101 }
102
103 // Used in testing setWithProperType; kind of clunky...
104 type Everything struct {
105 Integer int `form:"integer"`
106 Integer8 int8 `form:"integer8"`
107 Integer16 int16 `form:"integer16"`
108 Integer32 int32 `form:"integer32"`
109 Integer64 int64 `form:"integer64"`
110 Uinteger uint `form:"uinteger"`
111 Uinteger8 uint8 `form:"uinteger8"`
112 Uinteger16 uint16 `form:"uinteger16"`
113 Uinteger32 uint32 `form:"uinteger32"`
114 Uinteger64 uint64 `form:"uinteger64"`
115 Boolean1 bool `form:"boolean_1"`
116 Boolean2 bool `form:"boolean_2"`
117 Fl32_1 float32 `form:"fl32_1"`
118 Fl32_2 float32 `form:"fl32_2"`
119 Fl64_1 float64 `form:"fl64_1"`
120 Fl64_2 float64 `form:"fl64_2"`
121 Str string `form:"str"`
122 }