-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
multi_return_test.go
118 lines (91 loc) · 3.07 KB
/
multi_return_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
package fuego
import (
"context"
"errors"
"io"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
type MockCtxRenderer struct {
RenderFunc func(context.Context, io.Writer) error
}
func RenderString(s string) MockCtxRenderer {
return MockCtxRenderer{
RenderFunc: func(c context.Context, w io.Writer) error {
_, err := w.Write([]byte(s))
return err
},
}
}
var _ CtxRenderer = MockCtxRenderer{}
func (m MockCtxRenderer) Render(c context.Context, w io.Writer) error {
if m.RenderFunc == nil {
return errors.New("RenderFunc is nil")
}
return m.RenderFunc(c, w)
}
type MyType struct {
Name string
}
func TestMultiReturn(t *testing.T) {
s := NewServer()
Get(s, "/data", func(c ContextNoBody) (DataOrTemplate[MyType], error) {
entity := MyType{Name: "Ewen"}
return DataOrTemplate[MyType]{
Data: entity,
Template: RenderString(`<div>` + entity.Name + `</div>`),
}, nil
})
Get(s, "/other", func(c ContextNoBody) (*DataOrTemplate[MyType], error) {
entity := MyType{Name: "Ewen"}
return DataOrHTML(
entity,
RenderString(`<div>`+entity.Name+`</div>`),
), nil
})
t.Run("requests HTML by default", func(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/data", nil)
s.Mux.ServeHTTP(recorder, req)
require.Equal(t, 200, recorder.Code)
require.Equal(t, "text/html; charset=utf-8", recorder.Header().Get("Content-Type"))
require.Equal(t, `<div>Ewen</div>`, recorder.Body.String())
})
t.Run("requests JSON", func(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/data", nil)
req.Header.Set("Accept", "application/json")
s.Mux.ServeHTTP(recorder, req)
require.Equal(t, 200, recorder.Code)
require.Equal(t, "application/json", recorder.Header().Get("Content-Type"))
require.Equal(t, crlf(`{"Name":"Ewen"}`), recorder.Body.String())
})
t.Run("requests JSON, using the shortcut", func(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/other", nil)
req.Header.Set("Accept", "application/json")
s.Mux.ServeHTTP(recorder, req)
require.Equal(t, 200, recorder.Code)
require.Equal(t, "application/json", recorder.Header().Get("Content-Type"))
require.Equal(t, crlf(`{"Name":"Ewen"}`), recorder.Body.String())
})
t.Run("requests XML", func(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/data", nil)
req.Header.Set("Accept", "application/xml")
s.Mux.ServeHTTP(recorder, req)
require.Equal(t, 200, recorder.Code)
require.Equal(t, "application/xml", recorder.Header().Get("Content-Type"))
require.Equal(t, `<MyType><Name>Ewen</Name></MyType>`, recorder.Body.String())
})
t.Run("requests HTML", func(t *testing.T) {
recorder := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/data", nil)
req.Header.Set("Accept", "text/html")
s.Mux.ServeHTTP(recorder, req)
require.Equal(t, 200, recorder.Code)
require.Contains(t, recorder.Header().Get("Content-Type"), "text/html")
require.Equal(t, `<div>Ewen</div>`, recorder.Body.String())
})
}