OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // 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 "testing" | 12 "testing" |
12 | 13 |
13 "github.com/julienschmidt/httprouter" | 14 "github.com/julienschmidt/httprouter" |
14 "golang.org/x/net/context" | 15 "golang.org/x/net/context" |
15 "google.golang.org/grpc" | 16 "google.golang.org/grpc" |
16 "google.golang.org/grpc/codes" | 17 "google.golang.org/grpc/codes" |
17 | 18 |
| 19 "github.com/luci/luci-go/common/prpc" |
18 "github.com/luci/luci-go/server/middleware" | 20 "github.com/luci/luci-go/server/middleware" |
19 | 21 |
20 . "github.com/luci/luci-go/common/testing/assertions" | 22 . "github.com/luci/luci-go/common/testing/assertions" |
21 . "github.com/smartystreets/goconvey/convey" | 23 . "github.com/smartystreets/goconvey/convey" |
22 ) | 24 ) |
23 | 25 |
24 type greeterService struct{} | 26 type greeterService struct{} |
25 | 27 |
26 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) { |
27 if req.Name == "" { | 29 if req.Name == "" { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 "prpc.Greeter", | 61 "prpc.Greeter", |
60 }) | 62 }) |
61 }) | 63 }) |
62 | 64 |
63 Convey("Handlers", func() { | 65 Convey("Handlers", func() { |
64 c := context.Background() | 66 c := context.Background() |
65 r := httprouter.New() | 67 r := httprouter.New() |
66 server.InstallHandlers(r, middleware.TestingBase(c)) | 68 server.InstallHandlers(r, middleware.TestingBase(c)) |
67 res := httptest.NewRecorder() | 69 res := httptest.NewRecorder() |
68 hiMsg := bytes.NewBufferString(`name: "Lucy"`) | 70 hiMsg := bytes.NewBufferString(`name: "Lucy"`) |
69 » » » req, err := http.NewRequest("POST", "http://localhost/pr
pc/prpc.Greeter/SayHello", hiMsg) | 71 » » » req, err := http.NewRequest("POST", "/prpc/prpc.Greeter/
SayHello", hiMsg) |
70 So(err, ShouldBeNil) | 72 So(err, ShouldBeNil) |
71 req.Header.Set("Content-Type", mtPRPCText) | 73 req.Header.Set("Content-Type", mtPRPCText) |
72 | 74 |
| 75 invalidArgument := strconv.Itoa(int(codes.InvalidArgumen
t)) |
| 76 unimplemented := strconv.Itoa(int(codes.Unimplemented)) |
| 77 |
73 Convey("Works", func() { | 78 Convey("Works", func() { |
74 req.Header.Set("Accept", mtPRPCText) | 79 req.Header.Set("Accept", mtPRPCText) |
75 r.ServeHTTP(res, req) | 80 r.ServeHTTP(res, req) |
76 So(res.Code, ShouldEqual, http.StatusOK) | 81 So(res.Code, ShouldEqual, http.StatusOK) |
| 82 So(res.Header().Get(prpc.HeaderGRPCCode), Should
Equal, "0") |
77 So(res.Body.String(), ShouldEqual, "message: \"H
ello Lucy\"\n") | 83 So(res.Body.String(), ShouldEqual, "message: \"H
ello Lucy\"\n") |
78 }) | 84 }) |
79 | 85 |
80 Convey("Invalid Accept header", func() { | 86 Convey("Invalid Accept header", func() { |
81 req.Header.Set("Accept", "blah") | 87 req.Header.Set("Accept", "blah") |
82 r.ServeHTTP(res, req) | 88 r.ServeHTTP(res, req) |
83 So(res.Code, ShouldEqual, http.StatusNotAcceptab
le) | 89 So(res.Code, ShouldEqual, http.StatusNotAcceptab
le) |
| 90 So(res.Header().Get(prpc.HeaderGRPCCode), Should
Equal, invalidArgument) |
84 }) | 91 }) |
85 | 92 |
86 Convey("Invalid header", func() { | 93 Convey("Invalid header", func() { |
87 req.Header.Set("X-Bin", "zzz") | 94 req.Header.Set("X-Bin", "zzz") |
88 r.ServeHTTP(res, req) | 95 r.ServeHTTP(res, req) |
89 So(res.Code, ShouldEqual, http.StatusBadRequest) | 96 So(res.Code, ShouldEqual, http.StatusBadRequest) |
| 97 So(res.Header().Get(prpc.HeaderGRPCCode), Should
Equal, invalidArgument) |
90 }) | 98 }) |
91 | 99 |
92 Convey("Malformed request message", func() { | 100 Convey("Malformed request message", func() { |
93 hiMsg.WriteString("\nblah") | 101 hiMsg.WriteString("\nblah") |
94 r.ServeHTTP(res, req) | 102 r.ServeHTTP(res, req) |
95 So(res.Code, ShouldEqual, http.StatusBadRequest) | 103 So(res.Code, ShouldEqual, http.StatusBadRequest) |
| 104 So(res.Header().Get(prpc.HeaderGRPCCode), Should
Equal, invalidArgument) |
96 }) | 105 }) |
97 | 106 |
98 Convey("Invalid request message", func() { | 107 Convey("Invalid request message", func() { |
99 hiMsg.Reset() | 108 hiMsg.Reset() |
100 r.ServeHTTP(res, req) | 109 r.ServeHTTP(res, req) |
101 So(res.Code, ShouldEqual, http.StatusBadRequest) | 110 So(res.Code, ShouldEqual, http.StatusBadRequest) |
| 111 So(res.Header().Get(prpc.HeaderGRPCCode), Should
Equal, invalidArgument) |
102 So(res.Body.String(), ShouldEqual, "Name unspeci
fied\n") | 112 So(res.Body.String(), ShouldEqual, "Name unspeci
fied\n") |
103 }) | 113 }) |
| 114 |
| 115 Convey("no such service", func() { |
| 116 req.URL.Path = "/prpc/xxx/SayHello" |
| 117 r.ServeHTTP(res, req) |
| 118 So(res.Code, ShouldEqual, http.StatusNotImplemen
ted) |
| 119 So(res.Header().Get(prpc.HeaderGRPCCode), Should
Equal, unimplemented) |
| 120 }) |
| 121 Convey("no such method", func() { |
| 122 req.URL.Path = "/prpc/prpc.Greeter/xxx" |
| 123 r.ServeHTTP(res, req) |
| 124 So(res.Code, ShouldEqual, http.StatusNotImplemen
ted) |
| 125 So(res.Header().Get(prpc.HeaderGRPCCode), Should
Equal, unimplemented) |
| 126 }) |
104 }) | 127 }) |
105 }) | 128 }) |
106 } | 129 } |
OLD | NEW |