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

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

Issue 1638493004: server/prpc: updated server according to protocol (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: 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
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 contentType: "text/html",
37 }
38 r.write(c, rec)
39 So(rec.Code, ShouldEqual, http.StatusOK)
40 So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, " 0")
41 So(rec.Header().Get(headerContentType), ShouldEqual, "te xt/html")
42 So(rec.Body.String(), ShouldEqual, "hi")
43 })
44
45 Convey("ok newline", func() {
46 r := response{
47 body: []byte("hi"),
48 newLine: true,
49 }
50 r.write(c, rec)
51 So(rec.Code, ShouldEqual, http.StatusOK)
52 So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, " 0")
53 So(rec.Header().Get(headerContentType), ShouldEqual, "te xt/plain")
54 So(rec.Body.String(), ShouldEqual, "hi\n")
55 })
56
57 Convey("client error", func() {
58 r := errResponse(codes.NotFound, 0, "not found")
59 r.write(c, rec)
60 So(rec.Code, ShouldEqual, http.StatusNotFound)
61 So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, " 5")
62 So(rec.Header().Get(headerContentType), ShouldEqual, "te xt/plain")
63 So(rec.Body.String(), ShouldEqual, "not found\n")
64 })
65
66 Convey("internal error", func() {
67 r := errResponse(codes.Internal, 0, "errmsg")
68 r.write(c, rec)
69 So(rec.Code, ShouldEqual, http.StatusInternalServerError )
70 So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, " 13")
71 So(rec.Header().Get(headerContentType), ShouldEqual, "te xt/plain")
72 So(rec.Body.String(), ShouldEqual, "Internal Server Erro r\n")
73 So(log, ShouldHaveLog, logging.Error, "Internal error: e rrmsg")
74 })
75
76 Convey("unknown error", func() {
77 r := errResponse(codes.Unknown, 0, "errmsg")
78 r.write(c, rec)
79 So(rec.Code, ShouldEqual, http.StatusInternalServerError )
80 So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, " 2")
81 So(rec.Header().Get(headerContentType), ShouldEqual, "te xt/plain")
82 So(rec.Body.String(), ShouldEqual, "Internal Server Erro r\n")
83 So(log, ShouldHaveLog, logging.Error, "Unknown error: er rmsg")
84 })
85 })
86 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698