Skip to content

Commit 46fe28a

Browse files
committed
Add test for Solution File Type
1 parent d6ea6a5 commit 46fe28a

File tree

1 file changed

+60
-50
lines changed

1 file changed

+60
-50
lines changed

cmd/download_test.go

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,65 @@ func TestDownloadWithoutFlags(t *testing.T) {
7777
}
7878
}
7979

80+
func TestSolutionFile(t *testing.T) {
81+
testCases := []struct {
82+
name, file, expectedPath, expectedURL string
83+
}{
84+
{
85+
name: "filename with special character",
86+
file: "special-char-filename#.txt",
87+
expectedPath: "special-char-filename#.txt",
88+
expectedURL: "http://www.example.com/special-char-filename%23.txt",
89+
},
90+
{
91+
name: "filename with leading slash",
92+
file: "/with-leading-slash.txt",
93+
expectedPath: fmt.Sprintf("%cwith-leading-slash.txt", os.PathSeparator),
94+
expectedURL: "http://www.example.com//with-leading-slash.txt",
95+
},
96+
{
97+
name: "filename with leading backslash",
98+
file: "\\with-leading-backslash.txt",
99+
expectedPath: fmt.Sprintf("%cwith-leading-backslash.txt", os.PathSeparator),
100+
expectedURL: "http://www.example.com/%5Cwith-leading-backslash.txt",
101+
},
102+
{
103+
name: "filename with backslashes in path",
104+
file: "\\backslashes\\in-path.txt",
105+
expectedPath: fmt.Sprintf("%[1]cbackslashes%[1]cin-path.txt", os.PathSeparator),
106+
expectedURL: "http://www.example.com/%5Cbackslashes%5Cin-path.txt",
107+
},
108+
{
109+
name: "path with a numeric suffix",
110+
file: "/bogus-exercise-12345/numeric.txt",
111+
expectedPath: fmt.Sprintf("%[1]cbogus-exercise-12345%[1]cnumeric.txt", os.PathSeparator),
112+
expectedURL: "http://www.example.com//bogus-exercise-12345/numeric.txt",
113+
},
114+
}
115+
116+
for _, tc := range testCases {
117+
t.Run(tc.name, func(t *testing.T) {
118+
sf := solutionFile{
119+
path: tc.file,
120+
baseURL: "http://www.example.com/",
121+
}
122+
123+
if sf.relativePath() != tc.expectedPath {
124+
t.Fatalf("Expected path '%s', got '%s'", tc.expectedPath, sf.relativePath())
125+
}
126+
127+
url, err := sf.url()
128+
if err != nil {
129+
t.Fatal(err)
130+
}
131+
132+
if url != tc.expectedURL {
133+
t.Fatalf("Expected URL '%s', got '%s'", tc.expectedURL, url)
134+
}
135+
})
136+
}
137+
}
138+
80139
func TestDownload(t *testing.T) {
81140
co := newCapturedOutput()
82141
co.override()
@@ -161,25 +220,6 @@ func fakeDownloadServer(requestor, teamSlug string) *httptest.Server {
161220
fmt.Fprint(w, "this is file 2")
162221
})
163222

164-
mux.HandleFunc("/full/path/with/numeric-suffix/bogus-track/bogus-exercise-12345/subdir/numeric.txt", func(w http.ResponseWriter, r *http.Request) {
165-
fmt.Fprint(w, "with numeric suffix")
166-
})
167-
168-
mux.HandleFunc("/special-char-filename#.txt", func(w http.ResponseWriter, r *http.Request) {
169-
fmt.Fprint(w, "this is a special file")
170-
})
171-
172-
mux.HandleFunc("/\\with-leading-backslash.txt", func(w http.ResponseWriter, r *http.Request) {
173-
fmt.Fprint(w, "with backslash in name")
174-
})
175-
mux.HandleFunc("/\\with\\backslashes\\in\\path.txt", func(w http.ResponseWriter, r *http.Request) {
176-
fmt.Fprint(w, "with backslash in path")
177-
})
178-
179-
mux.HandleFunc("/with-leading-slash.txt", func(w http.ResponseWriter, r *http.Request) {
180-
fmt.Fprint(w, "this has a slash")
181-
})
182-
183223
mux.HandleFunc("/file-3.txt", func(w http.ResponseWriter, r *http.Request) {
184224
fmt.Fprint(w, "")
185225
})
@@ -216,31 +256,6 @@ func assertDownloadedCorrectFiles(t *testing.T, targetDir string) {
216256
path: filepath.Join(targetDir, "bogus-track", "bogus-exercise", "subdir", "file-2.txt"),
217257
contents: "this is file 2",
218258
},
219-
{
220-
desc: "a path with a numeric suffix",
221-
path: filepath.Join(targetDir, "bogus-track", "bogus-exercise", "subdir", "numeric.txt"),
222-
contents: "with numeric suffix",
223-
},
224-
{
225-
desc: "a file that requires URL encoding",
226-
path: filepath.Join(targetDir, "bogus-track", "bogus-exercise", "special-char-filename#.txt"),
227-
contents: "this is a special file",
228-
},
229-
{
230-
desc: "a file that has a leading slash",
231-
path: filepath.Join(targetDir, "bogus-track", "bogus-exercise", "with-leading-slash.txt"),
232-
contents: "this has a slash",
233-
},
234-
{
235-
desc: "a file with a leading backslash",
236-
path: filepath.Join(targetDir, "bogus-track", "bogus-exercise", "with-leading-backslash.txt"),
237-
contents: "with backslash in name",
238-
},
239-
{
240-
desc: "a file with backslashes in path",
241-
path: filepath.Join(targetDir, "bogus-track", "bogus-exercise", "with", "backslashes", "in", "path.txt"),
242-
contents: "with backslash in path",
243-
},
244259
}
245260

246261
for _, file := range expectedFiles {
@@ -278,12 +293,7 @@ const payloadTemplate = `
278293
"files": [
279294
"file-1.txt",
280295
"subdir/file-2.txt",
281-
"special-char-filename#.txt",
282-
"/with-leading-slash.txt",
283-
"\\with-leading-backslash.txt",
284-
"\\with\\backslashes\\in\\path.txt",
285-
"file-3.txt",
286-
"/full/path/with/numeric-suffix/bogus-track/bogus-exercise-12345/subdir/numeric.txt"
296+
"file-3.txt"
287297
],
288298
"iteration": {
289299
"submitted_at": "2017-08-21t10:11:12.130z"

0 commit comments

Comments
 (0)