u-na-gi
u-na-gi
CDCloudflare Developers
Created by u-na-gi on 9/26/2024 in #🦀rust-on-workers
Hi everyone,
In that case, everything works fine... I couldn't resolve this issue myself, so I decided to proceed with testing using the following approach: 1. Unit Test
#[cfg(test)]
{
println!("{}", "@@@@@@@@@@@@@@@for test");
use std::time::{SystemTime, UNIX_EPOCH};
// Get the current time
let now = SystemTime::now();
let epoch_time = now
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_secs() as usize;

return epoch_time;
}

let now = Date::new_0();
let now = (&now.get_time() / 1000.0).floor() as i64 as usize;

now
#[cfg(test)]
{
println!("{}", "@@@@@@@@@@@@@@@for test");
use std::time::{SystemTime, UNIX_EPOCH};
// Get the current time
let now = SystemTime::now();
let epoch_time = now
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_secs() as usize;

return epoch_time;
}

let now = Date::new_0();
let now = (&now.get_time() / 1000.0).floor() as i64 as usize;

now
I’ve separated the processing for non-wasm targets like this. While it doesn’t make for a perfect unit test, this approach allows me to continue writing unit tests for more important logic as I develop. 2. Blackbox Test
I've created blackbox tests for each endpoint using Deno, with the requirement that wrangler dev must be running beforehand. This isn't the ideal solution, but it allows me to keep progressing with my side project. Ideally, I would like cargo test to be able to bind to D1, but for now, I’ll continue developing and exploring better solutions as I go. Thank you again for your response!
4 replies
CDCloudflare Developers
Created by u-na-gi on 9/26/2024 in #🦀rust-on-workers
Hi everyone,
@kflansburg thanks! yes, i use this. but still suffering the same problem.
#[event(fetch, respond_with_errors)]
pub async fn fetch(
req: HttpRequest,
_env: Env,
_ctx: Context,
) -> Result<axum::http::Response<axum::body::Body>> {
panic::set_hook(Box::new(console_error_panic_hook::hook));
let result = router(_env).await.call(req).await;
// TODO: 二度手間
match result {
Ok(res) => {
console_log!("success");

match res.status() {
axum::http::StatusCode::OK => {
console_log!("status: OK");
}
axum::http::StatusCode::UNAUTHORIZED => {
console_log!("status: UNAUTHORIZED");
}
_ => {
console_log!("status: other");
}
}
Ok(res)
}
Err(e) => {
console_log!("{:?}", e);
Ok(axum::http::Response::builder()
.status(axum::http::StatusCode::INTERNAL_SERVER_ERROR)
.body(axum::body::Body::empty())
.unwrap())
}
}
}
#[event(fetch, respond_with_errors)]
pub async fn fetch(
req: HttpRequest,
_env: Env,
_ctx: Context,
) -> Result<axum::http::Response<axum::body::Body>> {
panic::set_hook(Box::new(console_error_panic_hook::hook));
let result = router(_env).await.call(req).await;
// TODO: 二度手間
match result {
Ok(res) => {
console_log!("success");

match res.status() {
axum::http::StatusCode::OK => {
console_log!("status: OK");
}
axum::http::StatusCode::UNAUTHORIZED => {
console_log!("status: UNAUTHORIZED");
}
_ => {
console_log!("status: other");
}
}
Ok(res)
}
Err(e) => {
console_log!("{:?}", e);
Ok(axum::http::Response::builder()
.status(axum::http::StatusCode::INTERNAL_SERVER_ERROR)
.body(axum::body::Body::empty())
.unwrap())
}
}
}
my fetch function is above one.
4 replies