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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: common/api/gitiles/gitiles_test.go
diff --git a/common/api/gitiles/gitiles_test.go b/common/api/gitiles/gitiles_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..280033b4d354de39e8ff658316895335c80a0b7c
--- /dev/null
+++ b/common/api/gitiles/gitiles_test.go
@@ -0,0 +1,206 @@
+// Copyright 2017 The LUCI Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package gitiles
+
+import (
+ "crypto/tls"
+ "crypto/x509"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "testing"
+
+ "golang.org/x/net/context"
+
+ "github.com/luci/luci-go/server/auth"
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+func TestRepoURL(t *testing.T) {
+ t.Parallel()
+ Convey("Malformed", t, func() {
+ f := func(arg string) {
+ So(ValidateRepoURL(arg), ShouldNotBeNil)
+ _, err := NormalizeRepoURL(arg)
+ So(err, ShouldNotBeNil)
+ }
+
+ f("wtf/\\is\this")
+ f("https://example.com/repo.git")
+ f("http://bad-protocol.googlesource.com/repo.git")
+ f("https://a.googlesource.com")
+ 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.
+ })
+
+ Convey("OK", t, func() {
+ f := func(arg, exp string) {
+ So(ValidateRepoURL(arg), ShouldBeNil)
+ act, err := NormalizeRepoURL(arg)
+ So(err, ShouldBeNil)
+ So(act, ShouldEqual, exp)
+ }
+
+ f("https://chromium.googlesource.com/repo.git",
+ "https://chromium.googlesource.com/a/repo")
+ f("https://chromium.googlesource.com/repo/",
+ "https://chromium.googlesource.com/a/repo")
+ f("https://chromium.googlesource.com/a/repo",
+ "https://chromium.googlesource.com/a/repo")
+ f("https://chromium.googlesource.com/parent/repo.git/",
+ "https://chromium.googlesource.com/a/parent/repo")
+ })
+}
+
+func TestLog(t *testing.T) {
+ t.Parallel()
+ ctx := context.Background()
+
+ Convey("Log Bad Repo", t, func() {
+ _, err := Log(context.Background(), "bad://repo.url", "master", 10)
+ So(err, ShouldNotBeNil)
+ })
+
+ Convey("Log w/o pages", t, func() {
+ srv, ctx := newMockHttps(ctx, func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(200)
+ w.Header().Set("Content-Type", "application/json")
+ fmt.Fprintf(w, ")]}'\n{\"log\": [%s, %s]}\n", fake_commit1_str, fake_commit2_str)
+ })
+ defer srv.Close()
+
+ Convey("Return All", func() {
+ commits, err := Log(ctx, "https://chromium.googlesource.com/repo.git",
+ "master..8de6836858c99e48f3c58164ab717bda728e95dd", 10)
+ So(err, ShouldBeNil)
+ So(len(commits), ShouldEqual, 2)
+ So(commits[0].Author.Name, ShouldEqual, "Author 1")
+ So(commits[1].Commit, ShouldEqual, "dc1dbf1aa56e4dd4cbfaab61c4d30a35adce5f40")
+ })
+
+ Convey("DO not exceed limit", func() {
+ commits, err := Log(ctx, "https://chromium.googlesource.com/repo.git",
+ "master..8de6836858c99e48f3c58164ab717bda728e95dd", 1)
+ So(err, ShouldBeNil)
+ So(len(commits), ShouldEqual, 1)
+ So(commits[0].Author.Name, ShouldEqual, "Author 1")
+ })
+ })
+
+ Convey("Log Paging", t, func() {
+ cursor_sent := ""
+ srv, ctx := newMockHttps(ctx, func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(200)
+ w.Header().Set("Content-Type", "application/json")
+ if next := r.URL.Query().Get("s"); next == "" {
+ fmt.Fprintf(w, ")]}'\n{\"log\": [%s], \"next\": \"next_cursor_value\"}\n", fake_commit1_str)
+ } else {
+ cursor_sent = next
+ fmt.Fprintf(w, ")]}'\n{\"log\": [%s]}\n", fake_commit2_str)
+ }
+ })
+ defer srv.Close()
+
+ Convey("Page till no cursor", func() {
+ commits, err := Log(ctx, "https://chromium.googlesource.com/repo.git",
+ "master..8de6836858c99e48f3c58164ab717bda728e95dd", 10)
+ So(err, ShouldBeNil)
+ So(cursor_sent, ShouldEqual, "next_cursor_value")
+ So(len(commits), ShouldEqual, 2)
+ So(commits[0].Author.Name, ShouldEqual, "Author 1")
+ So(commits[1].Commit, ShouldEqual, "dc1dbf1aa56e4dd4cbfaab61c4d30a35adce5f40")
+ })
+
+ Convey("Page till limit", func() {
+ commits, err := Log(ctx, "https://chromium.googlesource.com/repo.git", "master", 1)
+ So(err, ShouldBeNil)
+ So(cursor_sent, ShouldEqual, "")
+ So(len(commits), ShouldEqual, 1)
+ So(commits[0].Author.Name, ShouldEqual, "Author 1")
+ })
+ })
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+var (
+ fake_commit1_str = `{
+ "commit": "0b2c5409e58a71c691b05454b55cc5580cc822d1",
+ "tree": "3c6f95bc757698cd6aca3c49f88f640fd145ea69",
+ "parents": [ "dc1dbf1aa56e4dd4cbfaab61c4d30a35adce5f40" ],
+ "author": {
+ "name": "Author 1",
+ "email": "author1@example.com",
+ "time": "Mon Jul 17 15:02:43 2017"
+ },
+ "committer": {
+ "name": "Commit Bot",
+ "email": "commit-bot@chromium.org",
+ "time": "Mon Jul 17 15:02:43 2017"
+ },
+ "message": "Import wpt@d96d68ed964f9bfc2bb248c2d2fab7a8870dc685\\n\\nCr-Commit-Position: refs/heads/master@{#487078}"
+ }`
+ fake_commit2_str = `{
+ "commit": "dc1dbf1aa56e4dd4cbfaab61c4d30a35adce5f40",
+ "tree": "1ba2335c07915c31597b97a8d824aecc85a996f6",
+ "parents": ["8de6836858c99e48f3c58164ab717bda728e95dd"],
+ "author": {
+ "name": "Author 2",
+ "email": "author-2@example.com",
+ "time": "Mon Jul 17 15:01:13 2017"
+ },
+ "committer": {
+ "name": "Commit Bot",
+ "email": "commit-bot@chromium.org",
+ "time": "Mon Jul 17 15:01:13 2017"
+ },
+ "message": "[Web Payments] User weak ptr in Payment Request\u0027s error callback\\n\\nBug: 742329\\nReviewed-on: https://chromium-review.googlesource.com/570982\\nCr-Commit-Position: refs/heads/master@{#487077}"
+ }`
+)
+
+////////////////////////////////////////////////////////////////////////////////
+
+func newMockHttps(ctx context.Context, handler func(w http.ResponseWriter, r *http.Request)) (*httptest.Server, context.Context) {
+ // TODO(<REVIEWER>): any easier/better way to do this? I couldn't find anything 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.
+ srv := httptest.NewTLSServer(http.HandlerFunc(handler))
+ parsedServerUrl, err := url.Parse(srv.URL)
+ So(err, ShouldBeNil)
+ cert, err := x509.ParseCertificate(srv.TLS.Certificates[0].Certificate[0])
+ So(err, ShouldBeNil)
+ certpool := x509.NewCertPool()
+ certpool.AddCert(cert)
+
+ ctx = auth.ModifyConfig(ctx, func(cfg auth.Config) auth.Config {
+ cfg.AnonymousTransport = func(context.Context) http.RoundTripper {
+ return &httpsForwarder{
+ Transport: &http.Transport{TLSClientConfig: &tls.Config{RootCAs: certpool}},
+ URL: parsedServerUrl,
+ }
+ }
+ return cfg
+ })
+ return srv, ctx
+}
+
+type httpsForwarder struct {
+ Transport http.RoundTripper
+ URL *url.URL
+}
+
+func (t httpsForwarder) RoundTrip(req *http.Request) (*http.Response, error) {
+ req.URL.Scheme = t.URL.Scheme
+ req.URL.Host = t.URL.Host
+ return t.Transport.RoundTrip(req)
+}

Powered by Google App Engine
This is Rietveld 408576698