-
Notifications
You must be signed in to change notification settings - Fork 2
/
main_test.go
177 lines (169 loc) · 4.25 KB
/
main_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
mockRegistry "github.com/g-harel/npmfs/internal/registry/mock"
)
func TestRoutes(t *testing.T) {
tt := map[string]struct {
Path string
Status int
Redirect string
}{
"home": {
Path: "/",
Status: http.StatusOK,
},
"static favicon": {
Path: "/favicon.ico",
Status: http.StatusOK,
Redirect: "/assets/favicon.ico",
},
"static robots.txt": {
Path: "/robots.txt",
Status: http.StatusOK,
Redirect: "/assets/robots.txt",
},
"static icon": {
Path: "/assets/icon-package.svg",
Status: http.StatusOK,
},
"package versions shortcut": {
Path: "/test",
Status: http.StatusOK,
Redirect: "/package/test/",
},
"namespaced package versions shortcut": {
Path: "/@test/test",
Status: http.StatusOK,
Redirect: "/package/@test/test/",
},
"package versions": {
Path: "/package/test",
Status: http.StatusOK,
Redirect: "/package/test/",
},
"namespaced package versions": {
Path: "/package/@test/test",
Status: http.StatusOK,
Redirect: "/package/@test/test/",
},
"package versions from compare": {
Path: "/compare/test",
Status: http.StatusOK,
Redirect: "/package/test/",
},
"namespaced package versions from compare": {
Path: "/compare/@test/test",
Status: http.StatusOK,
Redirect: "/package/@test/test/",
},
"package contents": {
Path: "/package/test/0.0.0",
Status: http.StatusOK,
Redirect: "/package/test/0.0.0/",
},
"package contents v redirect": {
Path: "/package/test/v/0.0.0",
Status: http.StatusOK,
Redirect: "/package/test/0.0.0/",
},
"namespaced package contents": {
Path: "/package/@test/test/0.0.0",
Status: http.StatusOK,
Redirect: "/package/@test/test/0.0.0/",
},
"namespaced package contents v redirect": {
Path: "/package/@test/test/v/0.0.0",
Status: http.StatusOK,
Redirect: "/package/@test/test/0.0.0/",
},
"package file": {
Path: "/package/test/0.0.0/README.md",
Status: http.StatusOK,
},
"namespaced package file": {
Path: "/package/@test/test/0.0.0/README.md",
Status: http.StatusOK,
},
"package compare picker": {
Path: "/compare/test/0.0.0",
Status: http.StatusOK,
Redirect: "/compare/test/0.0.0/",
},
"namespaced package compare picker": {
Path: "/compare/@test/test/0.0.0",
Status: http.StatusOK,
Redirect: "/compare/@test/test/0.0.0/",
},
"package compare": {
Path: "/compare/test/0.0.0/1.1.1",
Status: http.StatusOK,
Redirect: "/compare/test/0.0.0/1.1.1/",
},
"namespaced package compare": {
Path: "/compare/@test/test/0.0.0/1.1.1",
Status: http.StatusOK,
Redirect: "/compare/@test/test/0.0.0/1.1.1/",
},
"package download": {
Path: "/download/test/0.0.0",
Status: http.StatusOK,
Redirect: "/download/test/0.0.0/",
},
"namespaced package download": {
Path: "/download/@test/test/0.0.0",
Status: http.StatusOK,
Redirect: "/download/@test/test/0.0.0/",
},
"file download": {
Path: "/download/test/0.0.0/README.md",
Status: http.StatusOK,
},
"namespaced file download": {
Path: "/download/@test/test/0.0.0/README.md",
Status: http.StatusOK,
},
"invalid": {
Path: "/@a",
Status: http.StatusNotFound,
},
}
client := &mockRegistry.Client{
Latest: "1.1.1",
Contents: map[string]map[string]string{
"0.0.0": {
"README.md": "",
},
"1.1.1": {
"README.md": "",
},
},
}
srv := httptest.NewServer(routes(client))
defer srv.Close()
for name, tc := range tt {
t.Run(name, func(t *testing.T) {
println("xxx - ",tc.Path)
res, err := http.Get(fmt.Sprintf("%v%v", srv.URL, tc.Path))
if err != nil {
t.Fatalf("send GET request: %v", err)
}
if res.StatusCode != tc.Status {
t.Fatalf("expected/received status codes do not match\n%v\n%v", tc.Status, res.StatusCode)
}
path := res.Request.URL.EscapedPath()
if tc.Path != path {
if tc.Redirect == "" {
t.Fatalf("unexpected redirection\n%v\n%v", tc.Path, path)
} else {
if tc.Redirect != path {
t.Fatalf("expected/received redirected paths do not match\n%v\n%v", tc.Redirect, path)
}
}
}
})
}
}