| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 package gitiles |
| 16 |
| 17 import ( |
| 18 "fmt" |
| 19 "net/http" |
| 20 "net/http/httptest" |
| 21 "testing" |
| 22 |
| 23 "golang.org/x/net/context" |
| 24 |
| 25 . "github.com/smartystreets/goconvey/convey" |
| 26 ) |
| 27 |
| 28 func TestRepoURL(t *testing.T) { |
| 29 t.Parallel() |
| 30 Convey("Malformed", t, func() { |
| 31 f := func(arg string) { |
| 32 So(ValidateRepoURL(arg), ShouldNotBeNil) |
| 33 _, err := NormalizeRepoURL(arg) |
| 34 So(err, ShouldNotBeNil) |
| 35 } |
| 36 |
| 37 f("wtf/\\is\this") |
| 38 f("https://example.com/repo.git") |
| 39 f("http://bad-protocol.googlesource.com/repo.git") |
| 40 f("https://a.googlesource.com") |
| 41 f("https://a.googlesource.com/") |
| 42 f("a.googlesource.com/no-protocol.git") |
| 43 f("https://a.googlesource.com/no-protocol#fragment") |
| 44 }) |
| 45 |
| 46 Convey("OK", t, func() { |
| 47 f := func(arg, exp string) { |
| 48 So(ValidateRepoURL(arg), ShouldBeNil) |
| 49 act, err := NormalizeRepoURL(arg) |
| 50 So(err, ShouldBeNil) |
| 51 So(act, ShouldEqual, exp) |
| 52 } |
| 53 |
| 54 f("https://chromium.googlesource.com/repo.git", |
| 55 "https://chromium.googlesource.com/a/repo") |
| 56 f("https://chromium.googlesource.com/repo/", |
| 57 "https://chromium.googlesource.com/a/repo") |
| 58 f("https://chromium.googlesource.com/a/repo", |
| 59 "https://chromium.googlesource.com/a/repo") |
| 60 f("https://chromium.googlesource.com/parent/repo.git/", |
| 61 "https://chromium.googlesource.com/a/parent/repo") |
| 62 }) |
| 63 } |
| 64 |
| 65 func TestLog(t *testing.T) { |
| 66 t.Parallel() |
| 67 ctx := context.Background() |
| 68 |
| 69 Convey("Log with bad URL", t, func() { |
| 70 srv, c := newMockClient(func(w http.ResponseWriter, r *http.Requ
est) {}) |
| 71 defer srv.Close() |
| 72 _, err := c.Log(ctx, "bad://repo.git/", "master", 10) |
| 73 So(err, ShouldNotBeNil) |
| 74 }) |
| 75 |
| 76 Convey("Log w/o pages", t, func() { |
| 77 srv, c := newMockClient(func(w http.ResponseWriter, r *http.Requ
est) { |
| 78 w.WriteHeader(200) |
| 79 w.Header().Set("Content-Type", "application/json") |
| 80 fmt.Fprintf(w, ")]}'\n{\"log\": [%s, %s]}\n", fake_commi
t1_str, fake_commit2_str) |
| 81 }) |
| 82 defer srv.Close() |
| 83 |
| 84 Convey("Return All", func() { |
| 85 commits, err := c.Log(ctx, "https://c.googlesource.com/r
epo", |
| 86 "master..8de6836858c99e48f3c58164ab717bda728e95d
d", 10) |
| 87 So(err, ShouldBeNil) |
| 88 So(len(commits), ShouldEqual, 2) |
| 89 So(commits[0].Author.Name, ShouldEqual, "Author 1") |
| 90 So(commits[1].Commit, ShouldEqual, "dc1dbf1aa56e4dd4cbfa
ab61c4d30a35adce5f40") |
| 91 }) |
| 92 |
| 93 Convey("DO not exceed limit", func() { |
| 94 commits, err := c.Log(ctx, "https://c.googlesource.com/r
epo", |
| 95 "master..8de6836858c99e48f3c58164ab717bda728e95d
d", 1) |
| 96 So(err, ShouldBeNil) |
| 97 So(len(commits), ShouldEqual, 1) |
| 98 So(commits[0].Author.Name, ShouldEqual, "Author 1") |
| 99 }) |
| 100 }) |
| 101 |
| 102 Convey("Log Paging", t, func() { |
| 103 cursor_sent := "" |
| 104 srv, c := newMockClient(func(w http.ResponseWriter, r *http.Requ
est) { |
| 105 w.WriteHeader(200) |
| 106 w.Header().Set("Content-Type", "application/json") |
| 107 if next := r.URL.Query().Get("s"); next == "" { |
| 108 fmt.Fprintf(w, ")]}'\n{\"log\": [%s], \"next\":
\"next_cursor_value\"}\n", fake_commit1_str) |
| 109 } else { |
| 110 cursor_sent = next |
| 111 fmt.Fprintf(w, ")]}'\n{\"log\": [%s]}\n", fake_c
ommit2_str) |
| 112 } |
| 113 }) |
| 114 defer srv.Close() |
| 115 |
| 116 Convey("Page till no cursor", func() { |
| 117 commits, err := c.Log(ctx, "https://c.googlesource.com/r
epo", |
| 118 "master..8de6836858c99e48f3c58164ab717bda728e95d
d", 10) |
| 119 So(err, ShouldBeNil) |
| 120 So(cursor_sent, ShouldEqual, "next_cursor_value") |
| 121 So(len(commits), ShouldEqual, 2) |
| 122 So(commits[0].Author.Name, ShouldEqual, "Author 1") |
| 123 So(commits[1].Commit, ShouldEqual, "dc1dbf1aa56e4dd4cbfa
ab61c4d30a35adce5f40") |
| 124 }) |
| 125 |
| 126 Convey("Page till limit", func() { |
| 127 commits, err := c.Log(ctx, "https://c.googlesource.com/r
epo", |
| 128 "master", 1) |
| 129 So(err, ShouldBeNil) |
| 130 So(cursor_sent, ShouldEqual, "") |
| 131 So(len(commits), ShouldEqual, 1) |
| 132 So(commits[0].Author.Name, ShouldEqual, "Author 1") |
| 133 }) |
| 134 }) |
| 135 } |
| 136 |
| 137 //////////////////////////////////////////////////////////////////////////////// |
| 138 |
| 139 var ( |
| 140 fake_commit1_str = `{ |
| 141 "commit": "0b2c5409e58a71c691b05454b55cc5580cc822d1", |
| 142 "tree": "3c6f95bc757698cd6aca3c49f88f640fd145ea69", |
| 143 "parents": [ "dc1dbf1aa56e4dd4cbfaab61c4d30a35adce5f40" ], |
| 144 "author": { |
| 145 "name": "Author 1", |
| 146 "email": "author1@example.com", |
| 147 "time": "Mon Jul 17 15:02:43 2017" |
| 148 }, |
| 149 "committer": { |
| 150 "name": "Commit Bot", |
| 151 "email": "commit-bot@chromium.org", |
| 152 "time": "Mon Jul 17 15:02:43 2017" |
| 153 }, |
| 154 "message": "Import wpt@d96d68ed964f9bfc2bb248c2d2fab7a8870dc685\
\n\\nCr-Commit-Position: refs/heads/master@{#487078}" |
| 155 }` |
| 156 fake_commit2_str = `{ |
| 157 "commit": "dc1dbf1aa56e4dd4cbfaab61c4d30a35adce5f40", |
| 158 "tree": "1ba2335c07915c31597b97a8d824aecc85a996f6", |
| 159 "parents": ["8de6836858c99e48f3c58164ab717bda728e95dd"], |
| 160 "author": { |
| 161 "name": "Author 2", |
| 162 "email": "author-2@example.com", |
| 163 "time": "Mon Jul 17 15:01:13 2017" |
| 164 }, |
| 165 "committer": { |
| 166 "name": "Commit Bot", |
| 167 "email": "commit-bot@chromium.org", |
| 168 "time": "Mon Jul 17 15:01:13 2017" |
| 169 }, |
| 170 "message": "[Web Payments] User weak ptr in Payment Request\u002
7s error callback\\n\\nBug: 742329\\nReviewed-on: https://chromium-review.google
source.com/570982\\nCr-Commit-Position: refs/heads/master@{#487077}" |
| 171 }` |
| 172 ) |
| 173 |
| 174 //////////////////////////////////////////////////////////////////////////////// |
| 175 |
| 176 func newMockClient(handler func(w http.ResponseWriter, r *http.Request)) (*httpt
est.Server, *Client) { |
| 177 srv := httptest.NewServer(http.HandlerFunc(handler)) |
| 178 return srv, &Client{Client: http.DefaultClient, mockRepoURL: srv.URL} |
| 179 } |
| OLD | NEW |