-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjosh_test.go
130 lines (121 loc) · 3.38 KB
/
josh_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package josh_test
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/orsinium-labs/josh"
)
func TestNewServer(t *testing.T) {
s := josh.NewServer("example.com:1337")
eq(s.Addr, "example.com:1337")
eq(s.ReadTimeout, 5*time.Second)
}
func TestSetHeader(t *testing.T) {
h := josh.Wrap(func(r josh.Req) josh.Resp {
josh.SetHeader(r, "X-Test", "hello")
return josh.NoContent()
})
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
h(w, req)
resp := w.Result()
eq(resp.StatusCode, 204)
eq(resp.Header.Get("X-Test"), "hello")
eq(resp.Header.Get("Content-Type"), "application/vnd.api+json")
}
// It should be possible to unwrap http.ResponseWriter and write the response directly.
func TestUnwrapResponseWriter(t *testing.T) {
h := josh.Wrap(func(r josh.Req) josh.Resp {
w := josh.Must(josh.GetSingleton[http.ResponseWriter](r))
w.WriteHeader(204)
return josh.Resp{}
})
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
h(w, req)
resp := w.Result()
eq(resp.StatusCode, 204)
eq(resp.Header.Get("Content-Type"), "")
}
func TestUnwrap(t *testing.T) {
hh := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(203)
}
h := josh.Wrap(josh.Unwrap(hh))
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
h(w, req)
resp := w.Result()
eq(resp.StatusCode, 203)
eq(resp.Header.Get("Content-Type"), "")
}
func TestRead(t *testing.T) {
h := josh.Wrap(func(r josh.Req) josh.Resp {
body, err := josh.Read[string]("foo", r.Body)
if err != nil {
panic(err)
}
msg := body.Attributes
msg = strings.ToUpper(msg)
return josh.Ok(msg)
})
req := httptest.NewRequest(
"GET", "http://example.com/foo",
bytes.NewReader([]byte(`{"data":{"type": "foo", "attributes": "hello"}}`)),
)
w := httptest.NewRecorder()
h(w, req)
resp := w.Result()
eq(resp.StatusCode, 200)
eq(resp.Header.Get("Content-Type"), "application/vnd.api+json")
body := must(io.ReadAll(resp.Body))
eq(string(body), `{"data":"HELLO"}`+"\n")
}
func TestSingleton(t *testing.T) {
type User struct{ name string }
h := josh.Wrap(func(r josh.Req) josh.Resp {
var err error
_, err = josh.GetSingleton[User](r)
if err == nil {
t.FailNow()
}
r = josh.Must(josh.WithSingleton(r, User{"aragorn"}))
user := josh.Must(josh.GetSingleton[User](r))
eq(user.name, "aragorn")
return josh.Ok("ok")
})
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
h(w, req)
resp := w.Result()
eq(resp.StatusCode, 200)
eq(resp.Header.Get("Content-Type"), "application/vnd.api+json")
body := must(io.ReadAll(resp.Body))
eq(string(body), `{"data":"ok"}`+"\n")
}
func TestGetOrSetSingleton(t *testing.T) {
type User struct{ name string }
h := josh.Wrap(func(r josh.Req) josh.Resp {
r, user := josh.GetOrSetSingleton(r, func() User {
return User{"aragorn"}
})
eq(user.name, "aragorn")
_, user = josh.GetOrSetSingleton(r, func() User {
return User{"gandalf"}
})
eq(user.name, "aragorn")
return josh.Ok("ok")
})
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
w := httptest.NewRecorder()
h(w, req)
resp := w.Result()
eq(resp.StatusCode, 200)
eq(resp.Header.Get("Content-Type"), "application/vnd.api+json")
body := must(io.ReadAll(resp.Body))
eq(string(body), `{"data":"ok"}`+"\n")
}