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

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

Issue 1636873006: server/prpc: updated server according to protocol (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Respond to nits, escape externally-supplied strings when passed to format parameters. 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("client error", func() {
48 r := errResponse(codes.NotFound, 0, "not found")
49 r.write(c, rec)
50 So(rec.Code, ShouldEqual, http.StatusNotFound)
51 So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, " 5")
52 So(rec.Header().Get(headerContentType), ShouldEqual, "te xt/plain")
53 So(rec.Body.String(), ShouldEqual, "not found\n")
54 })
55
56 Convey("internal error", func() {
57 r := errResponse(codes.Internal, 0, "errmsg")
58 r.write(c, rec)
59 So(rec.Code, ShouldEqual, http.StatusInternalServerError )
60 So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, " 13")
61 So(rec.Header().Get(headerContentType), ShouldEqual, "te xt/plain")
62 So(rec.Body.String(), ShouldEqual, "Internal Server Erro r\n")
63 So(log, ShouldHaveLog, logging.Error, "errmsg", map[stri ng]interface{}{
64 "code": codes.Internal,
65 })
66 })
67
68 Convey("unknown error", func() {
69 r := errResponse(codes.Unknown, 0, "errmsg")
70 r.write(c, rec)
71 So(rec.Code, ShouldEqual, http.StatusInternalServerError )
72 So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, " 2")
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.Unknown,
77 })
78 })
79 })
80 }
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