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

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

Issue 2983513002: gitiles.Log: implement paging. (Closed)
Patch Set: Better doc. 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/")
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 Simple", 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 commits, err := Log(ctx, "https://chromium.googlesource.com/repo .git",
84 "master..8de6836858c99e48f3c58164ab717bda728e95dd", 10)
85 So(err, ShouldBeNil)
86 So(len(commits), ShouldEqual, 2)
87 So(commits[0].Author.Name, ShouldEqual, "Author 1")
88 So(commits[1].Commit, ShouldEqual, "dc1dbf1aa56e4dd4cbfaab61c4d3 0a35adce5f40")
89 })
90
91 Convey("Log Paging", t, func() {
92 cursor_sent := ""
93 srv, ctx := newMockHttps(ctx, func(w http.ResponseWriter, r *htt p.Request) {
94 w.WriteHeader(200)
95 w.Header().Set("Content-Type", "application/json")
96 if next := r.URL.Query().Get("s"); next == "" {
97 fmt.Fprintf(w, ")]}'\n{\"log\": [%s], \"next\": \"next_cursor_value\"}\n", fake_commit1_str)
98 } else {
99 cursor_sent = next
100 fmt.Fprintf(w, ")]}'\n{\"log\": [%s]}\n", fake_c ommit2_str)
101 }
102 })
103 defer srv.Close()
104
105 Convey("Page till no cursor", func() {
106 commits, err := Log(ctx, "https://chromium.googlesource. com/repo.git",
107 "master..8de6836858c99e48f3c58164ab717bda728e95d d", 10)
108 So(err, ShouldBeNil)
109 So(cursor_sent, ShouldEqual, "next_cursor_value")
110 So(len(commits), ShouldEqual, 2)
111 So(commits[0].Author.Name, ShouldEqual, "Author 1")
112 So(commits[1].Commit, ShouldEqual, "dc1dbf1aa56e4dd4cbfa ab61c4d30a35adce5f40")
113 })
114
115 Convey("Page till limit", func() {
116 commits, err := Log(ctx, "https://chromium.googlesource. com/repo.git", "master", 1)
117 So(err, ShouldBeNil)
118 So(cursor_sent, ShouldEqual, "")
119 So(len(commits), ShouldEqual, 1)
120 So(commits[0].Author.Name, ShouldEqual, "Author 1")
121 })
122 })
123 }
124
125 ////////////////////////////////////////////////////////////////////////////////
126
127 var (
128 fake_commit1_str = `{
129 "commit": "0b2c5409e58a71c691b05454b55cc5580cc822d1",
130 "tree": "3c6f95bc757698cd6aca3c49f88f640fd145ea69",
131 "parents": [ "dc1dbf1aa56e4dd4cbfaab61c4d30a35adce5f40" ],
132 "author": {
133 "name": "Author 1",
134 "email": "author1@example.com",
135 "time": "Mon Jul 17 15:02:43 2017"
136 },
137 "committer": {
138 "name": "Commit Bot",
139 "email": "commit-bot@chromium.org",
140 "time": "Mon Jul 17 15:02:43 2017"
141 },
142 "message": "Import wpt@d96d68ed964f9bfc2bb248c2d2fab7a8870dc685\ \n\\nCr-Commit-Position: refs/heads/master@{#487078}"
143 }`
144 fake_commit2_str = `{
145 "commit": "dc1dbf1aa56e4dd4cbfaab61c4d30a35adce5f40",
146 "tree": "1ba2335c07915c31597b97a8d824aecc85a996f6",
147 "parents": ["8de6836858c99e48f3c58164ab717bda728e95dd"],
148 "author": {
149 "name": "Author 2",
150 "email": "author-2@example.com",
151 "time": "Mon Jul 17 15:01:13 2017"
152 },
153 "committer": {
154 "name": "Commit Bot",
155 "email": "commit-bot@chromium.org",
156 "time": "Mon Jul 17 15:01:13 2017"
157 },
158 "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}"
159 }`
160 )
161
162 ////////////////////////////////////////////////////////////////////////////////
163
164 func newMockHttps(ctx context.Context, handler func(w http.ResponseWriter, r *ht tp.Request)) (*httptest.Server, context.Context) {
165 // TODO(<REVIEWER>): any easier/better way to do this? I couldn't find a nything inside luci-go...
tandrii(chromium) 2017/07/19 20:12:31 any comment for this thing?
166 srv := httptest.NewTLSServer(http.HandlerFunc(handler))
167 parsedServerUrl, err := url.Parse(srv.URL)
168 So(err, ShouldBeNil)
169 cert, err := x509.ParseCertificate(srv.TLS.Certificates[0].Certificate[0 ])
170 So(err, ShouldBeNil)
171 certpool := x509.NewCertPool()
172 certpool.AddCert(cert)
173
174 ctx = auth.ModifyConfig(ctx, func(cfg auth.Config) auth.Config {
175 cfg.AnonymousTransport = func(context.Context) http.RoundTripper {
176 return &httpsForwarder{
177 Transport: &http.Transport{TLSClientConfig: &tls .Config{RootCAs: certpool}},
178 URL: parsedServerUrl,
179 }
180 }
181 return cfg
182 })
183 return srv, ctx
184 }
185
186 type httpsForwarder struct {
187 Transport http.RoundTripper
188 URL *url.URL
189 }
190
191 func (t httpsForwarder) RoundTrip(req *http.Request) (*http.Response, error) {
192 req.URL.Scheme = t.URL.Scheme
193 req.URL.Host = t.URL.Host
194 return t.Transport.RoundTrip(req)
195 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698