Repository navigation
Expand file tree
/
Copy pathweb_test.go
More file actions
148 lines (135 loc) · 3.51 KB
/
Copy pathweb_test.go
File metadata and controls
148 lines (135 loc) · 3.51 KB
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
package patchbin
import (
"context"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
"github.com/picosh/patchbin/util"
)
func TestWebRoutingAndRedirects(t *testing.T) {
dataDir := util.CreateTmpDir()
defer func() {
_ = os.RemoveAll(dataDir)
}()
suite := setupTest(dataDir, cfgSingleTenantTmpl)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := GitSshServer(ctx, suite.cfg)
go func() {
_ = s.ListenAndServe()
}()
time.Sleep(100 * time.Millisecond)
// Create a PR via SSH: repo=myrepo, slug=feat/auth
output := suite.userKey.MustCmd(suite.patch, "push myrepo:feat/auth")
if !strings.Contains(output, "created") {
t.Fatalf("failed to create PR: %s", output)
}
handler := GitWebServer(suite.cfg)
tests := []struct {
name string
path string
expectedStatus int
expectedHeader map[string]string
containsBody string
}{
{
name: "Home page",
path: "/",
expectedStatus: http.StatusOK,
containsBody: "A pastebin for patches",
},
{
name: "Active PRs page",
path: "/active",
expectedStatus: http.StatusOK,
containsBody: "feat/auth",
},
{
name: "Inactive PRs page",
path: "/inactive",
expectedStatus: http.StatusOK,
},
{
name: "Global RSS feed",
path: "/rss",
expectedStatus: http.StatusOK,
containsBody: "xml",
},
{
name: "Repo page",
path: "/myrepo",
expectedStatus: http.StatusOK,
containsBody: "feat/auth",
},
{
name: "Repo RSS feed",
path: "/myrepo/rss",
expectedStatus: http.StatusOK,
containsBody: "xml",
},
{
name: "PR detail page",
path: "/myrepo/feat/auth",
expectedStatus: http.StatusOK,
containsBody: "feat/auth",
},
{
name: "PR raw mbox patch",
path: "/myrepo/feat/auth.patch",
expectedStatus: http.StatusOK,
containsBody: "From ",
},
{
name: "PR RSS feed",
path: "/myrepo/feat/auth/rss",
expectedStatus: http.StatusOK,
containsBody: "xml",
},
{
name: "Legacy redirect /prs/1",
path: "/prs/1",
expectedStatus: http.StatusMovedPermanently,
expectedHeader: map[string]string{
"Location": "/myrepo/feat/auth",
},
},
{
name: "Legacy redirect /prs/1.patch",
path: "/prs/1.patch",
expectedStatus: http.StatusMovedPermanently,
expectedHeader: map[string]string{
"Location": "/myrepo/feat/auth.patch",
},
},
{
name: "Legacy redirect /prs/1/rss",
path: "/prs/1/rss",
expectedStatus: http.StatusMovedPermanently,
expectedHeader: map[string]string{
"Location": "/myrepo/feat/auth/rss",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("GET", tt.path, nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if rec.Code != tt.expectedStatus {
t.Fatalf("GET %s returned status %d, expected %d. Body: %s", tt.path, rec.Code, tt.expectedStatus, rec.Body.String())
}
for header, expVal := range tt.expectedHeader {
gotVal := rec.Header().Get(header)
if gotVal != expVal {
t.Errorf("GET %s header %s = %q, expected %q", tt.path, header, gotVal, expVal)
}
}
if tt.containsBody != "" && !strings.Contains(rec.Body.String(), tt.containsBody) {
t.Errorf("GET %s body does not contain %q", tt.path, tt.containsBody)
}
})
}
}