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

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: gaemiddleware: add middleware func for WithProd 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 | « server/prpc/server_test.go ('k') | server/router/handler.go » ('j') | no next file with comments »
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..be29225cbb4ef633d8efbe322ec09d8c0a323991
--- /dev/null
+++ b/server/router/example_test.go
@@ -0,0 +1,92 @@
+// 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"
+ "math/rand"
+ "net/http"
+ "net/http/httptest"
+
+ "golang.org/x/net/context"
+
+ "github.com/luci/luci-go/server/router"
+)
+
+func Logger(c *router.Context, next router.Handler) {
+ fmt.Println(c.Request.URL)
+ next(c)
+}
+
+func AuthCheck(c *router.Context, next router.Handler) {
+ var authenticated bool
+ if !authenticated {
+ c.Writer.WriteHeader(http.StatusUnauthorized)
+ c.Writer.Write([]byte("Authentication failed"))
+ return
+ }
+ next(c)
+}
+
+func GenerateSecret(c *router.Context, next router.Handler) {
+ c.Context = context.WithValue(c.Context, "secret", rand.Int())
+ next(c)
+}
+
+func makeRequest(client *http.Client, url string) string {
+ res, err := client.Get(url + "/")
+ if err != nil {
+ panic(err)
+ }
+ defer res.Body.Close()
+ p, err := ioutil.ReadAll(res.Body)
+ if err != nil {
+ panic(err)
+ }
+ if len(p) == 0 {
+ return fmt.Sprintf("%d", res.StatusCode)
+ }
+ return fmt.Sprintf("%d %s", res.StatusCode, p)
+}
+
+func makeRequests(url string) {
+ c := &http.Client{}
+ fmt.Println(makeRequest(c, url+"/hello"))
+ fmt.Println(makeRequest(c, url+"/hello/darknessmyoldfriend"))
+ fmt.Println(makeRequest(c, url+"/authenticated/secret"))
+}
+
+// Example_createServer demonstrates creating an HTTP server using the router
+// package.
+func Example_createServer() {
+ r := router.New()
+ r.Use(router.MiddlewareChain{Logger})
+ r.GET("/hello", nil, func(c *router.Context) {
+ fmt.Fprintf(c.Writer, "Hello")
+ })
+ r.GET("/hello/:name", nil, func(c *router.Context) {
+ fmt.Fprintf(c.Writer, "Hello %s", c.Params.ByName("name"))
+ })
+
+ auth := r.Subrouter("authenticated")
+ auth.Use(router.MiddlewareChain{AuthCheck})
+ auth.GET("/secret", router.MiddlewareChain{GenerateSecret}, func(c *router.Context) {
+ fmt.Fprintf(c.Writer, "secret: %d", c.Context.Value("secret"))
+ })
+
+ server := httptest.NewServer(r)
+ defer server.Close()
+
+ makeRequests(server.URL)
+
+ // Output:
+ // /hello
+ // 200 Hello
+ // /hello/darknessmyoldfriend
+ // 200 Hello darknessmyoldfriend
+ // /authenticated/secret
+ // 401 Authentication failed
+}
« no previous file with comments | « server/prpc/server_test.go ('k') | server/router/handler.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698