| 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 monorail |
| 6 |
| 7 import ( |
| 8 "encoding/json" |
| 9 "fmt" |
| 10 "net/http" |
| 11 "time" |
| 12 "net/http/httptest" |
| 13 "testing" |
| 14 |
| 15 "github.com/luci/luci-go/common/errors" |
| 16 "golang.org/x/net/context" |
| 17 |
| 18 . "github.com/smartystreets/goconvey/convey" |
| 19 . "github.com/luci/luci-go/common/testing/assertions" |
| 20 ) |
| 21 |
| 22 func TestEndpointsClient(t *testing.T) { |
| 23 t.Parallel() |
| 24 |
| 25 Convey("Endpoints client", t, func() { |
| 26 ctx := context.Background() |
| 27 |
| 28 Convey("Insert issue request succeeds", func(c C) { |
| 29 req := &InsertIssueRequest{ |
| 30 ProjectId: "chromium", |
| 31 Issue: &Issue{ |
| 32 Summary: "Write tests for monorail c
lient", |
| 33 Author: &AtomPerson{"seanmccullough
@chromium.org"}, |
| 34 Owner: &AtomPerson{"nodir@chromium
.org"}, |
| 35 Status: StatusStarted, |
| 36 Cc: []*AtomPerson{{"agable@chro
mium.org"}}, |
| 37 Description: "We should keep our code co
verage high, so write tests", |
| 38 Components: []string{"Infra"}, |
| 39 Labels: []string{"M-53"}, |
| 40 }, |
| 41 } |
| 42 |
| 43 res := &InsertIssueResponse{ |
| 44 Issue: &Issue{}, |
| 45 } |
| 46 *res.Issue = *req.Issue |
| 47 res.Issue.Id = 1 |
| 48 |
| 49 var insertIssueServer *httptest.Server |
| 50 insertIssueServer = httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) { |
| 51 c.So(r.URL.String(), ShouldEqual, "/projects/chr
omium/issues?sendEmail=false") |
| 52 |
| 53 actualReq := &Issue{} |
| 54 err := json.NewDecoder(r.Body).Decode(actualReq) |
| 55 c.So(err, ShouldBeNil) |
| 56 c.So(actualReq, ShouldResemble, req.Issue) |
| 57 |
| 58 err = json.NewEncoder(w).Encode(res.Issue) |
| 59 c.So(err, ShouldBeNil) |
| 60 })) |
| 61 defer insertIssueServer.Close() |
| 62 |
| 63 httpClient := &http.Client{Timeout:time.Second} |
| 64 client := NewEndpointsClient(httpClient, insertIssueServ
er.URL) |
| 65 actualRes, err := client.InsertIssue(ctx, req) |
| 66 So(err, ShouldBeNil) |
| 67 So(actualRes, ShouldResemble, res) |
| 68 }) |
| 69 |
| 70 Convey("Insert issue with invalid request", func(c C) { |
| 71 req := &InsertIssueRequest{ |
| 72 Issue: &Issue{ |
| 73 Summary: "Write tests for monorail c
lient", |
| 74 Author: &AtomPerson{"seanmccullough
@chromium.org"}, |
| 75 Owner: &AtomPerson{"nodir@chromium
.org"}, |
| 76 Status: StatusStarted, |
| 77 }, |
| 78 } |
| 79 |
| 80 httpClient := &http.Client{Timeout:time.Second} |
| 81 client := NewEndpointsClient(httpClient, "https://exampl
e.com") |
| 82 _, err := client.InsertIssue(ctx, req) |
| 83 So(err, ShouldErrLike, "no projectId") |
| 84 }) |
| 85 |
| 86 Convey("Insert comment request", func(c C) { |
| 87 req := &InsertCommentRequest{ |
| 88 Issue: &IssueRef{ |
| 89 ProjectId: "chromium", |
| 90 IssueId: 1, |
| 91 }, |
| 92 Comment: &InsertCommentRequest_Comment{ |
| 93 Content: "Done", |
| 94 Updates: &Update{ |
| 95 Status: StatusFixed, |
| 96 }, |
| 97 }, |
| 98 } |
| 99 |
| 100 Convey("Succeeds", func() { |
| 101 |
| 102 var serv *httptest.Server |
| 103 serv = httptest.NewServer(http.HandlerFunc(func(
w http.ResponseWriter, r *http.Request) { |
| 104 c.So(r.URL.String(), ShouldEqual, "/proj
ects/chromium/issues/1/comments") |
| 105 |
| 106 actualReq := &InsertCommentRequest_Comme
nt{} |
| 107 err := json.NewDecoder(r.Body).Decode(ac
tualReq) |
| 108 c.So(err, ShouldBeNil) |
| 109 c.So(actualReq, ShouldResemble, req.Comm
ent) |
| 110 |
| 111 fmt.Fprint(w, "{}") |
| 112 })) |
| 113 defer serv.Close() |
| 114 |
| 115 client := NewEndpointsClient(nil, serv.URL) |
| 116 _, err := client.InsertComment(ctx, req) |
| 117 So(err, ShouldBeNil) |
| 118 }) |
| 119 |
| 120 Convey("Transient error", func(c C) { |
| 121 test := func(status int) { |
| 122 var serv *httptest.Server |
| 123 serv = httptest.NewServer(http.HandlerFu
nc(func(w http.ResponseWriter, r *http.Request) { |
| 124 w.WriteHeader(status) |
| 125 })) |
| 126 defer serv.Close() |
| 127 |
| 128 client := NewEndpointsClient(nil, serv.U
RL) |
| 129 _, err := client.InsertComment(ctx, req) |
| 130 So(err, ShouldNotBeNil) |
| 131 So(errors.IsTransient(err), ShouldBeTrue
) |
| 132 } |
| 133 Convey("With HTTP 404", func() { |
| 134 test(404) |
| 135 }) |
| 136 Convey("With HTTP 503", func() { |
| 137 test(503) |
| 138 }) |
| 139 }) |
| 140 }) |
| 141 }) |
| 142 } |
| OLD | NEW |