-
Notifications
You must be signed in to change notification settings - Fork 172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ws client]: add wss
test + refactor WebSocketTransport builder
#209
Conversation
@@ -207,3 +207,10 @@ async fn ws_more_request_than_buffer_should_not_deadlock() { | |||
req.await.unwrap(); | |||
} | |||
} | |||
|
|||
#[tokio::test] | |||
async fn wss_works() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to be complete here:
- assert that
ws://
works - assert that omitting the port works (and uses the default)
- assert that providing a wacky port fails, bigger than
u16::MAX
, negative number,0
etc - assert that host name must be ascii (does it?)
@@ -37,6 +37,15 @@ use thiserror::Error; | |||
|
|||
type TlsOrPlain = crate::stream::EitherStream<TcpStream, TlsStream<TcpStream>>; | |||
|
|||
#[derive(Clone)] | |||
pub struct Host(String); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduced a new type to avoid nasty bugs when several params has the same type.
It could be a pub struct Host<'a>(&'a str)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think String
is fine.
@dvdplm I think this is ready for another round of review :) |
|
||
#[tokio::test] | ||
async fn http_with_non_ascii_url_doesnt_hang_or_panic() { | ||
let client = HttpClient::new("http://♥♥♥♥♥♥∀∂", HttpConfig::default()).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.rs/url/2.2.0/url/enum.Host.html#variant.Domain is the reason why it's doesn't fail parsing the URL in the constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and in the WebSocket
case the host is resolved to a SockAddr
because an actual connection is established in the constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL!
ws-client/src/transport.rs
Outdated
_ => return Err(WsHandshakeError::Url("URL scheme not supported, expects 'ws' or 'wss'".into())), | ||
}; | ||
let host = url.host_str().ok_or_else(|| WsHandshakeError::Url("No host in URL".into()))?.into(); | ||
let sockaddrs: Vec<SocketAddr> = url.socket_addrs(|| None).map_err(WsHandshakeError::ResolutionFailed)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switched to https://docs.rs/url/2.2.0/url/struct.Url.html#method.socket_addrs instead which provides the default port number if it's missing such that we don't have to do it manually anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we should add a comment stating this and what the default port is?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the best option is the document this on WsConfig
i.e, default ports is ws - 80, wss - 433
^^
Here we could add a comment that sock_addrs()
uses https://docs.rs/url/2.2.0/url/struct.Url.html#method.port_or_known_default if the port is missing
ws-client/src/transport.rs
Outdated
@@ -146,43 +159,8 @@ pub enum WsConnectError { | |||
|
|||
/// Creates a new WebSocket connection based on [`WsConfig`](crate::WsConfig) represented as a Sender and Receiver pair. | |||
pub async fn websocket_connection(config: WsConfig<'_>) -> Result<(Sender, Receiver), WsHandshakeError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you like the change i.e, provide TryFrom<WsConfig> for WebSocketTransportClient { ... }
then I think we could remove this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
wss
testwss
test + refactor WebSocketTransport builder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few minor nits.
|
||
#[tokio::test] | ||
async fn http_with_non_ascii_url_doesnt_hang_or_panic() { | ||
let client = HttpClient::new("http://♥♥♥♥♥♥∀∂", HttpConfig::default()).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL!
@@ -37,6 +37,15 @@ use thiserror::Error; | |||
|
|||
type TlsOrPlain = crate::stream::EitherStream<TcpStream, TlsStream<TcpStream>>; | |||
|
|||
#[derive(Clone)] | |||
pub struct Host(String); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think String
is fine.
ws-client/src/transport.rs
Outdated
_ => return Err(WsHandshakeError::Url("URL scheme not supported, expects 'ws' or 'wss'".into())), | ||
}; | ||
let host = url.host_str().ok_or_else(|| WsHandshakeError::Url("No host in URL".into()))?.into(); | ||
let sockaddrs: Vec<SocketAddr> = url.socket_addrs(|| None).map_err(WsHandshakeError::ResolutionFailed)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we should add a comment stating this and what the default port is?
"wss" => Mode::Tls, | ||
_ => return Err(WsHandshakeError::Url("URL scheme not supported, expects 'ws' or 'wss'".into())), | ||
}; | ||
let host = url.host_str().ok_or_else(|| WsHandshakeError::Url("No host in URL".into()))?.into(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the error case taken care of by the url::Url::parse()
call on line 276? Oh, I see, it is None
for unix sockets... Ok, fine then.
The error is bit funny though, it's not really a handshaking error. Can we use a more appropriate error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error is bit funny though, it's not really a handshaking error. Can we use a more appropriate error?
indeed, let's fix that in another PR.
impl<'a> TryFrom<WsConfig<'a>> for WsTransportClientBuilder<'a> { | ||
type Error = WsHandshakeError; | ||
|
||
fn try_from(config: WsConfig<'a>) -> Result<Self, Self::Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I quite like this pattern of converting a config into a builder.
No description provided.