| OLD | NEW | 
| (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 } | 
| OLD | NEW |