Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
409 lines (343 loc) · 11.1 KB
/
Copy pathexample_test.go
File metadata and controls
409 lines (343 loc) · 11.1 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package iris_test
import (
"context"
"database/sql"
"fmt"
"net/http/httptest"
"strings"
"github.com/kataras/iris/v14"
"github.com/kataras/iris/v14/rest/errors"
)
// Serve a request without binding a port, which is also how the httptest
// package runs your tests.
func Example() {
app := iris.New()
app.Get("/greet/{name:string}", func(ctx iris.Context) {
ctx.WriteString("Hello, " + ctx.Params().GetString("name"))
})
if err := app.Build(); err != nil {
panic(err)
}
w := httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("GET", "/greet/gopher", nil))
fmt.Println(w.Code)
fmt.Println(w.Body.String())
// Output:
// 200
// Hello, gopher
}
// A path parameter carries a type. A request whose value does not match is
// answered before the handler runs, so the handler never sees a bad id.
func ExampleNew() {
app := iris.New()
app.Get("/products/{id:uuid}", func(ctx iris.Context) {
ctx.JSON(iris.Map{"id": ctx.Params().GetString("id")})
})
if err := app.Build(); err != nil {
panic(err)
}
// The rejection status depends on how errors are configured, so this only
// reports whether the request reached the handler at all.
for _, path := range []string{
"/products/6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"/products/not-a-uuid",
} {
w := httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
fmt.Println(w.Code == 200)
}
// Output:
// true
// false
}
// Default pre-registers a debug logger, permissive CORS, panic recovery and
// request IDs. Reach for it while exploring; configure the middleware yourself
// before you ship.
//
// The application it returns is not built yet, so routes are registered on it
// exactly as on New.
func ExampleDefault() {
app := iris.Default()
app.Get("/", func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "Hello, World!"})
})
// In a program this is app.Listen(":8080"), which builds and serves.
if err := app.Build(); err != nil {
panic(err)
}
w := httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("GET", "/", nil))
fmt.Println(w.Code)
fmt.Println(w.Body.String())
// Output:
// 200
// {"message":"Hello, World!"}
}
// greetAPI is a controller: the builder resolves its constructor through the
// dependency container and calls Routing to register the group's routes.
type greetAPI struct{}
func newGreetAPI() *greetAPI { return &greetAPI{} }
func (api *greetAPI) Routing(r iris.Router) {
r.Get("/{name:string}", func(ctx iris.Context) {
ctx.WriteString("Hello, " + ctx.Params().GetString("name"))
})
}
// The builder returns a narrower interface at every step, so a chain written in
// the wrong order does not compile. Health comes before Services, Services
// before Middlewares, and API last.
//
// Build closes the chain. Routes registered on the returned Application after
// that point are not picked up, so register them through API.
func ExampleNewBuilder() {
app := iris.NewBuilder().
NoPrefix().
AllowOrigin("*").
Compression(true).
LogRequests(false).
NoHealth().
Services().
RouterMiddlewares().
Middlewares().
API("/greet", newGreetAPI).
Build()
w := httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("GET", "/greet/gopher", nil))
fmt.Println(w.Body.String())
// Output: Hello, gopher
}
// The pieces a real API is wired from: a resource with a Close, a repository
// over it, a service over that, and a controller that the container builds
// from the service.
type exampleDB struct{ closed bool }
func openExampleDB() *exampleDB { return &exampleDB{} }
func (db *exampleDB) Close() error {
db.closed = true
return nil
}
type noteRepository struct{ db *exampleDB }
func newNoteRepository(db *exampleDB) *noteRepository {
return ¬eRepository{db: db}
}
func (r *noteRepository) list() []string { return []string{"first", "second"} }
type noteService struct{ repo *noteRepository }
func newNoteService(repo *noteRepository) *noteService {
return ¬eService{repo: repo}
}
func (s *noteService) list() []string { return s.repo.list() }
type noteAPI struct{ service *noteService }
func newNoteAPI(service *noteService) *noteAPI {
return ¬eAPI{service: service}
}
func (api *noteAPI) Routing(r iris.Router) {
r.Get("/", func(ctx iris.Context) {
ctx.JSON(api.service.list())
})
}
// A full chain: the container resolves openExampleDB into the repository, the
// repository into the service and the service into the controller, and the
// database's Close is registered as a shutdown closer because Services
// recognises a value with a Close() error method.
func ExampleNewBuilder_full() {
app := iris.NewBuilder().
Prefix("/api").
AllowOrigin("https://example.com").
Compression(true).
LogRequests(false).
Health(true, "development", "you@example.com").
Errors(errors.NewOptions().
MapErrors(errors.NotFound, sql.ErrNoRows)).
Deferrables(func() { /* stop background workers */ }).
Services(
openExampleDB(),
newNoteRepository,
newNoteService,
).
RouterMiddlewares(func(ctx iris.Context) {
ctx.SetHeader("X-Api", "notes")
ctx.Next()
}).
Middlewares(func(service *noteService) iris.Handler {
return func(ctx iris.Context) {
ctx.Values().Set("notes", len(service.list()))
ctx.Next()
}
}).
API("/notes", newNoteAPI).
Build()
w := httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("GET", "/api/notes", nil))
fmt.Println(w.Header().Get("X-Api"))
fmt.Println(w.Body.String())
if err := app.Shutdown(context.Background()); err != nil {
panic(err)
}
// Output:
// notes
// ["first","second"]
}
// BindJSON decodes the request body into the type you name. When it returns
// false the client error (400 for malformed JSON, 413 for a body over
// BodyMaxMemory) has already been written, so the handler only returns.
func ExampleContext_BindJSON() {
type createUser struct {
Name string `json:"name"`
}
app := iris.New()
app.Post("/users", func(ctx iris.Context) {
in, ok := ctx.BindJSON[createUser]()
if !ok {
return
}
ctx.Writef("creating %s", in.Name)
})
if err := app.Build(); err != nil {
panic(err)
}
w := httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("POST", "/users", strings.NewReader(`{"name":"gopher"}`)))
fmt.Println(w.Code, w.Body.String())
// The 400 body is a rest/errors envelope with a fresh id, so only the
// status is printed here.
w = httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("POST", "/users", strings.NewReader(`{"name":`)))
fmt.Println(w.Code)
// Output:
// 200 creating gopher
// 400
}
// BindParams binds the typed path parameters to a struct through the `param`
// field tag, so a route with several parameters reads them in one call.
func ExampleContext_BindParams() {
type bookPath struct {
StoreID int `param:"store_id"`
Book string `param:"book"`
}
app := iris.New()
app.Get("/stores/{store_id:int}/books/{book:string}", func(ctx iris.Context) {
p, ok := ctx.BindParams[bookPath]()
if !ok {
return
}
ctx.Writef("store %d, book %s", p.StoreID, p.Book)
})
if err := app.Build(); err != nil {
panic(err)
}
w := httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("GET", "/stores/7/books/iris", nil))
fmt.Println(w.Body.String())
// Output: store 7, book iris
}
// Paginated reads the page and size from the URL query and the filter from the
// JSON body, calls your collector with them and writes the Page envelope.
// The collector receives the offset and limit already computed; page_size in
// the envelope is the number of items on this page.
func ExampleContext_Paginated() {
type bookFilter struct {
Author string `json:"author"`
}
books := []string{"first", "second", "third"}
app := iris.New()
app.Post("/books/list", func(ctx iris.Context) {
ctx.Paginated(func(_ context.Context, opts iris.PageOptions, f bookFilter) ([]string, int64, error) {
start := min(opts.GetOffset(), len(books))
end := min(start+opts.GetLimit(), len(books))
return books[start:end], int64(len(books)), nil
})
})
if err := app.Build(); err != nil {
panic(err)
}
w := httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("POST", "/books/list?page=2&size=2", strings.NewReader(`{"author":"any"}`)))
fmt.Println(w.Body.String())
// Output: {"current_page":2,"page_size":1,"total_pages":2,"total_items":3,"has_next_page":false,"filter":{"author":"any"},"items":["third"]}
}
// Count answers a "how many" call with {"count": n}. Zero is an ordinary 200;
// a non-nil error is rendered through rest/errors instead.
func ExampleContext_Count() {
app := iris.New()
app.Delete("/sessions", func(ctx iris.Context) {
var removed int64 = 3 // what the store reported.
ctx.Count(removed, nil)
})
if err := app.Build(); err != nil {
panic(err)
}
w := httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("DELETE", "/sessions", nil))
fmt.Println(w.Code, w.Body.String())
// Output: 200 {"count":3}
}
// OK takes a service call's value and error together: the value renders as
// JSON, the error is rendered through rest/errors (here tagged NotFound with
// errors.NotFound.Wrap; an application-wide map does the same for a bare
// sql.ErrNoRows) and OK reports false.
func ExampleContext_OK() {
type user struct {
ID int `json:"id"`
Name string `json:"name"`
}
find := func(id int) (user, error) {
if id != 1 {
return user{}, errors.NotFound.Wrap(sql.ErrNoRows)
}
return user{ID: 1, Name: "gopher"}, nil
}
app := iris.New()
app.Get("/users/{id:int}", func(ctx iris.Context) {
ctx.OK(find(ctx.Params().GetInt("id")))
})
if err := app.Build(); err != nil {
panic(err)
}
w := httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("GET", "/users/1", nil))
fmt.Println(w.Code, strings.TrimSpace(w.Body.String()))
w = httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("GET", "/users/2", nil))
fmt.Println(w.Code)
// Output:
// 200 {"id":1,"name":"gopher"}
// 404
}
// Created answers 201. A bare identifier (string, int, int64, uint64 or
// uuid.UUID) is wrapped as {"id": ...}; any other value renders as it is.
func ExampleContext_Created() {
app := iris.New()
app.Post("/users", func(ctx iris.Context) {
var newID int64 = 42 // what the insert returned.
ctx.Created(newID, nil)
})
if err := app.Build(); err != nil {
panic(err)
}
w := httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("POST", "/users", nil))
fmt.Println(w.Code, w.Body.String())
// Output: 201 {"id":42}
}
// NoContent is the empty-body answer to a delete or an update: 204 when the
// call changed something, 304 when it succeeded and nothing changed. A non-nil
// error is rendered through rest/errors instead.
func ExampleContext_NoContent() {
users := map[int]bool{1: true}
app := iris.New()
app.Delete("/users/{id:int}", func(ctx iris.Context) {
id := ctx.Params().GetInt("id")
_, deleted := users[id]
delete(users, id)
ctx.NoContent(deleted, nil)
})
if err := app.Build(); err != nil {
panic(err)
}
for range 2 {
w := httptest.NewRecorder()
app.Handler.ServeHTTP(w, httptest.NewRequest("DELETE", "/users/1", nil))
fmt.Println(w.Code)
}
// Output:
// 204
// 304
}