Skip to content

Commit

Permalink
cleanup after #453 (#454)
Browse files Browse the repository at this point in the history
* cleanup

* Update ws-server/src/tests.rs

* Update http-server/src/tests.rs
  • Loading branch information
niklasad1 authored Sep 8, 2021
1 parent 089aa11 commit b6d93d2
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
14 changes: 7 additions & 7 deletions http-server/src/access_control/hosts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,38 +193,38 @@ mod tests {
#[test]
fn should_reject_when_there_is_no_header() {
let valid = is_host_valid(None, &AllowHosts::Any);
assert_eq!(valid, false);
assert!(!valid);
let valid = is_host_valid(None, &AllowHosts::Only(vec![]));
assert_eq!(valid, false);
assert!(!valid);
}

#[test]
fn should_reject_when_validation_is_disabled() {
let valid = is_host_valid(Some("any"), &AllowHosts::Any);
assert_eq!(valid, true);
assert!(valid);
}

#[test]
fn should_reject_if_header_not_on_the_list() {
let valid = is_host_valid(Some("parity.io"), &AllowHosts::Only(vec![]));
assert_eq!(valid, false);
assert!(!valid);
}

#[test]
fn should_accept_if_on_the_list() {
let valid = is_host_valid(Some("parity.io"), &AllowHosts::Only(vec!["parity.io".into()]));
assert_eq!(valid, true);
assert!(valid);
}

#[test]
fn should_accept_if_on_the_list_with_port() {
let valid = is_host_valid(Some("parity.io:443"), &AllowHosts::Only(vec!["parity.io:443".into()]));
assert_eq!(valid, true);
assert!(valid);
}

#[test]
fn should_support_wildcards() {
let valid = is_host_valid(Some("parity.web3.site:8180"), &AllowHosts::Only(vec!["*.web3.site:*".into()]));
assert_eq!(valid, true);
assert!(valid);
}
}
6 changes: 3 additions & 3 deletions http-server/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,21 @@ async fn server_with_handles() -> (SocketAddr, JoinHandle<Result<(), Error>>, St
module.register_method("notif", |_, _| Ok("")).unwrap();
module
.register_method("should_err", |_, ctx| {
let _ = ctx.err().map_err(|e| CallError::Failed(e.into()))?;
let _ = ctx.err().map_err(CallError::Failed)?;
Ok("err")
})
.unwrap();

module
.register_method("should_ok", |_, ctx| {
let _ = ctx.ok().map_err(|e| CallError::Failed(e.into()))?;
let _ = ctx.ok().map_err(CallError::Failed)?;
Ok("ok")
})
.unwrap();
module
.register_async_method("should_ok_async", |_p, ctx| {
async move {
let _ = ctx.ok().map_err(|e| CallError::Failed(e.into()))?;
let _ = ctx.ok().map_err(CallError::Failed)?;
Ok("ok")
}
.boxed()
Expand Down
9 changes: 6 additions & 3 deletions proc-macros/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,12 @@ pub(crate) fn is_option(ty: &syn::Type) -> bool {
/// Note that `doc comments` are expanded into `#[doc = "some comment"]`
/// Thus, if the attribute starts with `doc` => it's regarded as a doc comment.
pub(crate) fn extract_doc_comments(attrs: &[syn::Attribute]) -> TokenStream2 {
let docs = attrs.iter().filter(|attr| attr.path.is_ident("doc")).filter(|attr| match attr.parse_meta() {
Ok(syn::Meta::NameValue(meta)) if matches!(&meta.lit, syn::Lit::Str(_)) => true,
_ => false,
let docs = attrs.iter().filter(|attr| {
attr.path.is_ident("doc")
&& match attr.parse_meta() {
Ok(syn::Meta::NameValue(meta)) => matches!(&meta.lit, syn::Lit::Str(_)),
_ => false,
}
});
quote! ( #(#docs)* )
}
Expand Down
2 changes: 1 addition & 1 deletion ws-client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ async fn run_request_with_response(response: String) -> Result<JsonValue, Error>
fn assert_error_response(err: Error, exp: JsonRpcErrorObject) {
match &err {
Error::Request(e) => {
let this: JsonRpcError = serde_json::from_str(&e).unwrap();
let this: JsonRpcError = serde_json::from_str(e).unwrap();
assert_eq!(this.error, exp);
}
e => panic!("Expected error: \"{}\", got: {:?}", err, e),
Expand Down
2 changes: 1 addition & 1 deletion ws-server/src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where

this.drive(cx);

if this.futures.len() == 0 {
if this.futures.is_empty() {
Poll::Ready(())
} else {
Poll::Pending
Expand Down
14 changes: 7 additions & 7 deletions ws-server/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ async fn server() -> SocketAddr {

/// Spawns a dummy `JSONRPC v2 WebSocket`
/// It has the following methods:
/// sync methods: `say_hello` and `add`
/// async: `say_hello_async` and `add_sync`
/// other: `invalid_params` (always returns `CallError::InvalidParams`), `call_fail` (always returns `CallError::Failed`), `sleep_for`
/// sync methods: `say_hello` and `add`
/// async: `say_hello_async` and `add_sync`
/// other: `invalid_params` (always returns `CallError::InvalidParams`), `call_fail` (always returns `CallError::Failed`), `sleep_for`
/// Returns the address together with handles for server future and server stop.
async fn server_with_handles() -> (SocketAddr, JoinHandle<()>, StopHandle) {
let server = WsServerBuilder::default().build("127.0.0.1:0").with_default_timeout().await.unwrap().unwrap();
Expand Down Expand Up @@ -122,22 +122,22 @@ async fn server_with_context() -> SocketAddr {

rpc_module
.register_method("should_err", |_p, ctx| {
let _ = ctx.err().map_err(|e| CallError::Failed(e.into()))?;
let _ = ctx.err().map_err(CallError::Failed)?;
Ok("err")
})
.unwrap();

rpc_module
.register_method("should_ok", |_p, ctx| {
let _ = ctx.ok().map_err(|e| CallError::Failed(e.into()))?;
let _ = ctx.ok().map_err(CallError::Failed)?;
Ok("ok")
})
.unwrap();

rpc_module
.register_async_method("should_ok_async", |_p, ctx| {
async move {
let _ = ctx.ok().map_err(|e| CallError::Failed(e.into()))?;
let _ = ctx.ok().map_err(CallError::Failed)?;
// Call some async function inside.
Ok(futures_util::future::ready("ok!").await)
}
Expand All @@ -148,7 +148,7 @@ async fn server_with_context() -> SocketAddr {
rpc_module
.register_async_method("err_async", |_p, ctx| {
async move {
let _ = ctx.ok().map_err(|e| CallError::Failed(e.into()))?;
let _ = ctx.ok().map_err(CallError::Failed)?;
// Async work that returns an error
futures_util::future::err::<(), _>(anyhow!("nah").into()).await
}
Expand Down

0 comments on commit b6d93d2

Please sign in to comment.