packages
The Louise packages, each released on its own
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runEn cours#33 · main
0Étoiles
0Forks
3Issues
| 1 | // The connection URL parser. |
| 2 | |
| 3 | import config |
| 4 | import std/string |
| 5 | import std/test.{assert, assert_equal} |
| 6 | |
| 7 | @test |
| 8 | fn a_full_url :: |
| 9 | { |
| 10 | match config.parse |
| 11 | "postgres://ada:sec%23ret@db.internal:5433/orders?sslmode=require&application_name=shop" |
| 12 | { Ok(c) -> |
| 13 | { |
| 14 | assert_equal c.host "db.internal" |
| 15 | assert_equal c.port 5433 |
| 16 | assert_equal c.user "ada" |
| 17 | assert (c.password == Some("sec#ret")) "the password, decoded" |
| 18 | assert_equal c.database "orders" |
| 19 | assert (c.tls == config.Require) "sslmode=require" |
| 20 | assert_equal c.application "shop" |
| 21 | } |
| 22 | , Error(e) -> panic e |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | @test |
| 27 | fn the_bare_url_uses_the_defaults :: |
| 28 | { |
| 29 | match config.parse "postgres://" |
| 30 | { Ok(c) -> |
| 31 | { |
| 32 | assert_equal c.host "localhost" |
| 33 | assert_equal c.port 5432 |
| 34 | assert_equal c.user "postgres" |
| 35 | assert_equal c.database "postgres" |
| 36 | assert (c.password == None) "no password" |
| 37 | assert (c.tls == config.Prefer) "sslmode defaults to prefer" |
| 38 | } |
| 39 | , Error(e) -> panic e |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | @test |
| 44 | fn a_host_only_url :: |
| 45 | { |
| 46 | match config.parse "postgresql://db.internal/mydb" |
| 47 | { Ok(c) -> |
| 48 | { |
| 49 | assert_equal c.host "db.internal" |
| 50 | assert_equal c.database "mydb" |
| 51 | assert_equal c.user "postgres" |
| 52 | } |
| 53 | , Error(e) -> panic e |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | @test |
| 58 | fn errors_never_quote_the_url :: |
| 59 | { |
| 60 | // The URL holds a password; the error must not repeat it. |
| 61 | match config.parse "postgres://ada:hunter2@db.internal:notaport/db" |
| 62 | { Ok(_) -> panic "a port that is no number must fail" |
| 63 | , Error(e) -> |
| 64 | { |
| 65 | assert_equal e "the port is not a number" |
| 66 | assert (!string.contains e "hunter2") "the password leaked into the error" |
| 67 | } |
| 68 | } |
| 69 | match config.parse "mysql://ada:hunter2@h/db" |
| 70 | { Ok(_) -> panic "a mysql URL must fail" |
| 71 | , Error(e) -> assert_equal e "not a postgres:// URL" |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | @test |
| 76 | fn sslmode_values :: |
| 77 | { |
| 78 | assert_equal (tls_of "postgres://h/db?sslmode=disable") "Disable" |
| 79 | assert_equal (tls_of "postgres://h/db?sslmode=verify-full") "error" |
| 80 | assert_equal (tls_of "postgres://h/db?sslmode=bogus") "error" |
| 81 | } |
| 82 | |
| 83 | fn tls_of :: url: String -> String |
| 84 | { |
| 85 | match config.parse url { Ok(c) -> "{c.tls:?}", Error(_) -> "error" } |
| 86 | } |