(t *testing.T)
| 98 | } |
| 99 | |
| 100 | func TestQueryParam(t *testing.T) { |
| 101 | var testCases = []struct { |
| 102 | name string |
| 103 | givenURL string |
| 104 | expect bool |
| 105 | expectErr string |
| 106 | }{ |
| 107 | { |
| 108 | name: "ok", |
| 109 | givenURL: "/?key=true", |
| 110 | expect: true, |
| 111 | }, |
| 112 | { |
| 113 | name: "nok, non existent key", |
| 114 | givenURL: "/?different=true", |
| 115 | expect: false, |
| 116 | expectErr: ErrNonExistentKey.Error(), |
| 117 | }, |
| 118 | { |
| 119 | name: "nok, invalid value", |
| 120 | givenURL: "/?key=invalidbool", |
| 121 | expect: false, |
| 122 | expectErr: `code=400, message=query param, err=failed to parse value, err: strconv.ParseBool: parsing "invalidbool": invalid syntax, field=key`, |
| 123 | }, |
| 124 | } |
| 125 | for _, tc := range testCases { |
| 126 | t.Run(tc.name, func(t *testing.T) { |
| 127 | req := httptest.NewRequest(http.MethodPost, tc.givenURL, nil) |
| 128 | c := NewContext(req, nil) |
| 129 | |
| 130 | v, err := QueryParam[bool](c, "key") |
| 131 | if tc.expectErr != "" { |
| 132 | assert.EqualError(t, err, tc.expectErr) |
| 133 | } else { |
| 134 | assert.NoError(t, err) |
| 135 | } |
| 136 | assert.Equal(t, tc.expect, v) |
| 137 | }) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestQueryParam_UnsupportedType(t *testing.T) { |
| 142 | req := httptest.NewRequest(http.MethodPost, "/?key=bool", nil) |
nothing calls this directly
no test coverage detected
searching dependent graphs…