@@ -24,11 +24,12 @@ import (
2424)
2525
2626func TestClient (t * testing.T ) {
27- client , err := go_ftp . NewClient ( go_ftp.ClientConfig {
27+ config := go_ftp.ClientConfig {
2828 Hostname : "127.0.0.1:2121" ,
2929 Username : "admin" ,
3030 Password : "123456" ,
31- })
31+ }
32+ client , err := go_ftp .NewClient (config )
3233 require .NotNil (t , client )
3334 require .NoError (t , err )
3435
@@ -127,6 +128,55 @@ func TestClient(t *testing.T) {
127128 require .ErrorContains (t , err , "retrieving new.txt failed: 551 File not available" )
128129 })
129130
131+ t .Run ("mkdir and upload" , func (t * testing.T ) {
132+ client , err := go_ftp .NewClient (go_ftp.ClientConfig {
133+ Hostname : config .Hostname ,
134+ Username : config .Username ,
135+ Password : config .Password ,
136+
137+ CreateUploadDirectories : true ,
138+ })
139+ require .NotNil (t , client )
140+ require .NoError (t , err )
141+
142+ // Make sure we've delted newdir from any previous test runs
143+ require .NoError (t , os .RemoveAll (filepath .Join ("testdata" , "ftp-server" , "newdir" )))
144+
145+ // Upload files into new directories
146+ err = client .UploadFile ("newdir/first.txt" , io .NopCloser (strings .NewReader ("first newdir data" )))
147+ require .NoError (t , err )
148+
149+ err = client .UploadFile ("newdir/a/second.txt" , io .NopCloser (strings .NewReader ("second newdir data" )))
150+ require .NoError (t , err )
151+
152+ err = client .UploadFile ("newdir/a/b/third.txt" , io .NopCloser (strings .NewReader ("third newdir data" )))
153+ require .NoError (t , err )
154+
155+ // read all files
156+ file , err := client .Open ("newdir/first.txt" )
157+ require .NoError (t , err )
158+
159+ bs , err := io .ReadAll (file )
160+ require .NoError (t , err )
161+ require .Equal (t , "first newdir data" , string (bs ))
162+
163+ // second file
164+ file , err = client .Open ("newdir/a/second.txt" )
165+ require .NoError (t , err )
166+
167+ bs , err = io .ReadAll (file )
168+ require .NoError (t , err )
169+ require .Equal (t , "second newdir data" , string (bs ))
170+
171+ // third file
172+ file , err = client .Open ("newdir/a/b/third.txt" )
173+ require .NoError (t , err )
174+
175+ bs , err = io .ReadAll (file )
176+ require .NoError (t , err )
177+ require .Equal (t , "third newdir data" , string (bs ))
178+ })
179+
130180 t .Run ("delete" , func (t * testing.T ) {
131181 err := client .Delete ("/missing.txt" )
132182 require .NoError (t , err )
@@ -217,6 +267,7 @@ func TestClient(t *testing.T) {
217267 "archive" , "archive/old.txt" , "archive/empty2.txt" ,
218268 "with-empty" , "with-empty/EMPTY1.txt" , "with-empty/empty_file2.txt" ,
219269 "with-empty/data.txt" , "with-empty/data2.txt" ,
270+ "newdir" , "newdir/first.txt" , "newdir/a" , "newdir/a/second.txt" , "newdir/a/b" , "newdir/a/b/third.txt" ,
220271 })
221272 })
222273
@@ -248,6 +299,7 @@ func TestClient(t *testing.T) {
248299 "bigdata" , "bigdata/large.txt" ,
249300 "archive" , "archive/old.txt" , "archive/empty2.txt" ,
250301 "Upper" , "Upper/names.txt" ,
302+ "newdir" , "newdir/first.txt" , "newdir/a" , "newdir/a/second.txt" , "newdir/a/b" , "newdir/a/b/third.txt" ,
251303 })
252304 })
253305
0 commit comments