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

Unified Diff: go/src/infra/monorail/endpoints_test.go

Issue 2037143002: Monorail client in Go (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: more tests Created 4 years, 6 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 | « go/src/infra/monorail/endpoints.go ('k') | go/src/infra/monorail/ext.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/src/infra/monorail/endpoints_test.go
diff --git a/go/src/infra/monorail/endpoints_test.go b/go/src/infra/monorail/endpoints_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..aed6e80652bb1d18e3310c6058e04f6be0a96b94
--- /dev/null
+++ b/go/src/infra/monorail/endpoints_test.go
@@ -0,0 +1,142 @@
+// 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 monorail
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "time"
+ "net/http/httptest"
+ "testing"
+
+ "github.com/luci/luci-go/common/errors"
+ "golang.org/x/net/context"
+
+ . "github.com/smartystreets/goconvey/convey"
+ . "github.com/luci/luci-go/common/testing/assertions"
+)
+
+func TestEndpointsClient(t *testing.T) {
+ t.Parallel()
+
+ Convey("Endpoints client", t, func() {
+ ctx := context.Background()
+
+ Convey("Insert issue request succeeds", func(c C) {
+ req := &InsertIssueRequest{
+ ProjectId: "chromium",
+ Issue: &Issue{
+ Summary: "Write tests for monorail client",
+ Author: &AtomPerson{"seanmccullough@chromium.org"},
+ Owner: &AtomPerson{"nodir@chromium.org"},
+ Status: StatusStarted,
+ Cc: []*AtomPerson{{"agable@chromium.org"}},
+ Description: "We should keep our code coverage high, so write tests",
+ Components: []string{"Infra"},
+ Labels: []string{"M-53"},
+ },
+ }
+
+ res := &InsertIssueResponse{
+ Issue: &Issue{},
+ }
+ *res.Issue = *req.Issue
+ res.Issue.Id = 1
+
+ var insertIssueServer *httptest.Server
+ insertIssueServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ c.So(r.URL.String(), ShouldEqual, "/projects/chromium/issues?sendEmail=false")
+
+ actualReq := &Issue{}
+ err := json.NewDecoder(r.Body).Decode(actualReq)
+ c.So(err, ShouldBeNil)
+ c.So(actualReq, ShouldResemble, req.Issue)
+
+ err = json.NewEncoder(w).Encode(res.Issue)
+ c.So(err, ShouldBeNil)
+ }))
+ defer insertIssueServer.Close()
+
+ httpClient := &http.Client{Timeout:time.Second}
+ client := NewEndpointsClient(httpClient, insertIssueServer.URL)
+ actualRes, err := client.InsertIssue(ctx, req)
+ So(err, ShouldBeNil)
+ So(actualRes, ShouldResemble, res)
+ })
+
+ Convey("Insert issue with invalid request", func(c C) {
+ req := &InsertIssueRequest{
+ Issue: &Issue{
+ Summary: "Write tests for monorail client",
+ Author: &AtomPerson{"seanmccullough@chromium.org"},
+ Owner: &AtomPerson{"nodir@chromium.org"},
+ Status: StatusStarted,
+ },
+ }
+
+ httpClient := &http.Client{Timeout:time.Second}
+ client := NewEndpointsClient(httpClient, "https://example.com")
+ _, err := client.InsertIssue(ctx, req)
+ So(err, ShouldErrLike, "no projectId")
+ })
+
+ Convey("Insert comment request", func(c C) {
+ req := &InsertCommentRequest{
+ Issue: &IssueRef{
+ ProjectId: "chromium",
+ IssueId: 1,
+ },
+ Comment: &InsertCommentRequest_Comment{
+ Content: "Done",
+ Updates: &Update{
+ Status: StatusFixed,
+ },
+ },
+ }
+
+ Convey("Succeeds", func() {
+
+ var serv *httptest.Server
+ serv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ c.So(r.URL.String(), ShouldEqual, "/projects/chromium/issues/1/comments")
+
+ actualReq := &InsertCommentRequest_Comment{}
+ err := json.NewDecoder(r.Body).Decode(actualReq)
+ c.So(err, ShouldBeNil)
+ c.So(actualReq, ShouldResemble, req.Comment)
+
+ fmt.Fprint(w, "{}")
+ }))
+ defer serv.Close()
+
+ client := NewEndpointsClient(nil, serv.URL)
+ _, err := client.InsertComment(ctx, req)
+ So(err, ShouldBeNil)
+ })
+
+ Convey("Transient error", func(c C) {
+ test := func(status int) {
+ var serv *httptest.Server
+ serv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(status)
+ }))
+ defer serv.Close()
+
+ client := NewEndpointsClient(nil, serv.URL)
+ _, err := client.InsertComment(ctx, req)
+ So(err, ShouldNotBeNil)
+ So(errors.IsTransient(err), ShouldBeTrue)
+ }
+ Convey("With HTTP 404", func() {
+ test(404)
+ })
+ Convey("With HTTP 503", func() {
+ test(503)
+ })
+ })
+ })
+ })
+}
« no previous file with comments | « go/src/infra/monorail/endpoints.go ('k') | go/src/infra/monorail/ext.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698