forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_writer_test.go
163 lines (126 loc) · 4.24 KB
/
response_writer_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
package iris_test
import (
"fmt"
"testing"
"gopkg.in/kataras/iris.v6"
"gopkg.in/kataras/iris.v6/httptest"
)
// most tests lives inside context_test.go:Transactions, there lives the response writer's full and coblex tests
func TestResponseWriterBeforeFlush(t *testing.T) {
app := iris.New()
app.Adapt(newTestNativeRouter())
body := "my body"
beforeFlushBody := "body appeneded or setted before callback"
app.Get("/", func(ctx *iris.Context) {
w := ctx.ResponseWriter
w.SetBeforeFlush(func() {
w.WriteString(beforeFlushBody)
})
w.WriteString(body)
})
// recorder can change the status code after write too
// it can also be changed everywhere inside the context's lifetime
app.Get("/recorder", func(ctx *iris.Context) {
w := ctx.Recorder()
w.SetBeforeFlush(func() {
w.SetBodyString(beforeFlushBody)
w.WriteHeader(iris.StatusForbidden)
})
w.WriteHeader(iris.StatusOK)
w.WriteString(body)
})
e := httptest.New(app, t)
e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(body + beforeFlushBody)
e.GET("/recorder").Expect().Status(iris.StatusForbidden).Body().Equal(beforeFlushBody)
}
func TestResponseWriterToRecorderMiddleware(t *testing.T) {
app := iris.New()
app.Adapt(newTestNativeRouter())
beforeFlushBody := "body appeneded or setted before callback"
app.UseGlobal(iris.Recorder)
app.Get("/", func(ctx *iris.Context) {
w := ctx.Recorder()
w.SetBeforeFlush(func() {
w.SetBodyString(beforeFlushBody)
w.WriteHeader(iris.StatusForbidden)
})
w.WriteHeader(iris.StatusOK)
w.WriteString("this will not be sent at all because of SetBodyString")
})
e := httptest.New(app, t)
e.GET("/").Expect().Status(iris.StatusForbidden).Body().Equal(beforeFlushBody)
}
func TestResponseRecorderStatusCodeContentTypeBody(t *testing.T) {
app := iris.New()
app.Adapt(newTestNativeRouter())
firstStatusCode := iris.StatusOK
contentType := "text/html; charset=" + app.Config.Charset
firstBodyPart := "first"
secondBodyPart := "second"
prependedBody := "zero"
expectedBody := prependedBody + firstBodyPart + secondBodyPart
app.Use(iris.Recorder)
// recorder's status code can change if needed by a middleware or the last handler.
app.UseFunc(func(ctx *iris.Context) {
ctx.SetStatusCode(firstStatusCode)
ctx.Next()
})
app.UseFunc(func(ctx *iris.Context) {
ctx.SetContentType(contentType)
ctx.Next()
})
app.UseFunc(func(ctx *iris.Context) {
// set a body ( we will append it later, only with response recorder we can set append or remove a body or a part of it*)
ctx.WriteString(firstBodyPart)
ctx.Next()
})
app.UseFunc(func(ctx *iris.Context) {
ctx.WriteString(secondBodyPart)
ctx.Next()
})
app.Get("/", func(ctx *iris.Context) {
previousStatusCode := ctx.StatusCode()
if previousStatusCode != firstStatusCode {
t.Fatalf("Previous status code should be %d but got %d", firstStatusCode, previousStatusCode)
}
previousContentType := ctx.ContentType()
if previousContentType != contentType {
t.Fatalf("First content type should be %s but got %d", contentType, previousContentType)
}
// change the status code, this will tested later on (httptest)
ctx.SetStatusCode(iris.StatusForbidden)
prevBody := string(ctx.Recorder().Body())
if prevBody != firstBodyPart+secondBodyPart {
t.Fatalf("Previous body (first handler + second handler's writes) expected to be: %s but got: %s", firstBodyPart+secondBodyPart, prevBody)
}
// test it on httptest later on
ctx.Recorder().SetBodyString(prependedBody + prevBody)
})
e := httptest.New(app, t)
et := e.GET("/").Expect().Status(iris.StatusForbidden)
et.Header("Content-Type").Equal(contentType)
et.Body().Equal(expectedBody)
}
func ExampleResponseWriter_WriteHeader() {
app := iris.New()
app.Adapt(newTestNativeRouter())
expectedOutput := "Hey"
app.Get("/", func(ctx *iris.Context) {
// here
for i := 0; i < 10; i++ {
ctx.ResponseWriter.WriteHeader(iris.StatusOK)
}
ctx.Writef(expectedOutput)
// here
fmt.Println(expectedOutput)
// here
for i := 0; i < 10; i++ {
ctx.SetStatusCode(iris.StatusOK)
}
})
e := httptest.New(app, nil)
e.GET("/").Expect().Status(iris.StatusOK).Body().Equal(expectedOutput)
// here it shouldn't log an error that status code write multiple times (by the net/http package.)
// Output:
// Hey
}