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

Side by Side Diff: server/prpc/response_test.go

Issue 1605363002: common/prpc, tools/cmd/cproto: prpc client (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: rebased and addressed comments Created 4 years, 11 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
« no previous file with comments | « server/prpc/response.go ('k') | server/prpc/server.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package prpc
6
7 import (
8 "net/http"
9 "net/http/httptest"
10 "testing"
11
12 "golang.org/x/net/context"
13 "google.golang.org/grpc/codes"
14
15 "github.com/luci/luci-go/common/logging"
16 "github.com/luci/luci-go/common/logging/memlogger"
17 "github.com/luci/luci-go/common/prpc"
18
19 . "github.com/luci/luci-go/common/logging/memlogger"
20 . "github.com/smartystreets/goconvey/convey"
21 )
22
23 func TestResponse(t *testing.T) {
24 t.Parallel()
25
26 Convey("response", t, func() {
27 c := context.Background()
28 c = memlogger.Use(c)
29 log := logging.Get(c).(*memlogger.MemLogger)
30
31 rec := httptest.NewRecorder()
32
33 Convey("ok", func() {
34 r := response{
35 body: []byte("hi"),
36 header: http.Header{
37 headerContentType: []string{"text/html"} ,
38 },
39 }
40 r.write(c, rec)
41 So(rec.Code, ShouldEqual, http.StatusOK)
42 So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, " 0")
43 So(rec.Header().Get(headerContentType), ShouldEqual, "te xt/html")
44 So(rec.Body.String(), ShouldEqual, "hi")
45 })
46
47 Convey("ok newline", func() {
48 r := response{
49 body: []byte("hi"),
50 newLine: true,
51 }
52 r.write(c, rec)
53 So(rec.Code, ShouldEqual, http.StatusOK)
54 So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, " 0")
55 So(rec.Header().Get(headerContentType), ShouldEqual, "")
56 So(rec.Body.String(), ShouldEqual, "hi\n")
57 })
58
59 Convey("client error", func() {
60 r := errResponse(codes.NotFound, 0, "not found")
61 r.write(c, rec)
62 So(rec.Code, ShouldEqual, http.StatusNotFound)
63 So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, " 5")
64 So(rec.Header().Get(headerContentType), ShouldEqual, "te xt/plain")
65 So(rec.Body.String(), ShouldEqual, "not found\n")
66 })
67
68 Convey("internal error", func() {
69 r := errResponse(codes.Internal, 0, "errmsg")
70 r.write(c, rec)
71 So(rec.Code, ShouldEqual, http.StatusInternalServerError )
72 So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, " 13")
73 So(rec.Header().Get(headerContentType), ShouldEqual, "te xt/plain")
74 So(rec.Body.String(), ShouldEqual, "Internal Server Erro r\n")
75 So(log, ShouldHaveLog, logging.Error, "errmsg", map[stri ng]interface{}{
76 "code": codes.Internal,
77 })
78 })
79
80 Convey("unknown error", func() {
81 r := errResponse(codes.Unknown, 0, "errmsg")
82 r.write(c, rec)
83 So(rec.Code, ShouldEqual, http.StatusInternalServerError )
84 So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, " 2")
85 So(rec.Header().Get(headerContentType), ShouldEqual, "te xt/plain")
86 So(rec.Body.String(), ShouldEqual, "Internal Server Erro r\n")
87 So(log, ShouldHaveLog, logging.Error, "errmsg", map[stri ng]interface{}{
88 "code": codes.Unknown,
89 })
90 })
91 })
92 }
OLDNEW
« no previous file with comments | « server/prpc/response.go ('k') | server/prpc/server.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698