-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_test.go
52 lines (45 loc) · 1.15 KB
/
path_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package josh_test
import (
"io"
"net/http/httptest"
"testing"
"github.com/orsinium-labs/josh"
)
func TestGetID(t *testing.T) {
h := josh.Wrap(func(r josh.Req) josh.Resp {
id, errResp := josh.GetID[int](r, "id")
if errResp != nil {
return josh.BadRequest(*errResp)
}
return josh.Ok(id)
})
{
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
h(w, req)
resp := w.Result()
eq(resp.StatusCode, 400)
body := must(io.ReadAll(resp.Body))
eq(string(body), `{"errors":[{"detail":"path parameter id is required"}]}`+"\n")
}
{
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
req.SetPathValue("id", "13")
w := httptest.NewRecorder()
h(w, req)
resp := w.Result()
eq(resp.StatusCode, 200)
body := must(io.ReadAll(resp.Body))
eq(string(body), `{"data":13}`+"\n")
}
{
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
req.SetPathValue("id", "hi")
w := httptest.NewRecorder()
h(w, req)
resp := w.Result()
eq(resp.StatusCode, 400)
body := must(io.ReadAll(resp.Body))
eq(string(body), `{"errors":[{"detail":"invalid path parameter id"}]}`+"\n")
}
}