| 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 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 Loading... |
| 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.MiddlewareChain{ |
| 69 » » » » func(ctx *router.Context, next router.Handler) { |
| 70 » » » » » ctx.Context = c |
| 71 » » » » » next(ctx) |
| 72 » » » » }, |
| 73 » » » }) |
| 70 res := httptest.NewRecorder() | 74 res := httptest.NewRecorder() |
| 71 hiMsg := bytes.NewBufferString(`name: "Lucy"`) | 75 hiMsg := bytes.NewBufferString(`name: "Lucy"`) |
| 72 req, err := http.NewRequest("POST", "/prpc/prpc.Greeter/
SayHello", hiMsg) | 76 req, err := http.NewRequest("POST", "/prpc/prpc.Greeter/
SayHello", hiMsg) |
| 73 So(err, ShouldBeNil) | 77 So(err, ShouldBeNil) |
| 74 req.Header.Set("Content-Type", mtPRPCText) | 78 req.Header.Set("Content-Type", mtPRPCText) |
| 75 | 79 |
| 76 invalidArgument := strconv.Itoa(int(codes.InvalidArgumen
t)) | 80 invalidArgument := strconv.Itoa(int(codes.InvalidArgumen
t)) |
| 77 unimplemented := strconv.Itoa(int(codes.Unimplemented)) | 81 unimplemented := strconv.Itoa(int(codes.Unimplemented)) |
| 78 | 82 |
| 79 Convey("Works", func() { | 83 Convey("Works", func() { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 r.ServeHTTP(res, req) | 177 r.ServeHTTP(res, req) |
| 174 So(res.Code, ShouldEqual, http.S
tatusOK) | 178 So(res.Code, ShouldEqual, http.S
tatusOK) |
| 175 So(res.Header().Get(prpc.HeaderG
RPCCode), ShouldEqual, "0") | 179 So(res.Header().Get(prpc.HeaderG
RPCCode), ShouldEqual, "0") |
| 176 So(res.Header().Get("Access-Cont
rol-Allow-Origin"), ShouldEqual, "") | 180 So(res.Header().Get("Access-Cont
rol-Allow-Origin"), ShouldEqual, "") |
| 177 }) | 181 }) |
| 178 }) | 182 }) |
| 179 }) | 183 }) |
| 180 }) | 184 }) |
| 181 }) | 185 }) |
| 182 } | 186 } |
| OLD | NEW |