Repository navigation
Expand file tree
/
Copy pathiris_builder_envelope_edges_test.go
More file actions
305 lines (253 loc) · 8.94 KB
/
Copy pathiris_builder_envelope_edges_test.go
File metadata and controls
305 lines (253 loc) · 8.94 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package iris_test
import (
"io"
"net/http"
nethttptest "net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/kataras/iris/v14"
"github.com/kataras/iris/v14/controller/fileserver"
"github.com/kataras/iris/v14/controller/mvc"
"github.com/kataras/iris/v14/httptest"
"github.com/kataras/iris/v14/sse"
"github.com/iris-contrib/httpexpect/v2"
"github.com/kataras/neffos"
)
// edgeBuilderApp is a builder application with the given routes under /, and
// nothing optional switched on.
func edgeBuilderApp(routes func(r iris.Router)) *iris.Application {
return iris.NewBuilder().
NoPrefix().
AllowOrigin("none").
Compression(false).
LogRequests(false).
NoHealth().
Services().
RouterMiddlewares().
Middlewares().
API("/", routes).
Build()
}
// A failure status is rendered whether or not the handler stopped the chain.
// ctx.NotFound() only sets the status, and a route that calls it and returns
// used to answer an empty body.
func TestBuilderEnvelopeCoversNotFoundWithoutStop(t *testing.T) {
app := edgeBuilderApp(func(r iris.Router) {
r.Get("/plans/{id}", func(ctx iris.Context) {
ctx.NotFound()
})
})
body := httptest.New(t, app).GET("/plans/42").Expect().
Status(httptest.StatusNotFound).Body().Raw()
doc := bareEnvelope(t, []byte(body), httptest.StatusNotFound, "NOT_FOUND")
if doc.Message != "Not Found" {
t.Fatalf("expected the status text as the message, got: %s", body)
}
}
// The file server sets 404 for a missing file and returns, without stopping
// the chain. Under the builder that is the envelope; a file that exists is
// served byte for byte, and the answers that carry no body keep none.
func TestBuilderFileServerMissingFile(t *testing.T) {
const contents = "body { color: #36c; }\n"
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "site.css"), []byte(contents), 0o600); err != nil {
t.Fatal(err)
}
app := iris.NewBuilder().
NoPrefix().
AllowOrigin("none").
Compression(false).
LogRequests(false).
NoHealth().
Services().
RouterMiddlewares().
Middlewares().
API("/static", fileserver.New(root, fileserver.Options{})).
Build()
e := httptest.New(t, app)
// An existing file, byte for byte.
existing := e.GET("/static/site.css").Expect().Status(httptest.StatusOK)
existing.Body().IsEqual(contents)
lastModified := existing.Header("Last-Modified").NotEmpty().Raw()
// A missing one.
body := e.GET("/static/missing.css").Expect().Status(httptest.StatusNotFound).Body().Raw()
bareEnvelope(t, []byte(body), httptest.StatusNotFound, "NOT_FOUND")
// A fresh copy: 304 is not a failure and stays empty.
e.GET("/static/site.css").WithHeader("If-Modified-Since", lastModified).Expect().
Status(httptest.StatusNotModified).Body().IsEmpty()
// HEAD for a missing file over a real connection: the status and no body,
// which is all a HEAD answer may carry.
srv := nethttptest.NewServer(app)
defer srv.Close()
res, err := srv.Client().Head(srv.URL + "/static/missing.css")
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
headBody, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != http.StatusNotFound || len(headBody) != 0 {
t.Fatalf("expected an empty 404 for HEAD, got %d %q", res.StatusCode, headBody)
}
// And the connection is still usable for the next request.
res, err = srv.Client().Get(srv.URL + "/static/site.css")
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
if got, _ := io.ReadAll(res.Body); string(got) != contents {
t.Fatalf("expected the file after the HEAD request, got %q", got)
}
}
// Nothing below 400 is touched: a redirect and a 304 answer what they answer
// on an application built with iris.New().
func TestBuilderEnvelopeLeavesRedirectsAndNotModifiedAlone(t *testing.T) {
routes := func(r iris.Router) {
r.Get("/old", func(ctx iris.Context) {
ctx.Redirect("/new", iris.StatusFound)
})
r.Get("/cached", func(ctx iris.Context) {
ctx.StopWithStatus(iris.StatusNotModified)
})
r.Get("/empty", func(ctx iris.Context) {
ctx.StatusCode(iris.StatusNoContent)
})
}
bare := iris.New()
routes(bare)
expect := httptest.New(t, bare)
got := httptest.New(t, edgeBuilderApp(routes))
for _, path := range []string{"/old", "/cached", "/empty"} {
expected := expect.GET(path).WithRedirectPolicy(httpexpect.DontFollowRedirects).Expect().Raw()
expectedBody, _ := io.ReadAll(expected.Body)
actual := got.GET(path).WithRedirectPolicy(httpexpect.DontFollowRedirects).Expect().Raw()
actualBody, _ := io.ReadAll(actual.Body)
if actual.StatusCode != expected.StatusCode || string(actualBody) != string(expectedBody) {
t.Fatalf("%s: expected %d %q as on a bare application, got %d %q", path,
expected.StatusCode, expectedBody, actual.StatusCode, actualBody)
}
if location := actual.Header.Get("Location"); location != expected.Header.Get("Location") {
t.Fatalf("%s: expected Location %q, got %q", path, expected.Header.Get("Location"), location)
}
}
}
// A stream that fails after its first event has a body already: the events.
// No error document may follow them.
func TestBuilderEnvelopeLeavesAStartedStreamAlone(t *testing.T) {
app := edgeBuilderApp(func(r iris.Router) {
r.Get("/events", func(ctx iris.Context) {
stream, err := sse.Open(ctx)
if err != nil {
ctx.StopWithStatus(iris.StatusNotImplemented)
return
}
defer stream.Close(nil)
if err := stream.Send(sse.Event{Data: []byte("first")}); err != nil {
return
}
ctx.StopWithStatus(iris.StatusInternalServerError)
})
})
app.Logger().SetLevel("disable")
srv := nethttptest.NewServer(app)
defer srv.Close()
client := srv.Client()
client.Timeout = 10 * time.Second
res, err := client.Get(srv.URL + "/events")
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != http.StatusOK {
t.Fatalf("expected the 200 the stream started with, got %d", res.StatusCode)
}
if !strings.Contains(string(body), "data: first") {
t.Fatalf("expected the first event in the stream, got %q", body)
}
if strings.Contains(string(body), "http_error_code") {
t.Fatalf("an error document was appended to a stream: %q", body)
}
}
type edgeWebsocketController struct{}
func (c *edgeWebsocketController) Namespace() string { return "default" }
func (c *edgeWebsocketController) OnNamespaceConnected(*neffos.NSConn, neffos.Message) error {
return nil
}
// A websocket endpoint refuses a request that is not a handshake with its own
// 400 and its own text. That answer has a body, so it is left as it is.
func TestBuilderRefusedWebsocketHandshakeUnchanged(t *testing.T) {
app := iris.NewBuilder().
NoPrefix().
AllowOrigin("none").
Compression(false).
LogRequests(false).
NoHealth().
Services().
RouterMiddlewares().
Middlewares().
API("/ws", mvc.Websocket(&edgeWebsocketController{})).
Build()
res := httptest.New(t, app).GET("/ws").Expect().Status(httptest.StatusBadRequest)
res.Body().IsEqual("Bad Request\n")
res.Header("Sec-Websocket-Version").IsEqual("13")
}
// iris.Default is built on the builder and hands back an application that is
// not built yet. The 404 and 405 handlers are installed when it is.
func TestDefaultAnswersTheEnvelope(t *testing.T) {
app := iris.Default()
app.Get("/forbidden", func(ctx iris.Context) {
ctx.StopWithStatus(iris.StatusForbidden)
})
e := httptest.New(t, app)
body := e.GET("/forbidden").Expect().Status(httptest.StatusForbidden).Body().Raw()
bareEnvelope(t, []byte(body), httptest.StatusForbidden, "PERMISSION_DENIED")
body = e.GET("/unknown").Expect().Status(httptest.StatusNotFound).Body().Raw()
bareEnvelope(t, []byte(body), httptest.StatusNotFound, "NOT_FOUND")
}
// The two failures the builder's handler does not render. Both are answered
// before it runs, and the documentation of NewBuilder names them; this test
// keeps that sentence true.
func TestBuilderEnvelopeNamedGaps(t *testing.T) {
// A UseRouter handler registered from a Configurator sits ahead of every
// built-in the builder registers, the errors handler included.
early := iris.ConfiguratorFunc(func(app *iris.Application) {
app.UseRouter(func(ctx iris.Context) {
if ctx.Header("X-Blocked") != "" {
ctx.StopWithStatus(iris.StatusForbidden)
return
}
ctx.Next()
})
})
app := iris.NewBuilder(early).
NoPrefix().
AllowOrigin("none").
Compression(true).
LogRequests(false).
NoHealth().
Services().
RouterMiddlewares().
Middlewares().
API("/", func(r iris.Router) {
r.Post("/upload", func(ctx iris.Context) {
ctx.WriteString("ok")
})
}).
Build()
e := httptest.New(t, app)
e.POST("/upload").WithHeader("X-Blocked", "1").WithText("x").Expect().
Status(httptest.StatusForbidden).Body().IsEmpty()
// The compression middleware's own 415: a request body that is not what
// its Content-Encoding says.
e.POST("/upload").WithHeader("Content-Encoding", "gzip").WithText("not gzip at all").Expect().
Status(httptest.StatusUnsupportedMediaType).Body().IsEmpty()
}