-
Notifications
You must be signed in to change notification settings - Fork 16
/
context_render_test.go
159 lines (128 loc) · 3.85 KB
/
context_render_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package rux
import (
"io/ioutil"
"net/http/httptest"
"os"
"testing"
"github.com/gookit/goutil/testutil/assert"
)
func TestContext_Redirect(t *testing.T) {
is := assert.New(t)
r := New()
// Redirect()
uri := "/Redirect"
r.GET(uri, func(c *Context) {
c.Redirect("/new-path")
})
w := mockRequest(r, GET, uri, nil)
is.Eq(301, w.Code)
is.Eq("/new-path", w.Header().Get("Location"))
is.Eq("<a href=\"/new-path\">Moved Permanently</a>.\n\n", w.Body.String())
uri = "/Redirect1"
r.GET(uri, func(c *Context) {
c.Redirect("/new-path1", 302)
})
w = mockRequest(r, GET, uri, nil)
is.Eq(302, w.Code)
is.Eq("/new-path1", w.Header().Get("Location"))
is.Eq("<a href=\"/new-path1\">Found</a>.\n\n", w.Body.String())
}
func TestContext_Back(t *testing.T) {
is := assert.New(t)
r := New()
// Back()
uri := "/Back"
r.GET(uri, func(c *Context) {
c.Back()
})
w := mockRequest(r, GET, uri, &md{H: m{"Referer": "/old-path"}})
is.Eq(302, w.Code)
is.Eq("/old-path", w.Header().Get("Location"))
is.Eq("<a href=\"/old-path\">Found</a>.\n\n", w.Body.String())
// Back()
uri = "/Back1"
r.GET(uri, func(c *Context) {
c.Back(301)
})
w = mockRequest(r, GET, uri, &md{H: m{"Referer": "/old-path1"}})
is.Eq(301, w.Code)
is.Eq("/old-path1", w.Header().Get("Location"))
is.Eq("<a href=\"/old-path1\">Moved Permanently</a>.\n\n", w.Body.String())
}
func TestContext_Blob(t *testing.T) {
is := assert.New(t)
r := New()
r.GET("/blob", func(c *Context) {
c.Blob(200, "text/plain; charset=UTF-8", []byte("blob-test"))
})
w := mockRequest(r, GET, "/blob", nil)
is.Eq(200, w.Code)
is.Eq("text/plain; charset=UTF-8", w.Header().Get(ContentType))
body, err := ioutil.ReadAll(w.Body)
is.NoErr(err)
is.Eq(string(body), "blob-test")
}
func TestContext_Binary(t *testing.T) {
is := assert.New(t)
c := mockContext("GET", "/site.md", nil, nil)
in, _ := os.Open("testdata/site.md")
c.Binary(200, in, "new-name.md", true)
w := c.RawWriter().(*httptest.ResponseRecorder)
is.Eq(200, w.Code)
ss, ok := w.Header()["Content-Type"]
is.True(ok)
is.Eq(ss[0], "application/octet-stream")
ss, ok = w.Header()["Content-Disposition"]
is.True(ok)
is.Eq(ss[0], "inline; filename=new-name.md")
is.Contains(w.Body.String(), "# readme")
}
func TestContext_FileContent(t *testing.T) {
is := assert.New(t)
c := mockContext("GET", "/site.md", nil, nil)
c.FileContent("testdata/site.md", "new-name.md")
w := c.RawWriter().(*httptest.ResponseRecorder)
is.Eq(200, c.StatusCode())
is.Eq(200, w.Code)
ss, ok := w.Header()["Content-Type"]
is.True(ok)
// go 1.14.4 "text/markdown; charset=utf-8" does not contain "text/plain"
is.Contains(ss[0], "text/")
is.Contains(w.Body.String(), "# readme")
is.True(8 < c.writer.Length())
c = mockContext("GET", "/site.md", nil, nil)
c.FileContent("testdata/not-exist.md")
w = c.RawWriter().(*httptest.ResponseRecorder)
is.Eq(500, c.StatusCode())
is.Eq(500, w.Code)
is.Eq("Internal Server Error\n", w.Body.String())
}
func TestContext_Attachment(t *testing.T) {
is := assert.New(t)
c := mockContext("GET", "/site.md", nil, nil)
c.Attachment("testdata/site.md", "new-name.md")
w := c.RawWriter().(*httptest.ResponseRecorder)
is.Eq(200, w.Code)
ss, ok := w.Header()["Content-Type"]
is.True(ok)
is.Eq(ss[0], "application/octet-stream")
ss, ok = w.Header()["Content-Disposition"]
is.True(ok)
is.Eq(ss[0], "attachment; filename=new-name.md")
is.Contains(w.Body.String(), "# readme")
}
func TestContext_Inline(t *testing.T) {
is := assert.New(t)
// Inline
c := mockContext("GET", "/site.md", nil, nil)
c.Inline("testdata/site.md", "new-name.md")
w := c.RawWriter().(*httptest.ResponseRecorder)
is.Eq(200, w.Code)
ss, ok := w.Header()["Content-Type"]
is.True(ok)
is.Eq(ss[0], "application/octet-stream")
ss, ok = w.Header()["Content-Disposition"]
is.True(ok)
is.Eq(ss[0], "inline; filename=new-name.md")
is.Contains(w.Body.String(), "# readme")
}