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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | server/router/handler.go » ('j') | server/router/router.go » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: server/router/example_test.go
diff --git a/server/router/example_test.go b/server/router/example_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..7c2adf68b45e2113d7a5dc5ebe30a134135ae18f
--- /dev/null
+++ b/server/router/example_test.go
@@ -0,0 +1,123 @@
+// Copyright 2016 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+package router_test
+
+import (
+ "fmt"
+ "io/ioutil"
+ "log"
+ "math/rand"
+ "net/http"
+ "net/http/httptest"
+ "strconv"
+ "time"
+
+ "golang.org/x/net/context"
+
+ "github.com/luci/luci-go/server/router"
+)
+
+var (
+ durations []time.Duration
nodir 2016/06/16 04:20:25 consider removing, see below
nishanths 2016/06/16 22:11:26 Done.
+ client *http.Client
nodir 2016/06/16 04:20:25 unsed
nishanths 2016/06/16 22:11:26 Removed.
+ testServer *httptest.Server
nodir 2016/06/16 04:20:25 unused
nishanths 2016/06/16 22:11:26 Removed.
+)
+
+func Logger(c *router.Context, next router.Handler) {
+ log.Println(c.Request.URL.String())
+ next(c)
+}
+
+func Monitor(c *router.Context, next router.Handler) {
+ start := time.Now()
+ next(c)
+ 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.
+}
+
+func AuthCheck(c *router.Context, next router.Handler) {
+ var authenticated bool
+ if !authenticated {
+ c.WriteHeader(http.StatusUnauthorized)
+ return
+ }
+ next(c)
+}
+
+func GenerateRandom(c *router.Context, next router.Handler) {
+ secret := rand.Int()
+ c.Context = context.WithValue(c.Context, "secret", secret)
+ next(c)
+}
+
+func Root(c *router.Context) {
+ c.WriteHeader(http.StatusOK)
+}
+
+func Hello(c *router.Context) {
+ name := c.Params[0].Value
+ fmt.Fprintf(c, "Hello, %s", name)
+}
+
+func Random(c *router.Context) {
+ s := c.Value("secret").(int)
+ fmt.Fprintf(c, "secret: %d", s)
+}
+
+type Response struct {
+ status int
+ body []byte
+}
+
+func (r Response) String() string {
+ if len(r.body) == 0 {
+ return strconv.Itoa(r.status)
+ }
+ return fmt.Sprintf("%d %s", r.status, r.body)
+}
+
+func makeRequest(client *http.Client, url string) Response {
+ res, err := client.Get(url + "/")
+ if err != nil {
+ panic(err)
+ }
+ defer res.Body.Close()
+ status := res.StatusCode
+ body, err := ioutil.ReadAll(res.Body)
+ if err != nil {
+ panic(err)
+ }
+ 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.
+}
+
+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.
+ c := &http.Client{}
+ 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
+ fmt.Println(makeRequest(c, server.URL+"/"))
+ fmt.Println(makeRequest(c, server.URL+"/hello/darknessmyoldfriend"))
+ fmt.Println(makeRequest(c, server.URL+"/authenticated/random"))
+}
+
+// Example_createServer demonstrates creating an HTTP server using the imported
+// router package.
+func Example_createServer() {
+ r := router.New()
+ r.Use(router.MiddlewareChain{Logger, Monitor})
+ r.GET("/", nil, Root)
+ 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.
+
+ auth := r.Subrouter("authenticated")
+ auth.Use(router.MiddlewareChain{AuthCheck})
+ auth.GET("/random", router.MiddlewareChain{GenerateRandom}, Random)
+
+ server := httptest.NewServer(r)
+ defer server.Close()
+
+ makeRequests(server)
+
+ // Output:
+ // 200
+ // 200 Hello, darknessmyoldfriend
+ // 401
+}
« 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