From d08138ea633da76d9ca390e4f9e3c5489aee23d1 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 5 Jul 2021 22:03:47 +0200 Subject: Update reqwest and make async. This means adding the full tokio as a dependency. While there isn't much gain to going async in the current cli demo app, a full fledged app may have more to gain by it. First foray into async rust, so I might not do it right... --- tests/zotapi.rs | 106 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 56 insertions(+), 50 deletions(-) (limited to 'tests/zotapi.rs') diff --git a/tests/zotapi.rs b/tests/zotapi.rs index af1d524..10a6c74 100644 --- a/tests/zotapi.rs +++ b/tests/zotapi.rs @@ -35,36 +35,36 @@ fn client() -> zotapi::Client { zotapi::client(&mockito::server_url(), "testuser", "test1234") } -#[test] -fn get_channel_stream() { +#[tokio::test] +async fn get_channel_stream() { let m = default_mock("GET", "/api/z/1.0/channel/stream"); - zotapi::channel_stream().fetch(&client()).unwrap(); + zotapi::channel_stream().fetch(&client()).await.unwrap(); m.assert(); } -#[test] -fn get_network_stream() { +#[tokio::test] +async fn get_network_stream() { let m = default_mock("GET", "/api/z/1.0/network/stream"); - zotapi::network_stream().fetch(&client()).unwrap(); + zotapi::network_stream().fetch(&client()).await.unwrap(); m.assert(); } -#[test] -fn return_error_if_invalid_auth_provided() { +#[tokio::test] +async fn return_error_if_invalid_auth_provided() { let m = mock("GET", "/api/z/1.0/channel/stream") .with_status(401) .with_header("content-type", "text") .with_body("This api requires login") .create(); - let data = zotapi::channel_stream().fetch(&client()); + let data = zotapi::channel_stream().fetch(&client()).await; m.assert(); assert!(data.is_err()); assert_eq!(format!("{:?}", data), "Err(Unauthorized)"); } -#[test] -fn create_new_post() { +#[tokio::test] +async fn create_new_post() { let m = mock_with_authorization("POST", "/api/z/1.0/item/update") .match_body("body=This+is+a+test") .with_status(200) @@ -72,13 +72,13 @@ fn create_new_post() { .with_body("{}") .create(); - let _res = zotapi::item().body("This is a test").create(&client()); + let _res = zotapi::item().body("This is a test").create(&client()).await; m.assert(); } -#[test] -fn create_new_post_with_title() { +#[tokio::test] +async fn create_new_post_with_title() { let m = mock_with_authorization("POST", "/api/z/1.0/item/update") .match_body(Matcher::AllOf(vec![ Matcher::UrlEncoded("title".to_string(), "A title".to_string()), @@ -92,13 +92,14 @@ fn create_new_post_with_title() { let _res = zotapi::item() .title("A title") .body("This is a test") - .create(&client()); + .create(&client()) + .await; m.assert(); } -#[test] -fn create_new_post_limited_to_one_privacy_group() { +#[tokio::test] +async fn create_new_post_limited_to_one_privacy_group() { // For some reason this mock does not match. // Visually inspecting the payload it does however seem valid, // so not sure how to handle this with the current framework. @@ -118,17 +119,18 @@ fn create_new_post_limited_to_one_privacy_group() { let _res = zotapi::item() .body("This is a test") .group_allow("grouphash") - .create(&client()); + .create(&client()) + .await; //m.assert(); } -#[test] -fn upload_item_with_media_file() { +#[tokio::test] +async fn upload_item_with_media_file() { let m = mock_with_authorization("POST", "/api/z/1.0/item/update") .match_header( "content-type", - Matcher::Regex("^multipart/form-data; boundary=.+$".into()), + Matcher::Regex("multipart/form-data; boundary=.+".into()), ) .match_body(Matcher::Regex( "--.+\r\n".to_owned() @@ -139,7 +141,6 @@ fn upload_item_with_media_file() { + "\r\nA title\r\n" + "--.+\r\n" + "Content-Disposition: form-data; name=\"media\"; filename=\"testfile.txt\"\r\n" - + "Content-Type: text/plain\r\n" + "\r\ntestfile contents\n" + "\r\n--.+--\r\n", )) @@ -152,13 +153,14 @@ fn upload_item_with_media_file() { .title("A title") .body("This is a test") .file("tests/fixtures/testfile.txt") - .create(&client()); + .create(&client()) + .await; m.assert(); } -#[test] -fn upload_item_with_two_files() { +#[tokio::test] +async fn upload_item_with_two_files() { let m = mock_with_authorization("POST", "/api/z/1.0/item/update") .match_header( "content-type", @@ -173,11 +175,9 @@ fn upload_item_with_two_files() { + "\r\nA title\r\n" + "--.+\r\n" + "Content-Disposition: form-data; name=\"media\"; filename=\"testfile.txt\"\r\n" - + "Content-Type: text/plain\r\n" + "\r\ntestfile contents\n" + "\r\n--.+\r\n" + "Content-Disposition: form-data; name=\"media\"; filename=\"testfile.txt\"\r\n" - + "Content-Type: text/plain\r\n" + "\r\ntestfile contents\n" + "\r\n--.+--\r\n", )) @@ -191,7 +191,8 @@ fn upload_item_with_two_files() { .body("This is a test") .file("tests/fixtures/testfile.txt") .file("tests/fixtures/testfile.txt") - .create(&client()); + .create(&client()) + .await; m.assert(); } @@ -225,8 +226,8 @@ const EMPTY_XCHAN: &str = r#"{ "deleted": 0 }"#; -#[test] -fn fetch_xchan_by_address() { +#[tokio::test] +async fn fetch_xchan_by_address() { let m = mock_with_authorization("GET", "/api/z/1.0/xchan?address=test%40test.com") .with_status(200) .with_header("content-type", "application/json") @@ -236,13 +237,14 @@ fn fetch_xchan_by_address() { let _res = zotapi::XChan::z() .by_address("test@test.com") .fetch(&client()) + .await .unwrap(); m.assert(); } -#[test] -fn fetch_xchan_by_hash() { +#[tokio::test] +async fn fetch_xchan_by_hash() { let m = mock_with_authorization("GET", "/api/z/1.0/xchan?hash=baffebaff") .with_status(200) .with_header("content-type", "application/json") @@ -252,13 +254,14 @@ fn fetch_xchan_by_hash() { let _res = zotapi::XChan::z() .by_hash("baffebaff") .fetch(&client()) + .await .unwrap(); m.assert(); } -#[test] -fn fetch_xchan_by_guid() { +#[tokio::test] +async fn fetch_xchan_by_guid() { let m = mock_with_authorization("GET", "/api/z/1.0/xchan?guid=baffebaff-baff-baff") .with_status(200) .with_header("content-type", "application/json") @@ -268,20 +271,21 @@ fn fetch_xchan_by_guid() { let _res = zotapi::XChan::z() .by_guid("baffebaff-baff-baff") .fetch(&client()) + .await .unwrap(); m.assert(); } -#[test] -fn fetch_connections() { +#[tokio::test] +async fn fetch_connections() { let m = default_mock("GET", "/api/z/1.0/abook"); - let _res = zotapi::Abook::z().fetch(&client()).unwrap(); + let _res = zotapi::Abook::z().fetch(&client()).await.unwrap(); m.assert(); } -#[test] -fn fetch_abconfig() { +#[tokio::test] +async fn fetch_abconfig() { let data = r#" [ { @@ -308,7 +312,7 @@ fn fetch_abconfig() { .with_body(&data) .create(); - let res = zotapi::ABConfig::z().fetch(&client()).unwrap(); + let res = zotapi::ABConfig::z().fetch(&client()).await.unwrap(); m.assert(); assert_eq!(res.len(), 2); @@ -316,27 +320,27 @@ fn fetch_abconfig() { assert_eq!(res[1].k, "key2"); } -#[test] -fn fetch_abconfig_for_specific_contact() { +#[tokio::test] +async fn fetch_abconfig_for_specific_contact() { let m = mock_with_authorization("GET", "/api/z/1.0/abconfig") .match_query(Matcher::UrlEncoded("abook_id".into(), 42.to_string())) .with_status(200) .with_body("[]") .create(); - zotapi::ABConfig::z().with_abook_id(42).fetch(&client()).unwrap(); + zotapi::ABConfig::z().with_abook_id(42).fetch(&client()).await.unwrap(); m.assert(); } -#[test] -fn fetch_privacy_groups() { +#[tokio::test] +async fn fetch_privacy_groups() { let m = default_mock("GET", "/api/z/1.0/group"); - let _res = zotapi::group().fetch(&client()).unwrap(); + let _res = zotapi::group().fetch(&client()).await.unwrap(); m.assert(); } -#[test] -fn fetch_members_of_group_by_group_id() { +#[tokio::test] +async fn fetch_members_of_group_by_group_id() { let m = mock_with_authorization("GET", "/api/z/1.0/group_members") .match_query(Matcher::UrlEncoded( "group_id".to_string(), @@ -350,13 +354,14 @@ fn fetch_members_of_group_by_group_id() { let _res = zotapi::group_members() .by_group_id(42) .fetch(&client()) + .await .unwrap(); m.assert(); } -#[test] -fn fetch_members_of_group_by_group_name() { +#[tokio::test] +async fn fetch_members_of_group_by_group_name() { let m = mock_with_authorization("GET", "/api/z/1.0/group_members") .match_query(Matcher::UrlEncoded( "group_name".into(), @@ -370,6 +375,7 @@ fn fetch_members_of_group_by_group_name() { let _res = zotapi::group_members() .by_group_name("Friends of pain") .fetch(&client()) + .await .unwrap(); m.assert(); -- cgit v1.2.3