@@ -77,6 +77,65 @@ func TestDownloadWithoutFlags(t *testing.T) {
77
77
}
78
78
}
79
79
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
+
80
139
func TestDownload (t * testing.T ) {
81
140
co := newCapturedOutput ()
82
141
co .override ()
@@ -161,25 +220,6 @@ func fakeDownloadServer(requestor, teamSlug string) *httptest.Server {
161
220
fmt .Fprint (w , "this is file 2" )
162
221
})
163
222
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
-
183
223
mux .HandleFunc ("/file-3.txt" , func (w http.ResponseWriter , r * http.Request ) {
184
224
fmt .Fprint (w , "" )
185
225
})
@@ -216,31 +256,6 @@ func assertDownloadedCorrectFiles(t *testing.T, targetDir string) {
216
256
path : filepath .Join (targetDir , "bogus-track" , "bogus-exercise" , "subdir" , "file-2.txt" ),
217
257
contents : "this is file 2" ,
218
258
},
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
- },
244
259
}
245
260
246
261
for _ , file := range expectedFiles {
@@ -278,12 +293,7 @@ const payloadTemplate = `
278
293
"files": [
279
294
"file-1.txt",
280
295
"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"
287
297
],
288
298
"iteration": {
289
299
"submitted_at": "2017-08-21t10:11:12.130z"
0 commit comments