forked from go-aah/aah
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply_test.go
258 lines (202 loc) · 5.87 KB
/
reply_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// Source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package aah
import (
"bytes"
"fmt"
"html/template"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"aahframe.work/essentials"
"github.com/stretchr/testify/assert"
)
func TestReplyStatusCodes(t *testing.T) {
re := newReply(newContext(nil, nil))
assert.Equal(t, http.StatusOK, re.Code)
re.Ok()
assert.Equal(t, http.StatusOK, re.Code)
re.Created()
assert.Equal(t, http.StatusCreated, re.Code)
re.Accepted()
assert.Equal(t, http.StatusAccepted, re.Code)
re.NoContent()
assert.Equal(t, http.StatusNoContent, re.Code)
re.MovedPermanently()
assert.Equal(t, http.StatusMovedPermanently, re.Code)
re.Found()
assert.Equal(t, http.StatusFound, re.Code)
re.TemporaryRedirect()
assert.Equal(t, http.StatusTemporaryRedirect, re.Code)
re.BadRequest()
assert.Equal(t, http.StatusBadRequest, re.Code)
re.Unauthorized()
assert.Equal(t, http.StatusUnauthorized, re.Code)
re.Forbidden()
assert.Equal(t, http.StatusForbidden, re.Code)
re.NotFound()
assert.Equal(t, http.StatusNotFound, re.Code)
re.MethodNotAllowed()
assert.Equal(t, http.StatusMethodNotAllowed, re.Code)
re.Conflict()
assert.Equal(t, http.StatusConflict, re.Code)
re.InternalServerError()
assert.Equal(t, http.StatusInternalServerError, re.Code)
re.ServiceUnavailable()
assert.Equal(t, http.StatusServiceUnavailable, re.Code)
}
func TestReplyHTML(t *testing.T) {
tmplStr := `
{{ define "title" }}<title>This is test title</title>{{ end }}
{{ define "body" }}<p>This is test body</p>{{ end }}
`
buf, re1 := acquireBuffer(), newReply(nil)
tmpl := template.Must(template.New("test").Parse(tmplStr))
assert.NotNil(t, tmpl)
masterTmpl := filepath.Join(testdataBaseDir(), "reply", "views", "master.html")
_, err := tmpl.ParseFiles(masterTmpl)
assert.Nil(t, err)
re1.HTMLl("master", nil)
htmlRdr := re1.Rdr.(*htmlRender)
htmlRdr.Template = tmpl
err = re1.Rdr.Render(buf)
assert.Nil(t, err)
assert.True(t, strings.Contains(buf.String(), "<title>This is test title</title>"))
assert.True(t, strings.Contains(buf.String(), "<p>This is test body</p>"))
// Not template/layout name
buf.Reset()
htmlRdr.Layout = ""
err = re1.Rdr.Render(buf)
assert.Nil(t, err)
assert.True(t, ess.IsStrEmpty(buf.String()))
// Template is Nil
buf.Reset()
re1.HTML(nil)
err = re1.Rdr.Render(buf)
assert.NotNil(t, err)
assert.Equal(t, "template is nil", err.Error())
// HTMLlf
relf := newReply(nil)
relf.HTMLlf("docs.html", "Filename.html", nil)
assert.Equal(t, "text/html; charset=utf-8", relf.ContType)
htmllf := relf.Rdr.(*htmlRender)
assert.Equal(t, "docs.html", htmllf.Layout)
assert.Equal(t, "Filename.html", htmllf.Filename)
// HTMLf
ref := newReply(nil)
ref.HTMLf("Filename1.html", nil)
assert.Equal(t, "text/html; charset=utf-8", ref.ContType)
htmlf := ref.Rdr.(*htmlRender)
assert.True(t, ess.IsStrEmpty(htmlf.Layout))
assert.Equal(t, "Filename1.html", htmlf.Filename)
}
func TestReplyDone(t *testing.T) {
re1 := newReply(nil)
assert.False(t, re1.done)
re1.Done()
assert.True(t, re1.done)
}
// customRender implements the interface `aah.Render`.
type customRender struct {
// ... your fields goes here
}
func (cr *customRender) Render(w io.Writer) error {
fmt.Fprint(w, "This is custom render struct")
return nil
}
func TestReplyCustomRender(t *testing.T) {
re := newReply(nil)
buf := acquireBuffer()
re.Render(&customRender{})
err := re.Rdr.Render(buf)
assert.Nil(t, err)
assert.Equal(t, "This is custom render struct", buf.String())
releaseBuffer(buf)
// Render func
re = newReply(nil)
buf = acquireBuffer()
re.Render(RenderFunc(func(w io.Writer) error {
fmt.Fprint(w, "This is custom render func")
return nil
}))
err = re.Rdr.Render(buf)
assert.Nil(t, err)
assert.Equal(t, "This is custom render func", buf.String())
releaseBuffer(buf)
}
func TestRenderText(t *testing.T) {
buf := new(bytes.Buffer)
text1 := textRender{
Format: "welcome to %s %s",
Values: []interface{}{"aah", "framework"},
}
err := text1.Render(buf)
assert.Nil(t, err, "")
assert.Equal(t, "welcome to aah framework", buf.String())
buf.Reset()
text2 := textRender{Format: "welcome to aah framework"}
err = text2.Render(buf)
assert.Nil(t, err, "")
assert.Equal(t, "welcome to aah framework", buf.String())
}
func TestRenderJSON(t *testing.T) {
buf := acquireBuffer()
data := struct {
Name string
Age int
Address string
}{
Name: "John",
Age: 28,
Address: "this is my street",
}
json1 := jsonRender{Data: data}
err := json1.Render(buf)
assert.Nil(t, err, "")
assert.Equal(t, `{"Name":"John","Age":28,"Address":"this is my street"}`,
strings.TrimSpace(buf.String()))
}
func TestRenderFailureXML(t *testing.T) {
buf := new(bytes.Buffer)
data := struct {
Name string
Age int
Address string
}{
Name: "John",
Age: 28,
Address: "this is my street",
}
xml1 := xmlRender{Data: data}
err := xml1.Render(buf)
assert.Equal(t, "xml: unsupported type: struct { Name string; Age int; Address string }", err.Error())
}
func TestRenderFileNotExistsAndDir(t *testing.T) {
buf := new(bytes.Buffer)
// Directory error
file1 := binaryRender{Path: os.Getenv("HOME")}
err := file1.Render(buf)
assert.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "is a directory"))
assert.True(t, ess.IsStrEmpty(buf.String()))
// File not exists
file2 := binaryRender{Path: "file-not-exists.txt"}
err = file2.Render(buf)
assert.NotNil(t, err)
assert.True(t, strings.Contains(err.Error(), "open file-not-exists.txt:"))
assert.True(t, ess.IsStrEmpty(buf.String()))
}
func TestHTMLRenderTmplNil(t *testing.T) {
// Template is Nil
htmlTmplNil := htmlRender{
Layout: "master",
}
var buf bytes.Buffer
err := htmlTmplNil.Render(&buf)
assert.NotNil(t, err)
assert.Equal(t, "template is nil", err.Error())
}