Skip to content

Commit ea6f530

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

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

sqlx-core/src/net/socket/async_rw_adapter.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ mod tests {
370370
assert_eq!(&received[..], &data[..]);
371371
});
372372
}
373-
374373
}
375374

376375
#[cfg(feature = "_rt-async-io")]

tests/postgres/postgres.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,3 +2218,48 @@ 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").fetch_one(&mut conn).await?;
2237+
assert_eq!(value.0, 2);
2238+
2239+
Ok(())
2240+
}
2241+
2242+
#[sqlx_macros::test]
2243+
async fn it_connects_raw_tokio_with_tls() -> anyhow::Result<()> {
2244+
setup_if_needed();
2245+
2246+
let db_url = env::var("DATABASE_URL")?;
2247+
let options: PgConnectOptions = db_url
2248+
.parse::<PgConnectOptions>()?
2249+
.ssl_mode(sqlx::postgres::PgSslMode::Require);
2250+
2251+
let stream =
2252+
tokio::net::TcpStream::connect(format!("{}:{}", options.get_host(), options.get_port()))
2253+
.await?;
2254+
2255+
let mut conn = PgConnection::connect_raw_tokio(stream, &options).await?;
2256+
conn.ping().await?;
2257+
2258+
// Verify TLS is actually in use by checking the connection's SSL status
2259+
let ssl: bool = sqlx::query_scalar("SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid()")
2260+
.fetch_one(&mut conn)
2261+
.await?;
2262+
assert!(ssl, "expected connection to be using TLS");
2263+
2264+
Ok(())
2265+
}

0 commit comments

Comments
 (0)