Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions packages/fullstack/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ where
{
fn from_response(res: ClientResponse) -> impl Future<Output = Result<Self, ServerFnError>> {
async move {
let status = res.status();

if !status.is_success() {
let ErrorPayload::<serde_json::Value> {
message,
code,
data,
} = res.json().await?;

return Err(ServerFnError::ServerError {
message,
code,
details: data,
});
}

let mut parts = res.make_parts();
let a = A::from_response_parts(&mut parts)?;
let b = B::from_response(res).await?;
Expand All @@ -99,6 +115,22 @@ where
{
fn from_response(res: ClientResponse) -> impl Future<Output = Result<Self, ServerFnError>> {
async move {
let status = res.status();

if !status.is_success() {
let ErrorPayload::<serde_json::Value> {
message,
code,
data,
} = res.json().await?;

return Err(ServerFnError::ServerError {
message,
code,
details: data,
});
}

let mut parts = res.make_parts();
let a = A::from_response_parts(&mut parts)?;
let b = B::from_response_parts(&mut parts)?;
Expand Down Expand Up @@ -293,4 +325,76 @@ mod test {
);
});
}

#[test]
fn tuple_two_path_decodes_ok_on_2xx() {
futures::executor::block_on(async {
let response = build_response(200, "".to_string());
let result = <(TestFromResponse, TestFromResponse)>::from_response(response).await;
assert!(
result.is_ok(),
"expected Ok(..) for HTTP 200 success case, got: {:?}",
result
);
});
}

#[test]
fn tuple_two_parses_error_payload_on_http_error() {
futures::executor::block_on(async {
let body = r#"{
"message": "qwerty",
"code": 400
}"#;
let response = build_response(400, body.to_string());
let result = <(TestFromResponse, TestFromResponse)>::from_response(response).await;
assert!(result.is_err(), "expected Err(..) for HTTP 400 failed case");
assert_eq!(
result.unwrap_err(),
ServerFnError::ServerError {
message: "qwerty".to_string(),
code: 400,
details: None
}
);
});
}

#[test]
fn tuple_three_path_decodes_ok_on_2xx() {
futures::executor::block_on(async {
let response = build_response(200, "".to_string());
let result =
<(TestFromResponse, TestFromResponse, TestFromResponse)>::from_response(response)
.await;
assert!(
result.is_ok(),
"expected Ok(..) for HTTP 200 success case, got: {:?}",
result
);
});
}

#[test]
fn tuple_three_parses_error_payload_on_http_error() {
futures::executor::block_on(async {
let body = r#"{
"message": "qwerty",
"code": 400
}"#;
let response = build_response(400, body.to_string());
let result =
<(TestFromResponse, TestFromResponse, TestFromResponse)>::from_response(response)
.await;
assert!(result.is_err(), "expected Err(..) for HTTP 400 failed case");
assert_eq!(
result.unwrap_err(),
ServerFnError::ServerError {
message: "qwerty".to_string(),
code: 400,
details: None
}
);
});
}
}