-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_test.go
More file actions
187 lines (164 loc) · 4.16 KB
/
Copy pathgithub_test.go
File metadata and controls
187 lines (164 loc) · 4.16 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
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
178
179
180
181
182
183
184
185
186
187
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseRepoURL(t *testing.T) {
tests := []struct {
name string
input string
wantOwner string
wantRepo string
wantErr bool
}{
{
name: "simple",
input: "https://github.com/user/repo",
wantOwner: "user",
wantRepo: "repo",
},
{
name: "trailing slash",
input: "https://github.com/user/repo/",
wantOwner: "user",
wantRepo: "repo",
},
{
name: "with tree path",
input: "https://github.com/user/repo/tree/main",
wantOwner: "user",
wantRepo: "repo",
},
{
name: "invalid host",
input: "https://gitlab.com/user/repo",
wantErr: true,
},
{
name: "too short path",
input: "https://github.com/user",
wantErr: true,
},
{
name: "empty",
input: "",
wantErr: true,
},
{
name: "whitespace",
input: " ",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
owner, repo, err := parseRepoURL(tt.input)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.wantOwner, owner)
assert.Equal(t, tt.wantRepo, repo)
})
}
}
func TestBuildRawURL(t *testing.T) {
owner := "user"
repo := "repo"
branch := "main"
filePath := "dir/file.go"
got := buildRawURL(owner, repo, branch, filePath)
want := "https://raw.githubusercontent.com/user/repo/main/dir/file.go"
assert.Equal(t, want, got)
}
func TestListRawURLsWithOwnerRepo_FiltersBlobsOnly(t *testing.T) {
// Arrange: fake GitHub API server
mux := http.NewServeMux()
mux.HandleFunc("/repos/o/r/git/trees/main", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "github-repo-raw-urls-ui", r.Header.Get("User-Agent"))
assert.Equal(t, "1", r.URL.Query().Get("recursive"))
resp := TreeResponse{
Sha: "abc",
Tree: []TreeObject{
{Path: "README.md", Type: "blob"},
{Path: "dir", Type: "tree"},
{Path: "dir/main.go", Type: "blob"},
},
}
_ = json.NewEncoder(w).Encode(resp)
})
srv := httptest.NewServer(mux)
defer srv.Close()
// Patch bases for test
oldAPI := githubAPIBase
oldRaw := rawBase
githubAPIBase = srv.URL
rawBase = "https://raw.test"
defer func() {
githubAPIBase = oldAPI
rawBase = oldRaw
}()
// Act
urls, err := ListRawURLsWithOwnerRepo("o", "r", "main")
// Assert
assert.NoError(t, err)
assert.Equal(t, []string{
"https://raw.test/o/r/main/README.md",
"https://raw.test/o/r/main/dir/main.go",
}, urls)
}
func TestFetchTree_404(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/repos/o/r/git/trees/missing", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
})
srv := httptest.NewServer(mux)
defer srv.Close()
oldAPI := githubAPIBase
githubAPIBase = srv.URL
defer func() { githubAPIBase = oldAPI }()
_, err := fetchTree("o", "r", "missing")
assert.Error(t, err)
// message depends on your english translation; adjust if you change it
assert.Contains(t, err.Error(), "HTTP 404")
}
func TestGetBranches_MarksDefaultBranch(t *testing.T) {
mux := http.NewServeMux()
// repo info endpoint
mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "github-repo-raw-urls-ui", r.Header.Get("User-Agent"))
_ = json.NewEncoder(w).Encode(repoInfo{DefaultBranch: "main"})
})
// branches endpoint
mux.HandleFunc("/repos/o/r/branches", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "github-repo-raw-urls-ui", r.Header.Get("User-Agent"))
_ = json.NewEncoder(w).Encode([]map[string]string{
{"name": "dev"},
{"name": "main"},
})
})
srv := httptest.NewServer(mux)
defer srv.Close()
oldAPI := githubAPIBase
githubAPIBase = srv.URL
defer func() { githubAPIBase = oldAPI }()
branches, def, err := getBranches("o", "r")
assert.NoError(t, err)
assert.Equal(t, "main", def)
assert.Len(t, branches, 2)
// Find main and ensure Default=true
var mainB *Branch
for i := range branches {
if branches[i].Name == "main" {
mainB = &branches[i]
break
}
}
if assert.NotNil(t, mainB) {
assert.True(t, mainB.Default)
}
}