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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « server/prpc/response.go ('k') | server/prpc/server.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: server/prpc/response_test.go
diff --git a/server/prpc/response_test.go b/server/prpc/response_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..a5e3209619b4aa0155d3079da72516424ba27e87
--- /dev/null
+++ b/server/prpc/response_test.go
@@ -0,0 +1,92 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package prpc
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+
+ "golang.org/x/net/context"
+ "google.golang.org/grpc/codes"
+
+ "github.com/luci/luci-go/common/logging"
+ "github.com/luci/luci-go/common/logging/memlogger"
+ "github.com/luci/luci-go/common/prpc"
+
+ . "github.com/luci/luci-go/common/logging/memlogger"
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+func TestResponse(t *testing.T) {
+ t.Parallel()
+
+ Convey("response", t, func() {
+ c := context.Background()
+ c = memlogger.Use(c)
+ log := logging.Get(c).(*memlogger.MemLogger)
+
+ rec := httptest.NewRecorder()
+
+ Convey("ok", func() {
+ r := response{
+ body: []byte("hi"),
+ header: http.Header{
+ headerContentType: []string{"text/html"},
+ },
+ }
+ r.write(c, rec)
+ So(rec.Code, ShouldEqual, http.StatusOK)
+ So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, "0")
+ So(rec.Header().Get(headerContentType), ShouldEqual, "text/html")
+ So(rec.Body.String(), ShouldEqual, "hi")
+ })
+
+ Convey("ok newline", func() {
+ r := response{
+ body: []byte("hi"),
+ newLine: true,
+ }
+ r.write(c, rec)
+ So(rec.Code, ShouldEqual, http.StatusOK)
+ So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, "0")
+ So(rec.Header().Get(headerContentType), ShouldEqual, "")
+ So(rec.Body.String(), ShouldEqual, "hi\n")
+ })
+
+ Convey("client error", func() {
+ r := errResponse(codes.NotFound, 0, "not found")
+ r.write(c, rec)
+ So(rec.Code, ShouldEqual, http.StatusNotFound)
+ So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, "5")
+ So(rec.Header().Get(headerContentType), ShouldEqual, "text/plain")
+ So(rec.Body.String(), ShouldEqual, "not found\n")
+ })
+
+ Convey("internal error", func() {
+ r := errResponse(codes.Internal, 0, "errmsg")
+ r.write(c, rec)
+ So(rec.Code, ShouldEqual, http.StatusInternalServerError)
+ So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, "13")
+ So(rec.Header().Get(headerContentType), ShouldEqual, "text/plain")
+ So(rec.Body.String(), ShouldEqual, "Internal Server Error\n")
+ So(log, ShouldHaveLog, logging.Error, "errmsg", map[string]interface{}{
+ "code": codes.Internal,
+ })
+ })
+
+ Convey("unknown error", func() {
+ r := errResponse(codes.Unknown, 0, "errmsg")
+ r.write(c, rec)
+ So(rec.Code, ShouldEqual, http.StatusInternalServerError)
+ So(rec.Header().Get(prpc.HeaderGRPCCode), ShouldEqual, "2")
+ So(rec.Header().Get(headerContentType), ShouldEqual, "text/plain")
+ So(rec.Body.String(), ShouldEqual, "Internal Server Error\n")
+ So(log, ShouldHaveLog, logging.Error, "errmsg", map[string]interface{}{
+ "code": codes.Unknown,
+ })
+ })
+ })
+}
« 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