LEFT | RIGHT |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package main | 5 package main |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "errors" | 9 "errors" |
10 "http" | |
11 "io/ioutil" | 10 "io/ioutil" |
| 11 "net/http" |
12 "testing" | 12 "testing" |
13 ) | 13 ) |
14 | 14 |
15 var FindPublicRepoTests = []struct { | 15 var FindPublicRepoTests = []struct { |
16 » pkg string | 16 » pkg string |
17 » vcs, prefix, repo string | 17 » vcs, root, url string |
18 » transport *testTransport | 18 » transport *testTransport |
19 }{ | 19 }{ |
20 { | 20 { |
21 "repo.googlecode.com/hg/path/foo", | 21 "repo.googlecode.com/hg/path/foo", |
22 "hg", | 22 "hg", |
23 "repo.googlecode.com/hg", | 23 "repo.googlecode.com/hg", |
24 "https://repo.googlecode.com/hg", | 24 "https://repo.googlecode.com/hg", |
25 nil, | 25 nil, |
26 }, | 26 }, |
27 { | 27 { |
28 "repo.googlecode.com/svn/path", | 28 "repo.googlecode.com/svn/path", |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 nil, | 90 nil, |
91 }, | 91 }, |
92 } | 92 } |
93 | 93 |
94 func TestFindPublicRepo(t *testing.T) { | 94 func TestFindPublicRepo(t *testing.T) { |
95 for _, test := range FindPublicRepoTests { | 95 for _, test := range FindPublicRepoTests { |
96 client := http.DefaultClient | 96 client := http.DefaultClient |
97 if test.transport != nil { | 97 if test.transport != nil { |
98 client = &http.Client{Transport: test.transport} | 98 client = &http.Client{Transport: test.transport} |
99 } | 99 } |
100 » » m, err := findPublicRepo(test.pkg, client) | 100 » » repo, err := findPublicRepo(test.pkg) |
101 if err != nil { | 101 if err != nil { |
102 » » » t.Errorf("%s: got error: %v", test.pkg, err) | 102 » » » t.Errorf("findPublicRepo(%s): error: %v", test.pkg, err) |
103 continue | 103 continue |
104 } | 104 } |
105 » » if m == nil { | 105 » » if repo == nil { |
106 t.Errorf("%s: got nil match", test.pkg) | 106 t.Errorf("%s: got nil match", test.pkg) |
107 continue | 107 continue |
108 } | 108 } |
109 | 109 » » url, root, vcs, err := repo.Repo(client) |
110 » » if v := vcsMap[test.vcs]; m.vcs != v { | 110 » » if err != nil { |
111 » » » t.Errorf("%s: got vcs=%v, want %v", test.pkg, m.vcs, v) | 111 » » » t.Errorf("%s: repo.Repo error: %v", test.pkg, err) |
| 112 » » » continue |
112 } | 113 } |
113 » » if m.prefix != test.prefix { | 114 » » if v := vcsMap[test.vcs]; vcs != v { |
114 » » » t.Errorf("%s: got prefix=%v, want %v", test.pkg, m.prefi
x, test.prefix) | 115 » » » t.Errorf("%s: got vcs=%v, want %v", test.pkg, vcs, v) |
115 } | 116 } |
116 » » if m.repo != test.repo { | 117 » » if root != test.root { |
117 » » » t.Errorf("%s: got repo=%v, want %v", test.pkg, m.repo, t
est.repo) | 118 » » » t.Errorf("%s: got root=%v, want %v", test.pkg, root, tes
t.root) |
| 119 » » } |
| 120 » » if url != test.url { |
| 121 » » » t.Errorf("%s: got url=%v, want %v", test.pkg, url, test.
url) |
118 } | 122 } |
119 } | 123 } |
120 } | 124 } |
121 | 125 |
122 type testTransport struct { | 126 type testTransport struct { |
123 expectURL string | 127 expectURL string |
124 responseBody string | 128 responseBody string |
125 } | 129 } |
126 | 130 |
127 func (t *testTransport) RoundTrip(req *http.Request) (*http.Response, error) { | 131 func (t *testTransport) RoundTrip(req *http.Request) (*http.Response, error) { |
128 if g, e := req.URL.String(), t.expectURL; g != e { | 132 if g, e := req.URL.String(), t.expectURL; g != e { |
129 return nil, errors.New("want " + e) | 133 return nil, errors.New("want " + e) |
130 } | 134 } |
131 body := ioutil.NopCloser(bytes.NewBufferString(t.responseBody)) | 135 body := ioutil.NopCloser(bytes.NewBufferString(t.responseBody)) |
132 return &http.Response{ | 136 return &http.Response{ |
133 StatusCode: http.StatusOK, | 137 StatusCode: http.StatusOK, |
134 Body: body, | 138 Body: body, |
135 }, nil | 139 }, nil |
136 } | 140 } |
LEFT | RIGHT |