Skip to content

Commit aa95915

Browse files
committed
test: add integration tests for connect_raw_tokio with and without TLS
1 parent 6914d5f commit aa95915

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

tests/postgres/postgres.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,3 +2218,50 @@ async fn it_can_recover_from_copy_in_invalid_params() -> anyhow::Result<()> {
22182218
)
22192219
.await
22202220
}
2221+
2222+
#[sqlx_macros::test]
2223+
async fn it_connects_raw_tokio() -> anyhow::Result<()> {
2224+
setup_if_needed();
2225+
2226+
let db_url = env::var("DATABASE_URL")?;
2227+
let options: PgConnectOptions = db_url.parse()?;
2228+
2229+
let stream =
2230+
tokio::net::TcpStream::connect(format!("{}:{}", options.get_host(), options.get_port()))
2231+
.await?;
2232+
2233+
let mut conn = PgConnection::connect_raw_tokio(stream, &options).await?;
2234+
conn.ping().await?;
2235+
2236+
let value: (i32,) = sqlx::query_as("SELECT 1 + 1")
2237+
.fetch_one(&mut conn)
2238+
.await?;
2239+
assert_eq!(value.0, 2);
2240+
2241+
Ok(())
2242+
}
2243+
2244+
#[sqlx_macros::test]
2245+
async fn it_connects_raw_tokio_with_tls() -> anyhow::Result<()> {
2246+
setup_if_needed();
2247+
2248+
let db_url = env::var("DATABASE_URL")?;
2249+
let options: PgConnectOptions = db_url
2250+
.parse::<PgConnectOptions>()?
2251+
.ssl_mode(sqlx::postgres::PgSslMode::Require);
2252+
2253+
let stream =
2254+
tokio::net::TcpStream::connect(format!("{}:{}", options.get_host(), options.get_port()))
2255+
.await?;
2256+
2257+
let mut conn = PgConnection::connect_raw_tokio(stream, &options).await?;
2258+
conn.ping().await?;
2259+
2260+
// Verify TLS is actually in use by checking the connection's SSL status
2261+
let ssl: bool = sqlx::query_scalar("SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid()")
2262+
.fetch_one(&mut conn)
2263+
.await?;
2264+
assert!(ssl, "expected connection to be using TLS");
2265+
2266+
Ok(())
2267+
}

0 commit comments

Comments
 (0)