// The connection URL parser. import config import std/string import std/test.{assert, assert_equal} @test fn a_full_url :: { match config.parse "postgres://ada:sec%23ret@db.internal:5433/orders?sslmode=require&application_name=shop" { Ok(c) -> { assert_equal c.host "db.internal" assert_equal c.port 5433 assert_equal c.user "ada" assert (c.password == Some("sec#ret")) "the password, decoded" assert_equal c.database "orders" assert (c.tls == config.Require) "sslmode=require" assert_equal c.application "shop" } , Error(e) -> panic e } } @test fn the_bare_url_uses_the_defaults :: { match config.parse "postgres://" { Ok(c) -> { assert_equal c.host "localhost" assert_equal c.port 5432 assert_equal c.user "postgres" assert_equal c.database "postgres" assert (c.password == None) "no password" assert (c.tls == config.Prefer) "sslmode defaults to prefer" } , Error(e) -> panic e } } @test fn a_host_only_url :: { match config.parse "postgresql://db.internal/mydb" { Ok(c) -> { assert_equal c.host "db.internal" assert_equal c.database "mydb" assert_equal c.user "postgres" } , Error(e) -> panic e } } @test fn errors_never_quote_the_url :: { // The URL holds a password; the error must not repeat it. match config.parse "postgres://ada:hunter2@db.internal:notaport/db" { Ok(_) -> panic "a port that is no number must fail" , Error(e) -> { assert_equal e "the port is not a number" assert (!string.contains e "hunter2") "the password leaked into the error" } } match config.parse "mysql://ada:hunter2@h/db" { Ok(_) -> panic "a mysql URL must fail" , Error(e) -> assert_equal e "not a postgres:// URL" } } @test fn sslmode_values :: { assert_equal (tls_of "postgres://h/db?sslmode=disable") "Disable" assert_equal (tls_of "postgres://h/db?sslmode=verify-full") "error" assert_equal (tls_of "postgres://h/db?sslmode=bogus") "error" } fn tls_of :: url: String -> String { match config.parse url { Ok(c) -> "{c.tls:?}", Error(_) -> "error" } }