Permalink
Browse files

Redirect to doc (if specified) when accessed outside go get

  • Loading branch information...
cskr committed Jul 28, 2012
1 parent 3011b43 commit f7c5d4afcdecd98ba24f65f9492ec95e20028325
Showing with 64 additions and 16 deletions.
  1. +1 −1 README.md
  2. +15 −6 gorepos.go
  3. +48 −9 gorepos_test.go
View
@@ -20,7 +20,7 @@ Ensure $GOPATH/bin is part of your $PATH. Install gorepos with,
The package list file must contain a line for each available package in the form,
- <path> <vcs> <repo-root>
+ <path> <vcs> <repo-root> [<godoc-url>]
# Example
View
@@ -155,21 +155,30 @@ func (pl *PackageList) ServeHTTP(w http.ResponseWriter, r *http.Request) {
})
} else {
if pkg, ok := pl.packages[r.URL.Path]; ok {
- pkgTmpl.Execute(w, map[string]interface{}{
- "host": r.Host,
- "pkg": pkg,
- })
+ if r.FormValue("go-get") == "1" || pkg.Doc == "" {
+ pkgTmpl.Execute(w, map[string]interface{}{
+ "host": r.Host,
+ "pkg": pkg,
+ })
+ } else {
+ http.Redirect(w, r, pkg.Doc, http.StatusFound)
+ }
} else {
http.NotFound(w, r)
}
}
}
type Package struct {
- Path, Vcs, Repo string
+ Path, Vcs, Repo, Doc string
}
func NewPackage(line string) Package {
fields := strings.Fields(line)
- return Package{fields[0], fields[1], fields[2]}
+ doc := ""
+ if len(fields) > 3 {
+ doc = fields[3]
+ }
+
+ return Package{fields[0], fields[1], fields[2], doc}
}
View
@@ -75,19 +75,19 @@ func TestPkg(t *testing.T) {
return
}
- body, expected := invokePkg(pl, "lib1", "git", "ssh://git@bitbucket.org/user1/lib1")
+ body, expected := invokePkg(pl, "lib1", "git", "ssh://git@bitbucket.org/user1/lib1", true)
if body != expected {
t.Errorf("Body = %s, want %s", body, expected)
return
}
- body, expected = invokePkg(pl, "lib2", "hg", "ssh://hg@bitbucket.org/user2/lib2")
+ body, expected = invokePkg(pl, "lib2", "hg", "ssh://hg@bitbucket.org/user2/lib2", true)
if body != expected {
t.Errorf("Body = %s, want %s", body, expected)
return
}
- body, expected = invokePkg(pl, "lib3", "git", "ssh://git@go.mydomain.com/lib3")
+ body, expected = invokePkg(pl, "lib3", "git", "ssh://git@go.mydomain.com/lib3", true)
if body != expected {
t.Errorf("Body = %s, want %s", body, expected)
}
@@ -113,12 +113,40 @@ func TestReload(t *testing.T) {
}
time.Sleep(100 * time.Millisecond)
- body, expected := invokePkg(pl, "lib4", "git", "ssh://git@go.mydomain.com/lib4")
+ body, expected := invokePkg(pl, "lib4", "git", "ssh://git@go.mydomain.com/lib4", true)
if body != expected {
t.Errorf("Body = %s, want %s", body, expected)
}
}
+func TestRedirect(t *testing.T) {
+ list, err := generateList()
+ if err != nil {
+ t.Errorf("Error generating package list: %s", err)
+ }
+
+ pl, err := NewPackageList(list)
+ if err != nil {
+ t.Errorf("Error reading package list: %s", err)
+ return
+ }
+
+ body, expected := invokePkg(pl, "lib1", "git", "ssh://git@bitbucket.org/user1/lib1", false)
+ if body != expected {
+ t.Errorf("Body = %s, want %s", body, expected)
+ return
+ }
+
+ w := recordPkg(pl, "lib3", "git", "ssh://git@go.mydomain.com/lib3", false)
+ if w.Code != 302 {
+ t.Errorf("Status = %d, want 302", w.Code)
+ }
+
+ if loc := w.Header()["Location"]; loc == nil || loc[0] != "http://godoc.mydomain.com/lib3" {
+ t.Errorf("Location = %v, want [\"http://godoc.mydomain.com/lib3\"]", loc)
+ }
+}
+
func generateList() (fname string, err error) {
fname = fmt.Sprintf("%s%ctest_list", os.TempDir(), os.PathSeparator)
f, err := os.Create(fname)
@@ -130,7 +158,7 @@ func generateList() (fname string, err error) {
fmt.Fprintln(f, "/lib1 git ssh://git@bitbucket.org/user1/lib1")
fmt.Fprintln(f, "/lib2 hg ssh://hg@bitbucket.org/user2/lib2")
fmt.Fprintln(f, " ")
- fmt.Fprintln(f, "/lib3 git ssh://git@go.mydomain.com/lib3")
+ fmt.Fprintln(f, "/lib3 git ssh://git@go.mydomain.com/lib3 http://godoc.mydomain.com/lib3")
return fname, nil
}
@@ -145,10 +173,8 @@ func appendList(list, line string) error {
return nil
}
-func invokePkg(pl *PackageList, pkg, vcs, repo string) (body, expected string) {
- r, _ := http.NewRequest("GET", "http://example.com/"+pkg, nil)
- w := httptest.NewRecorder()
- pl.ServeHTTP(w, r)
+func invokePkg(pl *PackageList, pkg, vcs, repo string, includeParam bool) (body, expected string) {
+ w := recordPkg(pl, pkg, vcs, repo, includeParam)
b := new(bytes.Buffer)
pkgTmpl.Execute(b, map[string]interface{}{
@@ -162,3 +188,16 @@ func invokePkg(pl *PackageList, pkg, vcs, repo string) (body, expected string) {
return w.Body.String(), b.String()
}
+
+func recordPkg(pl *PackageList, pkg, vcs, repo string, includeParam bool) *httptest.ResponseRecorder {
+ url := "http://example.com/" + pkg
+ if includeParam {
+ url += "?go-get=1"
+ }
+
+ r, _ := http.NewRequest("GET", url, nil)
+ w := httptest.NewRecorder()
+ pl.ServeHTTP(w, r)
+
+ return w
+}

0 comments on commit f7c5d4a

Please sign in to comment.