@@ -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 ()
@@ -162,25 +221,6 @@ func fakeDownloadServer(requestor, teamSlug string) *httptest.Server {
162
221
fmt .Fprint (w , "this is file 2" )
163
222
})
164
223
165
- mux .HandleFunc ("/full/path/with/numeric-suffix/bogus-track/bogus-exercise-12345/subdir/numeric.txt" , func (w http.ResponseWriter , r * http.Request ) {
166
- fmt .Fprint (w , "with numeric suffix" )
167
- })
168
-
169
- mux .HandleFunc ("/special-char-filename#.txt" , func (w http.ResponseWriter , r * http.Request ) {
170
- fmt .Fprint (w , "this is a special file" )
171
- })
172
-
173
- mux .HandleFunc ("/\\ with-leading-backslash.txt" , func (w http.ResponseWriter , r * http.Request ) {
174
- fmt .Fprint (w , "with backslash in name" )
175
- })
176
- mux .HandleFunc ("/\\ with\\ backslashes\\ in\\ path.txt" , func (w http.ResponseWriter , r * http.Request ) {
177
- fmt .Fprint (w , "with backslash in path" )
178
- })
179
-
180
- mux .HandleFunc ("/with-leading-slash.txt" , func (w http.ResponseWriter , r * http.Request ) {
181
- fmt .Fprint (w , "this has a slash" )
182
- })
183
-
184
224
mux .HandleFunc ("/file-3.txt" , func (w http.ResponseWriter , r * http.Request ) {
185
225
fmt .Fprint (w , "" )
186
226
})
@@ -217,31 +257,6 @@ func assertDownloadedCorrectFiles(t *testing.T, targetDir string) {
217
257
path : filepath .Join (targetDir , "bogus-track" , "bogus-exercise" , "subdir" , "file-2.txt" ),
218
258
contents : "this is file 2" ,
219
259
},
220
- {
221
- desc : "a path with a numeric suffix" ,
222
- path : filepath .Join (targetDir , "bogus-track" , "bogus-exercise" , "subdir" , "numeric.txt" ),
223
- contents : "with numeric suffix" ,
224
- },
225
- {
226
- desc : "a file that requires URL encoding" ,
227
- path : filepath .Join (targetDir , "bogus-track" , "bogus-exercise" , "special-char-filename#.txt" ),
228
- contents : "this is a special file" ,
229
- },
230
- {
231
- desc : "a file that has a leading slash" ,
232
- path : filepath .Join (targetDir , "bogus-track" , "bogus-exercise" , "with-leading-slash.txt" ),
233
- contents : "this has a slash" ,
234
- },
235
- {
236
- desc : "a file with a leading backslash" ,
237
- path : filepath .Join (targetDir , "bogus-track" , "bogus-exercise" , "with-leading-backslash.txt" ),
238
- contents : "with backslash in name" ,
239
- },
240
- {
241
- desc : "a file with backslashes in path" ,
242
- path : filepath .Join (targetDir , "bogus-track" , "bogus-exercise" , "with" , "backslashes" , "in" , "path.txt" ),
243
- contents : "with backslash in path" ,
244
- },
245
260
}
246
261
247
262
for _ , file := range expectedFiles {
@@ -279,12 +294,7 @@ const payloadTemplate = `
279
294
"files": [
280
295
"file-1.txt",
281
296
"subdir/file-2.txt",
282
- "special-char-filename#.txt",
283
- "/with-leading-slash.txt",
284
- "\\with-leading-backslash.txt",
285
- "\\with\\backslashes\\in\\path.txt",
286
- "file-3.txt",
287
- "/full/path/with/numeric-suffix/bogus-track/bogus-exercise-12345/subdir/numeric.txt"
297
+ "file-3.txt"
288
298
],
289
299
"iteration": {
290
300
"submitted_at": "2017-08-21t10:11:12.130z"
0 commit comments