| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Package prpctest is a package to facilitate pRPC testing by wrapping | 5 // Package prpctest is a package to facilitate pRPC testing by wrapping |
| 6 // httptest with a pRPC Server. | 6 // httptest with a pRPC Server. |
| 7 package prpctest | 7 package prpctest |
| 8 | 8 |
| 9 import ( | 9 import ( |
| 10 "errors" | 10 "errors" |
| 11 "fmt" | 11 "fmt" |
| 12 "net/http/httptest" | 12 "net/http/httptest" |
| 13 "net/url" | 13 "net/url" |
| 14 | 14 |
| 15 "github.com/julienschmidt/httprouter" | |
| 16 prpcCommon "github.com/luci/luci-go/common/prpc" | 15 prpcCommon "github.com/luci/luci-go/common/prpc" |
| 17 "github.com/luci/luci-go/server/auth" | 16 "github.com/luci/luci-go/server/auth" |
| 18 "github.com/luci/luci-go/server/middleware" | |
| 19 "github.com/luci/luci-go/server/prpc" | 17 "github.com/luci/luci-go/server/prpc" |
| 18 "github.com/luci/luci-go/server/router" |
| 20 "golang.org/x/net/context" | 19 "golang.org/x/net/context" |
| 21 ) | 20 ) |
| 22 | 21 |
| 23 // Server is a pRPC test server. | 22 // Server is a pRPC test server. |
| 24 type Server struct { | 23 type Server struct { |
| 25 prpc.Server | 24 prpc.Server |
| 26 | 25 |
| 27 » // Base is the base middleware generator factory. It is handed the Conte
xt | 26 » // Base returns a middleware chain. It is handed the Context passed to |
| 28 » // passed to Start. If nil, middleware.TestingBase will be used. | 27 » // Start. If Base is nil, setContext will be used. |
| 29 » Base func(context.Context) middleware.Base | 28 » Base func(context.Context) router.MiddlewareChain |
| 30 | 29 |
| 31 // HTTP is the active HTTP test server. It will be valid when the Server
is | 30 // HTTP is the active HTTP test server. It will be valid when the Server
is |
| 32 // running. | 31 // running. |
| 33 HTTP *httptest.Server | 32 HTTP *httptest.Server |
| 34 } | 33 } |
| 35 | 34 |
| 35 func setContext(c context.Context) router.MiddlewareChain { |
| 36 return router.MiddlewareChain{ |
| 37 func(ctx *router.Context, next router.Handler) { |
| 38 ctx.Context = c |
| 39 next(ctx) |
| 40 }, |
| 41 } |
| 42 } |
| 43 |
| 36 // Start starts the server. Any currently-registered services will be installed | 44 // Start starts the server. Any currently-registered services will be installed |
| 37 // into the pRPC Server. | 45 // into the pRPC Server. |
| 38 func (s *Server) Start(c context.Context) { | 46 func (s *Server) Start(c context.Context) { |
| 39 // Clean up any active server. | 47 // Clean up any active server. |
| 40 s.Close() | 48 s.Close() |
| 41 | 49 |
| 42 s.Authenticator = auth.Authenticator{} | 50 s.Authenticator = auth.Authenticator{} |
| 43 » mwb := s.Base | 51 » base := s.Base |
| 44 » if mwb == nil { | 52 » if base == nil { |
| 45 » » mwb = middleware.TestingBase | 53 » » base = setContext |
| 46 } | 54 } |
| 47 | 55 |
| 48 » r := httprouter.New() | 56 » r := router.New() |
| 49 » s.InstallHandlers(r, mwb(c)) | 57 » s.InstallHandlers(r, base(c)) |
| 50 s.HTTP = httptest.NewServer(r) | 58 s.HTTP = httptest.NewServer(r) |
| 51 } | 59 } |
| 52 | 60 |
| 53 // NewClient returns a prpc.Client configured to use the Server. | 61 // NewClient returns a prpc.Client configured to use the Server. |
| 54 func (s *Server) NewClient() (*prpcCommon.Client, error) { | 62 func (s *Server) NewClient() (*prpcCommon.Client, error) { |
| 55 if s.HTTP == nil { | 63 if s.HTTP == nil { |
| 56 return nil, errors.New("not running") | 64 return nil, errors.New("not running") |
| 57 } | 65 } |
| 58 | 66 |
| 59 u, err := url.Parse(s.HTTP.URL) | 67 u, err := url.Parse(s.HTTP.URL) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 70 } | 78 } |
| 71 | 79 |
| 72 // Close closes the Server, releasing any retained resources. | 80 // Close closes the Server, releasing any retained resources. |
| 73 func (s *Server) Close() { | 81 func (s *Server) Close() { |
| 74 if s.HTTP != nil { | 82 if s.HTTP != nil { |
| 75 s.HTTP.Close() | 83 s.HTTP.Close() |
| 76 | 84 |
| 77 s.HTTP = nil | 85 s.HTTP = nil |
| 78 } | 86 } |
| 79 } | 87 } |
| OLD | NEW |