| 1 | import net.ftp |
| 2 | |
| 3 | fn check_for_network(tname string) ? { |
| 4 | $if !network ? { |
| 5 | eprintln('> skipping ${tname:-20}, since `-d network` is not passed') |
| 6 | return none |
| 7 | } |
| 8 | } |
| 9 | |
| 10 | fn test_ftp_client() { |
| 11 | check_for_network(@FN) or { return } |
| 12 | // Note: this function makes network calls to external servers, |
| 13 | // that is why it is not a very good idea to run it in CI. |
| 14 | // If you want to run it manually, use: |
| 15 | // `v -d network vlib/net/ftp/ftp_test.v` |
| 16 | mut zftp := ftp.new() |
| 17 | defer { zftp.close() or {} } |
| 18 | server := 'ftp.sunet.se:21' |
| 19 | connect_result := zftp.connect(server) or { |
| 20 | eprintln('> skipping test_ftp_client: could not connect to ${server}: ${err}') |
| 21 | return |
| 22 | } |
| 23 | assert connect_result |
| 24 | println('> connected to ${server}') |
| 25 | login_result := zftp.login('ftp', 'ftp')! |
| 26 | assert login_result |
| 27 | pwd := zftp.pwd()! |
| 28 | assert pwd.len > 0 |
| 29 | zftp.cd('/')! |
| 30 | dir_list1 := zftp.dir()! |
| 31 | assert dir_list1.len > 0 |
| 32 | } |
| 33 | |
| 34 | fn test_ftp_get() ! { |
| 35 | check_for_network(@FN) or { return } |
| 36 | mut zftp := ftp.new() |
| 37 | defer { zftp.close() or {} } |
| 38 | server := 'ftp.sunet.se:21' |
| 39 | connect_result := zftp.connect(server) or { |
| 40 | eprintln('> skipping test_ftp_get: could not connect to ${server}: ${err}') |
| 41 | return |
| 42 | } |
| 43 | assert connect_result |
| 44 | println('> connected to ${server}') |
| 45 | login_result := zftp.login('ftp', 'ftp')! |
| 46 | assert login_result |
| 47 | pwd := zftp.pwd()! |
| 48 | assert pwd.len > 0 |
| 49 | mut txt := zftp.get('robots.txt')! |
| 50 | assert txt[0] == 35 // first byte is # char |
| 51 | zftp.pwd()! |
| 52 | zftp.cd('pub')! |
| 53 | zftp.cd('..')! |
| 54 | zftp.get('robots.txt')! |
| 55 | } |
| 56 | |