Skip to content

Commit 8ef5598

Browse files
committed
add tests for clients
1 parent a9783ed commit 8ef5598

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed

.github/workflows/cargo-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
run: rustup install 1.75.0 --profile minimal
3333

3434
- name: Run cargo test
35-
run: rustup run 1.75.0 cargo test
35+
run: rustup run 1.75.0 cargo test --all-features
3636

3737
- name: Install nightly toolchain
3838
run: |

tests/async_client_test.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use std::sync::Arc;
2+
use bytes::Bytes;
3+
use futures::io::Cursor;
4+
use dropbox_sdk::async_routes::check;
5+
use dropbox_sdk::async_client_trait::*;
6+
use dropbox_sdk::client_trait_common::{HttpRequest, TeamSelect};
7+
use dropbox_sdk::Error;
8+
9+
struct TestAsyncClient;
10+
struct TestRequest {
11+
url: String,
12+
}
13+
14+
impl HttpClient for TestAsyncClient {
15+
type Request = TestRequest;
16+
17+
async fn execute(&self, request: Self::Request, body: Bytes) -> Result<HttpRequestResultRaw, Error> {
18+
match request.url.as_str() {
19+
"https://api.dropboxapi.com/2/check/user" => {
20+
let arg = serde_json::from_slice::<check::EchoArg>(&body)?;
21+
22+
// ensure the future isn't immediately ready
23+
tokio::task::yield_now().await;
24+
25+
Ok(HttpRequestResultRaw {
26+
status: 200,
27+
result_header: None,
28+
content_length: None,
29+
body: Box::new(Cursor::new(format!(r#"{{"result":"{}"}}"#, arg.query).into_bytes())),
30+
})
31+
}
32+
_ => Err(Error::HttpClient(Box::new(std::io::Error::other(format!("unhandled URL {}", request.url))))),
33+
}
34+
}
35+
36+
fn new_request(&self, url: &str) -> Self::Request {
37+
TestRequest{ url: url.to_owned() }
38+
}
39+
40+
async fn update_token(&self, _old_token: Arc<String>) -> Result<bool, Error> {
41+
Ok(true)
42+
}
43+
44+
fn token(&self) -> Option<Arc<String>> {
45+
Some(Arc::new(String::new()))
46+
}
47+
48+
fn path_root(&self) -> Option<&str> {
49+
None
50+
}
51+
52+
fn team_select(&self) -> Option<&TeamSelect> {
53+
None
54+
}
55+
}
56+
57+
impl UserAuthClient for TestAsyncClient {}
58+
59+
impl HttpRequest for TestRequest {
60+
fn set_header(self, _name: &str, _value: &str) -> Self {
61+
self
62+
}
63+
}
64+
65+
#[tokio::test]
66+
async fn test_sync_client() {
67+
let client = TestAsyncClient;
68+
let req = check::EchoArg::default().with_query("foobar".to_owned());
69+
let resp = check::user(&client, &req).await.expect("request must not fail");
70+
if resp.result != req.query {
71+
panic!("response mismatch");
72+
}
73+
}

tests/sync_client_test.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use std::io::Cursor;
2+
use std::sync::Arc;
3+
use dropbox_sdk::sync_routes::check;
4+
use dropbox_sdk::client_trait::*;
5+
use dropbox_sdk::client_trait_common::{HttpRequest, TeamSelect};
6+
use dropbox_sdk::Error;
7+
8+
struct TestSyncClient;
9+
struct TestRequest {
10+
url: String,
11+
}
12+
13+
impl HttpClient for TestSyncClient {
14+
type Request = TestRequest;
15+
16+
fn execute(&self, request: Self::Request, body: &[u8]) -> Result<HttpRequestResultRaw, Error> {
17+
match request.url.as_str() {
18+
"https://api.dropboxapi.com/2/check/user" => {
19+
let arg = serde_json::from_slice::<check::EchoArg>(body)?;
20+
Ok(HttpRequestResultRaw {
21+
status: 200,
22+
result_header: None,
23+
content_length: None,
24+
body: Box::new(Cursor::new(format!(r#"{{"result":"{}"}}"#, arg.query))),
25+
})
26+
}
27+
_ => Err(Error::HttpClient(Box::new(std::io::Error::other(format!("unhandled URL {}", request.url))))),
28+
}
29+
}
30+
31+
fn new_request(&self, url: &str) -> Self::Request {
32+
TestRequest{ url: url.to_owned() }
33+
}
34+
35+
fn update_token(&self, _old_token: Arc<String>) -> Result<bool, Error> {
36+
Ok(true)
37+
}
38+
39+
fn token(&self) -> Option<Arc<String>> {
40+
Some(Arc::new(String::new()))
41+
}
42+
43+
fn path_root(&self) -> Option<&str> {
44+
None
45+
}
46+
47+
fn team_select(&self) -> Option<&TeamSelect> {
48+
None
49+
}
50+
}
51+
52+
impl UserAuthClient for TestSyncClient {}
53+
54+
impl HttpRequest for TestRequest {
55+
fn set_header(self, _name: &str, _value: &str) -> Self {
56+
self
57+
}
58+
}
59+
60+
#[test]
61+
fn test_sync_client() {
62+
let client = TestSyncClient;
63+
let req = check::EchoArg::default().with_query("foobar".to_owned());
64+
let resp = check::user(&client, &req).expect("request must not fail");
65+
if resp.result != req.query {
66+
panic!("response mismatch");
67+
}
68+
}

0 commit comments

Comments
 (0)