From 73bd2b1f3e78b7fd5ccb95de91ea2e1e1749d040 Mon Sep 17 00:00:00 2001 From: Joshua Gilman Date: Fri, 20 Aug 2021 22:25:43 -0700 Subject: [PATCH] Adds tests for raw request data and rustify_derive --- Cargo.toml | 1 + tests/common.rs | 45 +++++++++++++++- tests/endpoint.rs | 85 +++++++++++++------------------ tests/macro.rs | 5 ++ tests/macro/empty_attr.rs | 13 +++++ tests/macro/empty_attr.stderr | 19 +++++++ tests/macro/invalid_data.rs | 23 +++++++++ tests/macro/invalid_data.stderr | 23 +++++++++ tests/macro/invalid_method.rs | 9 ++++ tests/macro/invalid_method.stderr | 13 +++++ tests/macro/invalid_result.rs | 9 ++++ tests/macro/invalid_result.stderr | 13 +++++ tests/macro/invalid_type.rs | 9 ++++ tests/macro/invalid_type.stderr | 19 +++++++ tests/macro/no_attr.rs | 8 +++ tests/macro/no_attr.stderr | 15 ++++++ tests/macro/no_path.rs | 9 ++++ tests/macro/no_path.stderr | 15 ++++++ 18 files changed, 283 insertions(+), 50 deletions(-) create mode 100644 tests/macro.rs create mode 100644 tests/macro/empty_attr.rs create mode 100644 tests/macro/empty_attr.stderr create mode 100644 tests/macro/invalid_data.rs create mode 100644 tests/macro/invalid_data.stderr create mode 100644 tests/macro/invalid_method.rs create mode 100644 tests/macro/invalid_method.stderr create mode 100644 tests/macro/invalid_result.rs create mode 100644 tests/macro/invalid_result.stderr create mode 100644 tests/macro/invalid_type.rs create mode 100644 tests/macro/invalid_type.stderr create mode 100644 tests/macro/no_attr.rs create mode 100644 tests/macro/no_attr.stderr create mode 100644 tests/macro/no_path.rs create mode 100644 tests/macro/no_path.stderr diff --git a/Cargo.toml b/Cargo.toml index 409a88b..4d2faa7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,3 +27,4 @@ env_logger = "0.9.0" httpmock = "0.6.2" serde_with = "1.9.4" test-env-log = "0.2.7" +trybuild = "1.0.45" diff --git a/tests/common.rs b/tests/common.rs index d8c2047..c8d04c2 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -1,5 +1,11 @@ use httpmock::prelude::*; -use rustify::clients::reqwest::ReqwestClient; +use rustify::{ + clients::reqwest::ReqwestClient, + endpoint::{Endpoint, MiddleWare}, + errors::ClientError, +}; +use serde::Deserialize; +use serde_json::Value; pub struct TestServer { pub server: MockServer, @@ -26,3 +32,40 @@ impl Default for TestServer { } } } + +#[derive(Debug, Deserialize)] +pub struct TestResponse { + pub age: u8, +} + +#[derive(Debug, Deserialize)] +pub struct TestWrapper { + pub result: Value, +} + +pub struct Middle {} +impl MiddleWare for Middle { + fn request( + &self, + _: &E, + req: &mut rustify::client::Request, + ) -> Result<(), ClientError> { + req.headers + .push(("X-API-Token".to_string(), "mytoken".to_string())); + Ok(()) + } + fn response( + &self, + _: &E, + resp: &mut rustify::client::Response, + ) -> Result<(), ClientError> { + let err_body = resp.body.clone(); + let wrapper: TestWrapper = + serde_json::from_slice(&resp.body).map_err(|e| ClientError::ResponseParseError { + source: Box::new(e), + content: String::from_utf8(err_body).ok(), + })?; + resp.body = wrapper.result.to_string().as_bytes().to_vec(); + Ok(()) + } +} diff --git a/tests/endpoint.rs b/tests/endpoint.rs index d277377..02b6628 100644 --- a/tests/endpoint.rs +++ b/tests/endpoint.rs @@ -2,56 +2,16 @@ mod common; use std::{fmt::Debug, marker::PhantomData}; -use common::TestServer; +use common::{Middle, TestResponse, TestServer}; use derive_builder::Builder; use httpmock::prelude::*; -use rustify::{ - endpoint::{Endpoint, MiddleWare}, - errors::ClientError, -}; +use rustify::{endpoint::Endpoint, errors::ClientError}; use rustify_derive::Endpoint; use serde::{de::DeserializeOwned, Deserialize, Serialize}; -use serde_json::{json, Value}; +use serde_json::json; use serde_with::skip_serializing_none; use test_env_log::test; -#[derive(Debug, Deserialize)] -struct TestResponse { - age: u8, -} - -#[derive(Debug, Deserialize)] -struct TestWrapper { - result: Value, -} - -struct Middle {} -impl MiddleWare for Middle { - fn request( - &self, - _: &E, - req: &mut rustify::client::Request, - ) -> Result<(), ClientError> { - req.headers - .push(("X-API-Token".to_string(), "mytoken".to_string())); - Ok(()) - } - fn response( - &self, - _: &E, - resp: &mut rustify::client::Response, - ) -> Result<(), ClientError> { - let err_body = resp.body.clone(); - let wrapper: TestWrapper = - serde_json::from_slice(&resp.body).map_err(|e| ClientError::ResponseParseError { - source: Box::new(e), - content: String::from_utf8(err_body).ok(), - })?; - resp.body = wrapper.result.to_string().as_bytes().to_vec(); - Ok(()) - } -} - #[test] fn test_path() { #[derive(Debug, Endpoint, Serialize)] @@ -71,7 +31,7 @@ fn test_path() { } #[test] -fn test_path_method() { +fn test_method() { #[derive(Debug, Endpoint, Serialize)] #[endpoint(path = "test/path", method = "POST")] struct Test {} @@ -89,7 +49,7 @@ fn test_path_method() { } #[test] -fn test_path_query() { +fn test_query() { #[derive(Debug, Endpoint, Serialize)] #[endpoint(path = "test/path", method = "POST")] struct Test { @@ -120,7 +80,7 @@ fn test_path_query() { } #[test] -fn test_path_method_with_format() { +fn test_path_with_format() { #[derive(Debug, Endpoint, Serialize)] #[endpoint(path = "test/path/{self.name}", method = "POST")] struct Test { @@ -143,7 +103,7 @@ fn test_path_method_with_format() { } #[test] -fn test_path_method_with_data() { +fn test_data() { #[derive(Debug, Endpoint, Serialize)] #[endpoint(path = "test/path", method = "POST")] struct Test { @@ -167,7 +127,34 @@ fn test_path_method_with_data() { } #[test] -fn test_path_result() { +fn test_raw_data() { + #[derive(Debug, Endpoint, Serialize)] + #[endpoint(path = "test/path/{self.name}", method = "POST")] + struct Test { + name: String, + #[endpoint(data)] + data: Vec, + } + + let t = TestServer::default(); + let e = Test { + name: "test".to_string(), + data: "somebits".as_bytes().to_vec(), + }; + let m = t.server.mock(|when, then| { + when.method(POST) + .path("/test/path/test") + .body_contains("somebits"); + then.status(200); + }); + let r = e.exec(&t.client); + + m.assert(); + assert!(r.is_ok()) +} + +#[test] +fn test_result() { #[derive(Debug, Endpoint, Serialize)] #[endpoint(path = "test/path", result = "TestResponse")] struct Test {} @@ -232,7 +219,7 @@ fn test_mutate() { } #[test] -fn test_raw() { +fn test_raw_response() { #[derive(Debug, Endpoint, Serialize)] #[endpoint(path = "test/path", result = "TestResponse")] struct Test {} diff --git a/tests/macro.rs b/tests/macro.rs new file mode 100644 index 0000000..f3710d5 --- /dev/null +++ b/tests/macro.rs @@ -0,0 +1,5 @@ +#[test] +fn test_macro() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/macro/*.rs"); +} diff --git a/tests/macro/empty_attr.rs b/tests/macro/empty_attr.rs new file mode 100644 index 0000000..f052d6d --- /dev/null +++ b/tests/macro/empty_attr.rs @@ -0,0 +1,13 @@ +use rustify::endpoint::Endpoint; +use rustify_derive::Endpoint; +use serde::Serialize; + +#[derive(Debug, Endpoint, Serialize)] +#[endpoint] +struct Test {} + +#[derive(Debug, Endpoint, Serialize)] +#[endpoint()] +struct TestTwo {} + +fn main() {} diff --git a/tests/macro/empty_attr.stderr b/tests/macro/empty_attr.stderr new file mode 100644 index 0000000..abfcb9e --- /dev/null +++ b/tests/macro/empty_attr.stderr @@ -0,0 +1,19 @@ +error: Cannot parse attribute as list + --> $DIR/empty_attr.rs:6:3 + | +6 | #[endpoint] + | ^^^^^^^^ + +error: Attribute cannot be empty + --> $DIR/empty_attr.rs:10:3 + | +10 | #[endpoint()] + | ^^^^^^^^ + +warning: unused import: `rustify::endpoint::Endpoint` + --> $DIR/empty_attr.rs:1:5 + | +1 | use rustify::endpoint::Endpoint; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_imports)]` on by default diff --git a/tests/macro/invalid_data.rs b/tests/macro/invalid_data.rs new file mode 100644 index 0000000..decd417 --- /dev/null +++ b/tests/macro/invalid_data.rs @@ -0,0 +1,23 @@ +use rustify::endpoint::Endpoint; +use rustify_derive::Endpoint; +use serde::Serialize; + +#[derive(Debug, Endpoint, Serialize)] +#[endpoint(path = "test/path")] +struct Test { + pub name: String, + #[endpoint(data)] + pub data: String, +} + +#[derive(Debug, Endpoint, Serialize)] +#[endpoint(path = "test/path")] +struct TestTwo { + pub name: String, + #[endpoint(data)] + pub data: Vec, + #[endpoint(data)] + pub data_two: Vec, +} + +fn main() {} diff --git a/tests/macro/invalid_data.stderr b/tests/macro/invalid_data.stderr new file mode 100644 index 0000000..5b8e1a5 --- /dev/null +++ b/tests/macro/invalid_data.stderr @@ -0,0 +1,23 @@ +error: May only mark one field as the data field + --> $DIR/invalid_data.rs:20:9 + | +20 | pub data_two: Vec, + | ^^^^^^^^ + +error[E0308]: mismatched types + --> $DIR/invalid_data.rs:5:17 + | +5 | #[derive(Debug, Endpoint, Serialize)] + | ^^^^^^^^ expected slice `[u8]`, found struct `std::string::String` + | + = note: expected reference `&[u8]` + found reference `&std::string::String` + = note: this error originates in the derive macro `Endpoint` (in Nightly builds, run with -Z macro-backtrace for more info) + +warning: unused import: `rustify::endpoint::Endpoint` + --> $DIR/invalid_data.rs:1:5 + | +1 | use rustify::endpoint::Endpoint; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_imports)]` on by default diff --git a/tests/macro/invalid_method.rs b/tests/macro/invalid_method.rs new file mode 100644 index 0000000..9bc5eb8 --- /dev/null +++ b/tests/macro/invalid_method.rs @@ -0,0 +1,9 @@ +use rustify::endpoint::Endpoint; +use rustify_derive::Endpoint; +use serde::Serialize; + +#[derive(Debug, Endpoint, Serialize)] +#[endpoint(path = "test/path", method = "TEST")] +struct Test {} + +fn main() {} diff --git a/tests/macro/invalid_method.stderr b/tests/macro/invalid_method.stderr new file mode 100644 index 0000000..053d417 --- /dev/null +++ b/tests/macro/invalid_method.stderr @@ -0,0 +1,13 @@ +warning: unused import: `rustify::endpoint::Endpoint` + --> $DIR/invalid_method.rs:1:5 + | +1 | use rustify::endpoint::Endpoint; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_imports)]` on by default + +error[E0599]: no variant or associated item named `TEST` found for enum `RequestMethod` in the current scope + --> $DIR/invalid_method.rs:6:41 + | +6 | #[endpoint(path = "test/path", method = "TEST")] + | ^^^^^^ variant or associated item not found in `RequestMethod` diff --git a/tests/macro/invalid_result.rs b/tests/macro/invalid_result.rs new file mode 100644 index 0000000..0882a2f --- /dev/null +++ b/tests/macro/invalid_result.rs @@ -0,0 +1,9 @@ +use rustify::endpoint::Endpoint; +use rustify_derive::Endpoint; +use serde::Serialize; + +#[derive(Debug, Endpoint, Serialize)] +#[endpoint(path = "test/path", result = "DoesNotExist")] +struct Test {} + +fn main() {} diff --git a/tests/macro/invalid_result.stderr b/tests/macro/invalid_result.stderr new file mode 100644 index 0000000..d8ebfab --- /dev/null +++ b/tests/macro/invalid_result.stderr @@ -0,0 +1,13 @@ +error[E0412]: cannot find type `DoesNotExist` in this scope + --> $DIR/invalid_result.rs:6:41 + | +6 | #[endpoint(path = "test/path", result = "DoesNotExist")] + | ^^^^^^^^^^^^^^ not found in this scope + +warning: unused import: `rustify::endpoint::Endpoint` + --> $DIR/invalid_result.rs:1:5 + | +1 | use rustify::endpoint::Endpoint; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_imports)]` on by default diff --git a/tests/macro/invalid_type.rs b/tests/macro/invalid_type.rs new file mode 100644 index 0000000..0716f7c --- /dev/null +++ b/tests/macro/invalid_type.rs @@ -0,0 +1,9 @@ +use rustify::endpoint::Endpoint; +use rustify_derive::Endpoint; +use serde::Serialize; + +#[derive(Debug, Endpoint, Serialize)] +#[endpoint(path = "test/path", request_type = "BAD", response_type = "BAD")] +struct Test {} + +fn main() {} diff --git a/tests/macro/invalid_type.stderr b/tests/macro/invalid_type.stderr new file mode 100644 index 0000000..0b949c6 --- /dev/null +++ b/tests/macro/invalid_type.stderr @@ -0,0 +1,19 @@ +warning: unused import: `rustify::endpoint::Endpoint` + --> $DIR/invalid_type.rs:1:5 + | +1 | use rustify::endpoint::Endpoint; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_imports)]` on by default + +error[E0599]: no variant or associated item named `BAD` found for enum `RequestType` in the current scope + --> $DIR/invalid_type.rs:6:47 + | +6 | #[endpoint(path = "test/path", request_type = "BAD", response_type = "BAD")] + | ^^^^^ variant or associated item not found in `RequestType` + +error[E0599]: no variant or associated item named `BAD` found for enum `ResponseType` in the current scope + --> $DIR/invalid_type.rs:6:70 + | +6 | #[endpoint(path = "test/path", request_type = "BAD", response_type = "BAD")] + | ^^^^^ variant or associated item not found in `ResponseType` diff --git a/tests/macro/no_attr.rs b/tests/macro/no_attr.rs new file mode 100644 index 0000000..47e4713 --- /dev/null +++ b/tests/macro/no_attr.rs @@ -0,0 +1,8 @@ +use rustify::endpoint::Endpoint; +use rustify_derive::Endpoint; +use serde::Serialize; + +#[derive(Debug, Endpoint, Serialize)] +struct Test {} + +fn main() {} diff --git a/tests/macro/no_attr.stderr b/tests/macro/no_attr.stderr new file mode 100644 index 0000000..802ee90 --- /dev/null +++ b/tests/macro/no_attr.stderr @@ -0,0 +1,15 @@ +error: Deriving `Endpoint` requires attaching an `endpoint` attribute + --> $DIR/no_attr.rs:5:17 + | +5 | #[derive(Debug, Endpoint, Serialize)] + | ^^^^^^^^ + | + = note: this error originates in the derive macro `Endpoint` (in Nightly builds, run with -Z macro-backtrace for more info) + +warning: unused import: `rustify::endpoint::Endpoint` + --> $DIR/no_attr.rs:1:5 + | +1 | use rustify::endpoint::Endpoint; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_imports)]` on by default diff --git a/tests/macro/no_path.rs b/tests/macro/no_path.rs new file mode 100644 index 0000000..000c8f1 --- /dev/null +++ b/tests/macro/no_path.rs @@ -0,0 +1,9 @@ +use rustify::endpoint::Endpoint; +use rustify_derive::Endpoint; +use serde::Serialize; + +#[derive(Debug, Endpoint, Serialize)] +#[endpoint(method = "POST")] +struct Test {} + +fn main() {} diff --git a/tests/macro/no_path.stderr b/tests/macro/no_path.stderr new file mode 100644 index 0000000..177a706 --- /dev/null +++ b/tests/macro/no_path.stderr @@ -0,0 +1,15 @@ +error: Missing required parameter: path + --> $DIR/no_path.rs:5:17 + | +5 | #[derive(Debug, Endpoint, Serialize)] + | ^^^^^^^^ + | + = note: this error originates in the derive macro `Endpoint` (in Nightly builds, run with -Z macro-backtrace for more info) + +warning: unused import: `rustify::endpoint::Endpoint` + --> $DIR/no_path.rs:1:5 + | +1 | use rustify::endpoint::Endpoint; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_imports)]` on by default