Skip to content

Commit

Permalink
Adds tests for raw request data and rustify_derive
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgilman committed Aug 21, 2021
1 parent c6b3034 commit 73bd2b1
Show file tree
Hide file tree
Showing 18 changed files with 283 additions and 50 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
45 changes: 44 additions & 1 deletion tests/common.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<E: Endpoint>(
&self,
_: &E,
req: &mut rustify::client::Request,
) -> Result<(), ClientError> {
req.headers
.push(("X-API-Token".to_string(), "mytoken".to_string()));
Ok(())
}
fn response<E: Endpoint>(
&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(())
}
}
85 changes: 36 additions & 49 deletions tests/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<E: Endpoint>(
&self,
_: &E,
req: &mut rustify::client::Request,
) -> Result<(), ClientError> {
req.headers
.push(("X-API-Token".to_string(), "mytoken".to_string()));
Ok(())
}
fn response<E: Endpoint>(
&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)]
Expand All @@ -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 {}
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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<u8>,
}

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 {}
Expand Down Expand Up @@ -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 {}
Expand Down
5 changes: 5 additions & 0 deletions tests/macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[test]
fn test_macro() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/macro/*.rs");
}
13 changes: 13 additions & 0 deletions tests/macro/empty_attr.rs
Original file line number Diff line number Diff line change
@@ -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() {}
19 changes: 19 additions & 0 deletions tests/macro/empty_attr.stderr
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions tests/macro/invalid_data.rs
Original file line number Diff line number Diff line change
@@ -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<u8>,
#[endpoint(data)]
pub data_two: Vec<u8>,
}

fn main() {}
23 changes: 23 additions & 0 deletions tests/macro/invalid_data.stderr
Original file line number Diff line number Diff line change
@@ -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<u8>,
| ^^^^^^^^

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
9 changes: 9 additions & 0 deletions tests/macro/invalid_method.rs
Original file line number Diff line number Diff line change
@@ -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() {}
13 changes: 13 additions & 0 deletions tests/macro/invalid_method.stderr
Original file line number Diff line number Diff line change
@@ -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`
9 changes: 9 additions & 0 deletions tests/macro/invalid_result.rs
Original file line number Diff line number Diff line change
@@ -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() {}
13 changes: 13 additions & 0 deletions tests/macro/invalid_result.stderr
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions tests/macro/invalid_type.rs
Original file line number Diff line number Diff line change
@@ -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() {}
19 changes: 19 additions & 0 deletions tests/macro/invalid_type.stderr
Original file line number Diff line number Diff line change
@@ -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`
8 changes: 8 additions & 0 deletions tests/macro/no_attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use rustify::endpoint::Endpoint;
use rustify_derive::Endpoint;
use serde::Serialize;

#[derive(Debug, Endpoint, Serialize)]
struct Test {}

fn main() {}
15 changes: 15 additions & 0 deletions tests/macro/no_attr.stderr
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 73bd2b1

Please sign in to comment.