Unverified Commit 9be85fad authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

chore(turbo-tasks-fetch): Port tests from httpmock to mockito (#80341)

This should help speed up builds that include test targets.

`httpmock` was a bad fit for us because it has a really large dependency list that includes `async-std`.

`mockito` appears to be much more popular. It depends on `hyper` and `tokio`, which we already pull in.
parent 9503b880
This diff is collapsed.
...@@ -33,7 +33,7 @@ turbo-tasks-fs = { workspace = true } ...@@ -33,7 +33,7 @@ turbo-tasks-fs = { workspace = true }
turbopack-core = { workspace = true } turbopack-core = { workspace = true }
[dev-dependencies] [dev-dependencies]
httpmock = { workspace = true } mockito = { version = "1.7.0", default-features = false }
tokio = { workspace = true, features = ["full"] } tokio = { workspace = true, features = ["full"] }
turbo-tasks-testing = { workspace = true } turbo-tasks-testing = { workspace = true }
turbo-tasks-backend = { workspace = true } turbo-tasks-backend = { workspace = true }
......
...@@ -87,7 +87,7 @@ pub async fn fetch( ...@@ -87,7 +87,7 @@ pub async fn fetch(
Ok(Vc::cell(Ok(HttpResponse { Ok(Vc::cell(Ok(HttpResponse {
status, status,
body: HttpResponseBody::resolved_cell(HttpResponseBody(body)), body: HttpResponseBody(body).resolved_cell(),
} }
.resolved_cell()))) .resolved_cell())))
} }
......
...@@ -13,25 +13,26 @@ static REGISTRATION: Registration = register!(turbo_tasks_fetch::register); ...@@ -13,25 +13,26 @@ static REGISTRATION: Registration = register!(turbo_tasks_fetch::register);
#[tokio::test] #[tokio::test]
async fn basic_get() { async fn basic_get() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let server = httpmock::MockServer::start(); let mut server = mockito::Server::new_async().await;
let resource_mock = server.mock(|when, then| { let resource_mock = server
when.path("/foo.woff"); .mock("GET", "/foo.woff")
then.status(200).body("responsebody"); .with_body("responsebody")
}); .create_async()
.await;
let result = &*fetch(server.url("/foo.woff").into(), None, Vc::cell(None))
.await let response = &*fetch(
.unwrap(); RcStr::from(format!("{}/foo.woff", server.url())),
resource_mock.assert(); /* user_agent */ None,
/* proxy */ Vc::cell(None),
match result { )
Err(_) => panic!(), .await?
Ok(response) => { .unwrap()
let response = response.await?; .await?;
assert_eq!(response.status, 200);
assert_eq!(*response.body.to_string().await?, "responsebody"); resource_mock.assert_async().await;
}
} assert_eq!(response.status, 200);
assert_eq!(*response.body.to_string().await?, "responsebody");
anyhow::Ok(()) anyhow::Ok(())
}) })
.await .await
...@@ -41,24 +42,27 @@ async fn basic_get() { ...@@ -41,24 +42,27 @@ async fn basic_get() {
#[tokio::test] #[tokio::test]
async fn sends_user_agent() { async fn sends_user_agent() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let server = httpmock::MockServer::start(); let mut server = mockito::Server::new_async().await;
let resource_mock = server.mock(|when, then| { let resource_mock = server
when.path("/foo.woff").header("User-Agent", "foo"); .mock("GET", "/foo.woff")
then.status(200).body("responsebody"); .match_header("User-Agent", "mock-user-agent")
}); .with_body("responsebody")
.create_async()
let result = &*fetch( .await;
server.url("/foo.woff").into(),
Some(rcstr!("foo")), eprintln!("{}", server.url());
Vc::cell(None),
let response = &*fetch(
RcStr::from(format!("{}/foo.woff", server.url())),
Some(rcstr!("mock-user-agent")),
/* proxy */ Vc::cell(None),
) )
.await .await?
.unwrap(); .unwrap()
resource_mock.assert(); .await?;
let Ok(response) = result else { panic!() }; resource_mock.assert_async().await;
let response = response.await?;
assert_eq!(response.status, 200); assert_eq!(response.status, 200);
assert_eq!(*response.body.to_string().await?, "responsebody"); assert_eq!(*response.body.to_string().await?, "responsebody");
anyhow::Ok(()) anyhow::Ok(())
...@@ -72,32 +76,34 @@ async fn sends_user_agent() { ...@@ -72,32 +76,34 @@ async fn sends_user_agent() {
#[tokio::test] #[tokio::test]
async fn invalidation_does_not_invalidate() { async fn invalidation_does_not_invalidate() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let server = httpmock::MockServer::start(); let mut server = mockito::Server::new_async().await;
let resource_mock = server.mock(|when, then| { let resource_mock = server
when.path("/foo.woff").header("User-Agent", "foo"); .mock("GET", "/foo.woff")
then.status(200).body("responsebody"); .with_body("responsebody")
}); .with_header("Cache-Control", "no-store")
.create_async()
let url: RcStr = server.url("/foo.woff").into(); .await;
let user_agent = Some(rcstr!("foo"));
let proxy = Vc::cell(None); let url = RcStr::from(format!("{}/foo.woff", server.url()));
let result = &*fetch(url.clone(), user_agent.clone(), proxy).await?; let proxy_vc = Vc::cell(None);
resource_mock.assert(); let response = &*fetch(url.clone(), /* user_agent */ None, proxy_vc)
.await?
let Ok(response_vc) = result else { panic!() }; .unwrap()
let response = response_vc.await?; .await?;
resource_mock.assert_async().await;
assert_eq!(response.status, 200); assert_eq!(response.status, 200);
assert_eq!(*response.body.to_string().await?, "responsebody"); assert_eq!(*response.body.to_string().await?, "responsebody");
let second_result = &*fetch(url, user_agent, proxy).await?; let second_response = &*fetch(url.clone(), /* user_agent */ None, proxy_vc)
let Ok(second_response_vc) = second_result else { .await?
panic!() .unwrap()
}; .await?;
let second_response = second_response_vc.await?;
// Assert that a second request is never sent -- the result is cached via turbo tasks
resource_mock.expect(1).assert_async().await;
// Assert that a second request is never sent -- the result is cached via turbo
// tasks
resource_mock.assert_hits(1);
assert_eq!(response, second_response); assert_eq!(response, second_response);
anyhow::Ok(()) anyhow::Ok(())
}) })
...@@ -108,47 +114,58 @@ async fn invalidation_does_not_invalidate() { ...@@ -108,47 +114,58 @@ async fn invalidation_does_not_invalidate() {
#[tokio::test] #[tokio::test]
async fn errors_on_failed_connection() { async fn errors_on_failed_connection() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let url = rcstr!("https://doesnotexist/foo.woff"); // Try to connect to port 0 on localhost, which is never valid and immediately returns
let result = &*fetch(url.clone(), None, Vc::cell(None)).await?; // `ECONNREFUSED`.
let Err(err_vc) = result else { // Other values (e.g. domain name, reservered IP address block) may result in long timeouts.
panic!() let url = rcstr!("http://127.0.0.1:0/foo.woff");
}; let response_vc = fetch(url.clone(), None, Vc::cell(None));
let err = &*err_vc.await?; let err_vc = &*response_vc.await?.unwrap_err();
let err = err_vc.await?;
assert_eq!(*err.kind.await?, FetchErrorKind::Connect); assert_eq!(*err.kind.await?, FetchErrorKind::Connect);
assert_eq!(*err.url.await?, url); assert_eq!(*err.url.await?, url);
let issue = err_vc.to_issue(IssueSeverity::Error.into(), get_issue_context()); let issue = err_vc.to_issue(IssueSeverity::Error.into(), get_issue_context());
assert_eq!(*issue.severity().await?, IssueSeverity::Error); assert_eq!(*issue.severity().await?, IssueSeverity::Error);
assert_eq!(*issue.description().await?.unwrap().await?, StyledString::Text(rcstr!("There was an issue establishing a connection while requesting https://doesnotexist/foo.woff."))); assert_eq!(
*issue.description().await?.unwrap().await?,
StyledString::Text(rcstr!(
"There was an issue establishing a connection while requesting \
http://127.0.0.1:0/foo.woff."
))
);
anyhow::Ok(()) anyhow::Ok(())
}) })
.await.unwrap() .await
.unwrap()
} }
#[tokio::test] #[tokio::test]
async fn errors_on_404() { async fn errors_on_404() {
run(&REGISTRATION, || async { run(&REGISTRATION, || async {
let server = httpmock::MockServer::start(); let mut server = mockito::Server::new_async().await;
let resource_url: RcStr = server.url("/").into(); let resource_mock = server
let result = &*fetch(resource_url.clone(), None, Vc::cell(None)) .mock("GET", "/")
.await .with_status(404)
.unwrap(); .create_async()
let Err(err_vc) = result else { panic!() }; .await;
let err = &*err_vc.await?;
let url = RcStr::from(server.url());
let response_vc = fetch(url.clone(), None, Vc::cell(None));
let err_vc = &*response_vc.await?.unwrap_err();
let err = err_vc.await?;
resource_mock.assert_async().await;
assert!(matches!(*err.kind.await?, FetchErrorKind::Status(404))); assert!(matches!(*err.kind.await?, FetchErrorKind::Status(404)));
assert_eq!(*err.url.await?, resource_url); assert_eq!(*err.url.await?, url);
let issue = err_vc.to_issue(IssueSeverity::Error.into(), get_issue_context()); let issue = err_vc.to_issue(IssueSeverity::Error.into(), get_issue_context());
assert_eq!(*issue.severity().await?, IssueSeverity::Error); assert_eq!(*issue.severity().await?, IssueSeverity::Error);
assert_eq!( assert_eq!(
*issue.description().await?.unwrap().await?, *issue.description().await?.unwrap().await?,
StyledString::Text( StyledString::Text(RcStr::from(format!(
format!( "Received response with status 404 when requesting {url}"
"Received response with status 404 when requesting {}", )))
&resource_url
)
.into()
)
); );
anyhow::Ok(()) anyhow::Ok(())
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment