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

Side by Side 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: Update tests 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
OLDNEW
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 is the base middleware generator factory. It is handed the Conte xt
28 » // passed to Start. If nil, middleware.TestingBase will be used. 27 » // passed to Start. If Base is nil, middleware.TestingBase will be used.
29 » Base func(context.Context) middleware.Base 28 » Base func(c context.Context) router.Base
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
36 // Start starts the server. Any currently-registered services will be installed 35 // Start starts the server. Any currently-registered services will be installed
37 // into the pRPC Server. 36 // into the pRPC Server.
38 func (s *Server) Start(c context.Context) { 37 func (s *Server) Start(c context.Context) {
39 // Clean up any active server. 38 // Clean up any active server.
40 s.Close() 39 s.Close()
41 40
42 s.Authenticator = auth.Authenticator{} 41 s.Authenticator = auth.Authenticator{}
43 mwb := s.Base 42 mwb := s.Base
44 if mwb == nil { 43 if mwb == nil {
45 » » mwb = middleware.TestingBase 44 » » mwb = router.TestingBase
46 } 45 }
47 46
48 » r := httprouter.New() 47 » r := router.New()
49 » s.InstallHandlers(r, mwb(c)) 48 » s.InstallHandlers(r, mwb(c)())
50 s.HTTP = httptest.NewServer(r) 49 s.HTTP = httptest.NewServer(r)
51 } 50 }
52 51
53 // NewClient returns a prpc.Client configured to use the Server. 52 // NewClient returns a prpc.Client configured to use the Server.
54 func (s *Server) NewClient() (*prpcCommon.Client, error) { 53 func (s *Server) NewClient() (*prpcCommon.Client, error) {
55 if s.HTTP == nil { 54 if s.HTTP == nil {
56 return nil, errors.New("not running") 55 return nil, errors.New("not running")
57 } 56 }
58 57
59 u, err := url.Parse(s.HTTP.URL) 58 u, err := url.Parse(s.HTTP.URL)
(...skipping 10 matching lines...) Expand all
70 } 69 }
71 70
72 // Close closes the Server, releasing any retained resources. 71 // Close closes the Server, releasing any retained resources.
73 func (s *Server) Close() { 72 func (s *Server) Close() {
74 if s.HTTP != nil { 73 if s.HTTP != nil {
75 s.HTTP.Close() 74 s.HTTP.Close()
76 75
77 s.HTTP = nil 76 s.HTTP = nil
78 } 77 }
79 } 78 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698