Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(461)

Side by Side Diff: server/router/example_test.go

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: router: Improve documentation style Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | server/router/handler.go » ('j') | server/router/router.go » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package router_test
6
7 import (
8 "fmt"
9 "io/ioutil"
10 "log"
11 "math/rand"
12 "net/http"
13 "net/http/httptest"
14 "strconv"
15 "time"
16
17 "golang.org/x/net/context"
18
19 "github.com/luci/luci-go/server/router"
20 )
21
22 var (
23 durations []time.Duration
nodir 2016/06/16 04:20:25 consider removing, see below
nishanths 2016/06/16 22:11:26 Done.
24 client *http.Client
nodir 2016/06/16 04:20:25 unsed
nishanths 2016/06/16 22:11:26 Removed.
25 testServer *httptest.Server
nodir 2016/06/16 04:20:25 unused
nishanths 2016/06/16 22:11:26 Removed.
26 )
27
28 func Logger(c *router.Context, next router.Handler) {
29 log.Println(c.Request.URL.String())
30 next(c)
31 }
32
33 func Monitor(c *router.Context, next router.Handler) {
34 start := time.Now()
35 next(c)
36 durations = append(durations, time.Since(start))
nodir 2016/06/16 04:20:25 maybe just print it to stdout? it will be more vis
nishanths 2016/06/16 22:11:26 Done.
37 }
38
39 func AuthCheck(c *router.Context, next router.Handler) {
40 var authenticated bool
41 if !authenticated {
42 c.WriteHeader(http.StatusUnauthorized)
43 return
44 }
45 next(c)
46 }
47
48 func GenerateRandom(c *router.Context, next router.Handler) {
49 secret := rand.Int()
50 c.Context = context.WithValue(c.Context, "secret", secret)
51 next(c)
52 }
53
54 func Root(c *router.Context) {
55 c.WriteHeader(http.StatusOK)
56 }
57
58 func Hello(c *router.Context) {
59 name := c.Params[0].Value
60 fmt.Fprintf(c, "Hello, %s", name)
61 }
62
63 func Random(c *router.Context) {
64 s := c.Value("secret").(int)
65 fmt.Fprintf(c, "secret: %d", s)
66 }
67
68 type Response struct {
69 status int
70 body []byte
71 }
72
73 func (r Response) String() string {
74 if len(r.body) == 0 {
75 return strconv.Itoa(r.status)
76 }
77 return fmt.Sprintf("%d %s", r.status, r.body)
78 }
79
80 func makeRequest(client *http.Client, url string) Response {
81 res, err := client.Get(url + "/")
82 if err != nil {
83 panic(err)
84 }
85 defer res.Body.Close()
86 status := res.StatusCode
87 body, err := ioutil.ReadAll(res.Body)
88 if err != nil {
89 panic(err)
90 }
91 return Response{status, body}
nodir 2016/06/16 04:20:25 to simplify, I'd return sprintf right here
nishanths 2016/06/16 22:11:26 Done.
92 }
93
94 func makeRequests(server *httptest.Server) {
nodir 2016/06/16 04:20:25 perhaps "url string" would be enough here
nishanths 2016/06/16 22:11:26 Done.
95 c := &http.Client{}
96 c.Timeout = 10 * time.Second
nodir 2016/06/16 04:20:25 why this is needed?
nishanths 2016/06/16 22:11:26 If Timeout==0, the client waits infinitely for a r
97 fmt.Println(makeRequest(c, server.URL+"/"))
98 fmt.Println(makeRequest(c, server.URL+"/hello/darknessmyoldfriend"))
99 fmt.Println(makeRequest(c, server.URL+"/authenticated/random"))
100 }
101
102 // Example_createServer demonstrates creating an HTTP server using the imported
103 // router package.
104 func Example_createServer() {
105 r := router.New()
106 r.Use(router.MiddlewareChain{Logger, Monitor})
107 r.GET("/", nil, Root)
108 r.GET("/hello/:name", nil, Hello)
nodir 2016/06/16 04:20:25 consider inlining Root, Hello and Random to make e
nishanths 2016/06/16 22:11:26 Done.
109
110 auth := r.Subrouter("authenticated")
111 auth.Use(router.MiddlewareChain{AuthCheck})
112 auth.GET("/random", router.MiddlewareChain{GenerateRandom}, Random)
113
114 server := httptest.NewServer(r)
115 defer server.Close()
116
117 makeRequests(server)
118
119 // Output:
120 // 200
121 // 200 Hello, darknessmyoldfriend
122 // 401
123 }
OLDNEW
« no previous file with comments | « no previous file | server/router/handler.go » ('j') | server/router/router.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698