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

Side by Side Diff: common/api/gitiles/gitiles_test.go

Issue 2983513002: gitiles.Log: implement paging. (Closed)
Patch Set: review Created 3 years, 5 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 unified diff | Download patch
OLDNEW
(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 "crypto/tls"
19 "crypto/x509"
20 "fmt"
21 "net/http"
22 "net/http/httptest"
23 "net/url"
24 "testing"
25
26 "golang.org/x/net/context"
27
28 "github.com/luci/luci-go/server/auth"
29 . "github.com/smartystreets/goconvey/convey"
30 )
31
32 func TestRepoURL(t *testing.T) {
33 t.Parallel()
34 Convey("Malformed", t, func() {
35 f := func(arg string) {
36 So(ValidateRepoURL(arg), ShouldNotBeNil)
37 _, err := NormalizeRepoURL(arg)
38 So(err, ShouldNotBeNil)
39 }
40
41 f("wtf/\\is\this")
42 f("https://example.com/repo.git")
43 f("http://bad-protocol.googlesource.com/repo.git")
44 f("https://a.googlesource.com")
45 f("https://a.googlesource.com/")
Vadim Sh. 2017/07/19 23:02:31 check also how it reacts to #fragments and unspeci
tandrii(chromium) 2017/07/20 16:19:15 Done.
46 })
47
48 Convey("OK", t, func() {
49 f := func(arg, exp string) {
50 So(ValidateRepoURL(arg), ShouldBeNil)
51 act, err := NormalizeRepoURL(arg)
52 So(err, ShouldBeNil)
53 So(act, ShouldEqual, exp)
54 }
55
56 f("https://chromium.googlesource.com/repo.git",
57 "https://chromium.googlesource.com/a/repo")
58 f("https://chromium.googlesource.com/repo/",
59 "https://chromium.googlesource.com/a/repo")
60 f("https://chromium.googlesource.com/a/repo",
61 "https://chromium.googlesource.com/a/repo")
62 f("https://chromium.googlesource.com/parent/repo.git/",
63 "https://chromium.googlesource.com/a/parent/repo")
64 })
65 }
66
67 func TestLog(t *testing.T) {
68 t.Parallel()
69 ctx := context.Background()
70
71 Convey("Log Bad Repo", t, func() {
72 _, err := Log(context.Background(), "bad://repo.url", "master", 10)
73 So(err, ShouldNotBeNil)
74 })
75
76 Convey("Log w/o pages", t, func() {
77 srv, ctx := newMockHttps(ctx, func(w http.ResponseWriter, r *htt p.Request) {
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 := Log(ctx, "https://chromium.googlesource. com/repo.git",
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 := Log(ctx, "https://chromium.googlesource. com/repo.git",
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, ctx := newMockHttps(ctx, func(w http.ResponseWriter, r *htt p.Request) {
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 := Log(ctx, "https://chromium.googlesource. com/repo.git",
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 := Log(ctx, "https://chromium.googlesource. com/repo.git", "master", 1)
128 So(err, ShouldBeNil)
129 So(cursor_sent, ShouldEqual, "")
130 So(len(commits), ShouldEqual, 1)
131 So(commits[0].Author.Name, ShouldEqual, "Author 1")
132 })
133 })
134 }
135
136 ////////////////////////////////////////////////////////////////////////////////
137
138 var (
139 fake_commit1_str = `{
140 "commit": "0b2c5409e58a71c691b05454b55cc5580cc822d1",
141 "tree": "3c6f95bc757698cd6aca3c49f88f640fd145ea69",
142 "parents": [ "dc1dbf1aa56e4dd4cbfaab61c4d30a35adce5f40" ],
143 "author": {
144 "name": "Author 1",
145 "email": "author1@example.com",
146 "time": "Mon Jul 17 15:02:43 2017"
147 },
148 "committer": {
149 "name": "Commit Bot",
150 "email": "commit-bot@chromium.org",
151 "time": "Mon Jul 17 15:02:43 2017"
152 },
153 "message": "Import wpt@d96d68ed964f9bfc2bb248c2d2fab7a8870dc685\ \n\\nCr-Commit-Position: refs/heads/master@{#487078}"
154 }`
155 fake_commit2_str = `{
156 "commit": "dc1dbf1aa56e4dd4cbfaab61c4d30a35adce5f40",
157 "tree": "1ba2335c07915c31597b97a8d824aecc85a996f6",
158 "parents": ["8de6836858c99e48f3c58164ab717bda728e95dd"],
159 "author": {
160 "name": "Author 2",
161 "email": "author-2@example.com",
162 "time": "Mon Jul 17 15:01:13 2017"
163 },
164 "committer": {
165 "name": "Commit Bot",
166 "email": "commit-bot@chromium.org",
167 "time": "Mon Jul 17 15:01:13 2017"
168 },
169 "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}"
170 }`
171 )
172
173 ////////////////////////////////////////////////////////////////////////////////
174
175 func newMockHttps(ctx context.Context, handler func(w http.ResponseWriter, r *ht tp.Request)) (*httptest.Server, context.Context) {
176 // TODO(<REVIEWER>): any easier/better way to do this? I couldn't find a nything inside luci-go...
Vadim Sh. 2017/07/19 23:02:31 client := Client{ mockedGitiles : srv.URL } fun
tandrii(chromium) 2017/07/20 16:19:15 Thanks, I've added RepoURL into gitiles client.
177 srv := httptest.NewTLSServer(http.HandlerFunc(handler))
178 parsedServerUrl, err := url.Parse(srv.URL)
179 So(err, ShouldBeNil)
180 cert, err := x509.ParseCertificate(srv.TLS.Certificates[0].Certificate[0 ])
181 So(err, ShouldBeNil)
182 certpool := x509.NewCertPool()
183 certpool.AddCert(cert)
184
185 ctx = auth.ModifyConfig(ctx, func(cfg auth.Config) auth.Config {
186 cfg.AnonymousTransport = func(context.Context) http.RoundTripper {
187 return &httpsForwarder{
188 Transport: &http.Transport{TLSClientConfig: &tls .Config{RootCAs: certpool}},
189 URL: parsedServerUrl,
190 }
191 }
192 return cfg
193 })
194 return srv, ctx
195 }
196
197 type httpsForwarder struct {
198 Transport http.RoundTripper
199 URL *url.URL
200 }
201
202 func (t httpsForwarder) RoundTrip(req *http.Request) (*http.Response, error) {
203 req.URL.Scheme = t.URL.Scheme
204 req.URL.Host = t.URL.Host
205 return t.Transport.RoundTrip(req)
206 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698