forked from gomods/athens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
64 lines (57 loc) · 1.85 KB
/
handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package download
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/gomods/athens/pkg/download/mode"
"github.com/gomods/athens/pkg/errors"
"github.com/gomods/athens/pkg/log"
"github.com/gomods/athens/pkg/storage"
"github.com/gorilla/mux"
)
func TestRedirect(t *testing.T) {
for _, url := range []string{"https://gomods.io", "https://internal.domain/repository/gonexus"} {
r := mux.NewRouter()
RegisterHandlers(r, &HandlerOpts{
Protocol: &mockProtocol{},
Logger: log.NoOpLogger(),
DownloadFile: &mode.DownloadFile{
Mode: mode.Redirect,
DownloadURL: url,
},
})
for _, path := range [...]string{
"/github.com/gomods/athens/@v/v0.4.0.info",
"/github.com/gomods/athens/@v/v0.4.0.mod",
"/github.com/gomods/athens/@v/v0.4.0.zip",
} {
req := httptest.NewRequest("GET", path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusMovedPermanently {
t.Fatalf("expected a redirect status (301) but got %v", w.Code)
}
expectedRedirect := url + path
givenRedirect := w.HeaderMap.Get("location")
if expectedRedirect != givenRedirect {
t.Fatalf("expected the handler to redirect to %q but got %q", expectedRedirect, givenRedirect)
}
}
}
}
type mockProtocol struct {
Protocol
}
func (mp *mockProtocol) Info(ctx context.Context, mod, ver string) ([]byte, error) {
const op errors.Op = "mockProtocol.Info"
return nil, errors.E(op, "not found", errors.KindRedirect)
}
func (mp *mockProtocol) GoMod(ctx context.Context, mod, ver string) ([]byte, error) {
const op errors.Op = "mockProtocol.GoMod"
return nil, errors.E(op, "not found", errors.KindRedirect)
}
func (mp *mockProtocol) Zip(ctx context.Context, mod, ver string) (storage.SizeReadCloser, error) {
const op errors.Op = "mockProtocol.Zip"
return nil, errors.E(op, "not found", errors.KindRedirect)
}