-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse_test.go
111 lines (105 loc) · 2.26 KB
/
response_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
package hx
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestResponse(t *testing.T) {
t.Parallel()
type args struct {
options []ResponseOption
}
tests := map[string]struct {
args args
wantHeaders http.Header
wantStatus int
wantErr error
}{
"Set status": {
args: args{
options: []ResponseOption{
Status(http.StatusAccepted),
},
},
wantHeaders: http.Header{},
wantStatus: http.StatusAccepted,
},
"Set stop polling": {
args: args{
options: []ResponseOption{
StatusStopPolling,
},
},
wantHeaders: http.Header{},
wantStatus: int(StatusStopPolling),
},
"Set headers": {
args: args{
options: []ResponseOption{
Location("/foo"),
SwapOuterHtml.FocusScroll(true),
},
},
wantHeaders: http.Header{
HxLocation: []string{`/foo`},
HxReswap: []string{`outerHTML focus-scroll:true`},
},
wantStatus: http.StatusOK,
},
"Overwrite headers": {
args: args{
options: []ResponseOption{
Location("/foo"),
SwapOuterHtml.FocusScroll(true),
Location("/bar"),
},
},
wantHeaders: http.Header{
HxLocation: []string{`/bar`},
HxReswap: []string{`outerHTML focus-scroll:true`},
},
wantStatus: http.StatusOK,
},
"Set status and headers": {
args: args{
options: []ResponseOption{
Status(http.StatusAccepted),
Location("/foo"),
SwapOuterHtml.FocusScroll(false),
},
},
wantHeaders: http.Header{
HxLocation: []string{`/foo`},
HxReswap: []string{`outerHTML focus-scroll:false`},
},
wantStatus: http.StatusAccepted,
},
"Panics and recovers": {
args: args{
options: []ResponseOption{
Location("/foo"),
Trigger(func() map[string]any {
panic(fmt.Errorf("bad event data"))
}),
},
},
wantErr: fmt.Errorf("bad event data"),
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
wr := httptest.NewRecorder()
err := Response(wr, tt.args.options...)
if tt.wantErr != nil {
assert.EqualError(t, err, tt.wantErr.Error())
return
}
gotHeaders := wr.Header()
assert.Equal(t, tt.wantHeaders, gotHeaders)
gotStatus := wr.Code
assert.Equal(t, tt.wantStatus, gotStatus)
})
}
}