-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/golangorg: remove duplicate list of repo names from the /x/ handler
Also, add tests. This also removes the golang.org/x/codereview redirect because code.google.com and our Mercurial repository are long dead. Updates golang/go#36047 Change-Id: I0aacbe5a963b0edeea2dd2f1b2e2ad6a2f1f7319 Reviewed-on: https://go-review.googlesource.com/c/website/+/210744 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
- Loading branch information
Showing
5 changed files
with
145 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2019 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestXHandler(t *testing.T) { | ||
type check func(t *testing.T, rec *httptest.ResponseRecorder) | ||
status := func(v int) check { | ||
return func(t *testing.T, rec *httptest.ResponseRecorder) { | ||
t.Helper() | ||
if rec.Code != v { | ||
t.Errorf("response status = %v; want %v", rec.Code, v) | ||
} | ||
} | ||
} | ||
substr := func(s string) check { | ||
return func(t *testing.T, rec *httptest.ResponseRecorder) { | ||
t.Helper() | ||
if !strings.Contains(rec.Body.String(), s) { | ||
t.Errorf("missing expected substring %q in value: %#q", s, rec.Body) | ||
} | ||
} | ||
} | ||
hasHeader := func(k, v string) check { | ||
return func(t *testing.T, rec *httptest.ResponseRecorder) { | ||
t.Helper() | ||
if got := rec.HeaderMap.Get(k); got != v { | ||
t.Errorf("header[%q] = %q; want %q", k, got, v) | ||
} | ||
} | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
path string | ||
checks []check | ||
}{ | ||
{ | ||
name: "net", | ||
path: "/x/net", | ||
checks: []check{ | ||
status(200), | ||
substr(`<meta name="go-import" content="golang.org/x/net git https://go.googlesource.com/net">`), | ||
substr(`http-equiv="refresh" content="0; url=https://godoc.org/golang.org/x/net">`), | ||
}, | ||
}, | ||
{ | ||
name: "net-suffix", | ||
path: "/x/net/suffix", | ||
checks: []check{ | ||
status(200), | ||
substr(`<meta name="go-import" content="golang.org/x/net git https://go.googlesource.com/net">`), | ||
substr(`http-equiv="refresh" content="0; url=https://godoc.org/golang.org/x/net/suffix">`), | ||
}, | ||
}, | ||
{ | ||
name: "notexist", | ||
path: "/x/notexist", | ||
checks: []check{status(404)}, | ||
}, | ||
{ | ||
name: "empty", | ||
path: "/x/", | ||
checks: []check{ | ||
status(307), | ||
hasHeader("Location", "https://godoc.org/-/subrepo"), | ||
}, | ||
}, | ||
{ | ||
name: "invalid", | ||
path: "/x/In%20Valid,X", | ||
checks: []check{status(404)}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req := httptest.NewRequest("GET", tt.path, nil) | ||
rec := httptest.NewRecorder() | ||
xHandler(rec, req) | ||
for _, check := range tt.checks { | ||
check(t, rec) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters