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

Unified Diff: common/testing/prpctest/server.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 | « common/prpc/talk/helloworld/frontend/prpc.go ('k') | server/auth/context.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/testing/prpctest/server.go
diff --git a/common/testing/prpctest/server.go b/common/testing/prpctest/server.go
index 670482440596f82ba99e157f6af35790aee079a5..298f5f3459bb80ff30504dc1ad1e98dab6c4c79d 100644
--- a/common/testing/prpctest/server.go
+++ b/common/testing/prpctest/server.go
@@ -5,55 +5,63 @@
// Package prpctest is a package to facilitate pRPC testing by wrapping
// httptest with a pRPC Server.
package prpctest
import (
"errors"
"fmt"
"net/http/httptest"
"net/url"
- "github.com/julienschmidt/httprouter"
prpcCommon "github.com/luci/luci-go/common/prpc"
"github.com/luci/luci-go/server/auth"
- "github.com/luci/luci-go/server/middleware"
"github.com/luci/luci-go/server/prpc"
+ "github.com/luci/luci-go/server/router"
"golang.org/x/net/context"
)
// Server is a pRPC test server.
type Server struct {
prpc.Server
- // Base is the base middleware generator factory. It is handed the Context
- // passed to Start. If nil, middleware.TestingBase will be used.
- Base func(context.Context) middleware.Base
+ // Base returns a middleware chain. It is handed the Context passed to
+ // Start. If Base is nil, setContext will be used.
+ Base func(context.Context) router.MiddlewareChain
// HTTP is the active HTTP test server. It will be valid when the Server is
// running.
HTTP *httptest.Server
}
+func setContext(c context.Context) router.MiddlewareChain {
+ return router.MiddlewareChain{
+ func(ctx *router.Context, next router.Handler) {
+ ctx.Context = c
+ next(ctx)
+ },
+ }
+}
+
// Start starts the server. Any currently-registered services will be installed
// into the pRPC Server.
func (s *Server) Start(c context.Context) {
// Clean up any active server.
s.Close()
s.Authenticator = auth.Authenticator{}
- mwb := s.Base
- if mwb == nil {
- mwb = middleware.TestingBase
+ base := s.Base
+ if base == nil {
+ base = setContext
}
- r := httprouter.New()
- s.InstallHandlers(r, mwb(c))
+ r := router.New()
+ s.InstallHandlers(r, base(c))
s.HTTP = httptest.NewServer(r)
}
// NewClient returns a prpc.Client configured to use the Server.
func (s *Server) NewClient() (*prpcCommon.Client, error) {
if s.HTTP == nil {
return nil, errors.New("not running")
}
u, err := url.Parse(s.HTTP.URL)
« no previous file with comments | « common/prpc/talk/helloworld/frontend/prpc.go ('k') | server/auth/context.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698