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

Side by Side Diff: server/prpc/server_test.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 prpc 5 package prpc
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "net/http" 9 "net/http"
10 "net/http/httptest" 10 "net/http/httptest"
11 "strconv" 11 "strconv"
12 "testing" 12 "testing"
13 13
14 "github.com/julienschmidt/httprouter"
15 "golang.org/x/net/context" 14 "golang.org/x/net/context"
16 "google.golang.org/grpc" 15 "google.golang.org/grpc"
17 "google.golang.org/grpc/codes" 16 "google.golang.org/grpc/codes"
18 17
19 "github.com/luci/luci-go/common/prpc" 18 "github.com/luci/luci-go/common/prpc"
20 prpccommon "github.com/luci/luci-go/common/prpc" 19 prpccommon "github.com/luci/luci-go/common/prpc"
21 "github.com/luci/luci-go/server/auth" 20 "github.com/luci/luci-go/server/auth"
22 » "github.com/luci/luci-go/server/middleware" 21 » "github.com/luci/luci-go/server/router"
23 22
24 . "github.com/smartystreets/goconvey/convey" 23 . "github.com/smartystreets/goconvey/convey"
25 ) 24 )
26 25
27 type greeterService struct{} 26 type greeterService struct{}
28 27
29 func (s *greeterService) SayHello(c context.Context, req *HelloRequest) (*HelloR eply, error) { 28 func (s *greeterService) SayHello(c context.Context, req *HelloRequest) (*HelloR eply, error) {
30 if req.Name == "" { 29 if req.Name == "" {
31 return nil, grpc.Errorf(codes.InvalidArgument, "Name unspecified ") 30 return nil, grpc.Errorf(codes.InvalidArgument, "Name unspecified ")
32 } 31 }
(...skipping 25 matching lines...) Expand all
58 Convey("Register Calc service", func() { 57 Convey("Register Calc service", func() {
59 RegisterCalcServer(&server, &calcService{}) 58 RegisterCalcServer(&server, &calcService{})
60 So(server.ServiceNames(), ShouldResemble, []string{ 59 So(server.ServiceNames(), ShouldResemble, []string{
61 "prpc.Calc", 60 "prpc.Calc",
62 "prpc.Greeter", 61 "prpc.Greeter",
63 }) 62 })
64 }) 63 })
65 64
66 Convey("Handlers", func() { 65 Convey("Handlers", func() {
67 c := context.Background() 66 c := context.Background()
68 » » » r := httprouter.New() 67 » » » r := router.New()
69 » » » server.InstallHandlers(r, middleware.TestingBase(c)) 68 » » » server.InstallHandlers(r, []router.Handler{router.Handle rWithContext(c)})
70 res := httptest.NewRecorder() 69 res := httptest.NewRecorder()
71 hiMsg := bytes.NewBufferString(`name: "Lucy"`) 70 hiMsg := bytes.NewBufferString(`name: "Lucy"`)
72 req, err := http.NewRequest("POST", "/prpc/prpc.Greeter/ SayHello", hiMsg) 71 req, err := http.NewRequest("POST", "/prpc/prpc.Greeter/ SayHello", hiMsg)
73 So(err, ShouldBeNil) 72 So(err, ShouldBeNil)
74 req.Header.Set("Content-Type", mtPRPCText) 73 req.Header.Set("Content-Type", mtPRPCText)
75 74
76 invalidArgument := strconv.Itoa(int(codes.InvalidArgumen t)) 75 invalidArgument := strconv.Itoa(int(codes.InvalidArgumen t))
77 unimplemented := strconv.Itoa(int(codes.Unimplemented)) 76 unimplemented := strconv.Itoa(int(codes.Unimplemented))
78 77
79 Convey("Works", func() { 78 Convey("Works", func() {
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 r.ServeHTTP(res, req) 172 r.ServeHTTP(res, req)
174 So(res.Code, ShouldEqual, http.S tatusOK) 173 So(res.Code, ShouldEqual, http.S tatusOK)
175 So(res.Header().Get(prpc.HeaderG RPCCode), ShouldEqual, "0") 174 So(res.Header().Get(prpc.HeaderG RPCCode), ShouldEqual, "0")
176 So(res.Header().Get("Access-Cont rol-Allow-Origin"), ShouldEqual, "") 175 So(res.Header().Get("Access-Cont rol-Allow-Origin"), ShouldEqual, "")
177 }) 176 })
178 }) 177 })
179 }) 178 })
180 }) 179 })
181 }) 180 })
182 } 181 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698