mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-06-05 21:30:42 +00:00
merge workspace and main crate
This commit is contained in:
parent
52679ad29d
commit
5587ded60d
45 changed files with 58 additions and 67 deletions
49
tests/custom_request_body.rs
Normal file
49
tests/custom_request_body.rs
Normal file
|
@ -0,0 +1,49 @@
|
|||
mod custom_request_body
|
||||
{
|
||||
|
||||
|
||||
use gotham::{
|
||||
hyper::header::CONTENT_TYPE,
|
||||
router::builder::*,
|
||||
test::TestServer
|
||||
};
|
||||
use gotham_restful::*;
|
||||
use mime::TEXT_PLAIN;
|
||||
|
||||
const RESPONSE : &[u8] = b"This is the only valid response.";
|
||||
|
||||
#[derive(Resource)]
|
||||
#[resource(create)]
|
||||
struct FooResource;
|
||||
|
||||
#[derive(FromBody, RequestBody)]
|
||||
#[supported_types(TEXT_PLAIN)]
|
||||
struct Foo {
|
||||
content: Vec<u8>,
|
||||
content_type: Mime
|
||||
}
|
||||
|
||||
#[create(FooResource)]
|
||||
fn create(body : Foo) -> Raw<Vec<u8>> {
|
||||
Raw::new(body.content, body.content_type)
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test()
|
||||
{
|
||||
let server = TestServer::new(build_simple_router(|router| {
|
||||
router.resource::<FooResource>("foo");
|
||||
})).unwrap();
|
||||
|
||||
let res = server.client()
|
||||
.post("http://localhost/foo", RESPONSE, TEXT_PLAIN)
|
||||
.perform().unwrap();
|
||||
assert_eq!(res.headers().get(CONTENT_TYPE).unwrap().to_str().unwrap(), "text/plain");
|
||||
let res = res.read_body().unwrap();
|
||||
let body : &[u8] = res.as_ref();
|
||||
assert_eq!(body, RESPONSE);
|
||||
}
|
||||
|
||||
|
||||
}
|
61
tests/openapi_supports_scope.rs
Normal file
61
tests/openapi_supports_scope.rs
Normal file
|
@ -0,0 +1,61 @@
|
|||
#[cfg(feature = "openapi")]
|
||||
mod openapi_supports_scope
|
||||
{
|
||||
|
||||
|
||||
use gotham::{
|
||||
router::builder::*,
|
||||
test::TestServer
|
||||
};
|
||||
use gotham_restful::*;
|
||||
use mime::TEXT_PLAIN;
|
||||
|
||||
const RESPONSE : &[u8] = b"This is the only valid response.";
|
||||
|
||||
#[derive(Resource)]
|
||||
#[resource(read_all)]
|
||||
struct FooResource;
|
||||
|
||||
#[read_all(FooResource)]
|
||||
fn read_all() -> Raw<&'static [u8]>
|
||||
{
|
||||
Raw::new(RESPONSE, TEXT_PLAIN)
|
||||
}
|
||||
|
||||
|
||||
fn test_response(server : &TestServer, path : &str)
|
||||
{
|
||||
let res = server.client().get(path).perform().unwrap().read_body().unwrap();
|
||||
let body : &[u8] = res.as_ref();
|
||||
assert_eq!(body, RESPONSE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test()
|
||||
{
|
||||
let info = OpenapiInfo {
|
||||
title: "Test".to_owned(),
|
||||
version: "1.2.3".to_owned(),
|
||||
urls: Vec::new()
|
||||
};
|
||||
let server = TestServer::new(build_simple_router(|router| {
|
||||
router.with_openapi(info, |mut router| {
|
||||
router.resource::<FooResource>("foo1");
|
||||
router.scope("/bar", |router| {
|
||||
router.resource::<FooResource>("foo2");
|
||||
router.scope("/baz", |router| {
|
||||
router.resource::<FooResource>("foo3");
|
||||
})
|
||||
});
|
||||
router.resource::<FooResource>("foo4");
|
||||
});
|
||||
})).unwrap();
|
||||
|
||||
test_response(&server, "http://localhost/foo1");
|
||||
test_response(&server, "http://localhost/bar/foo2");
|
||||
test_response(&server, "http://localhost/bar/baz/foo3");
|
||||
test_response(&server, "http://localhost/foo4");
|
||||
}
|
||||
|
||||
|
||||
} // mod test
|
11
tests/trybuild_ui.rs
Normal file
11
tests/trybuild_ui.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
use trybuild::TestCases;
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn trybuild_ui()
|
||||
{
|
||||
let t = TestCases::new();
|
||||
|
||||
// always enabled
|
||||
t.compile_fail("tests/ui/from_body_enum.rs");
|
||||
}
|
12
tests/ui/from_body_enum.rs
Normal file
12
tests/ui/from_body_enum.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
#[macro_use] extern crate gotham_restful;
|
||||
|
||||
#[derive(FromBody)]
|
||||
enum FromBodyEnum
|
||||
{
|
||||
SomeVariant(Vec<u8>),
|
||||
OtherVariant(String)
|
||||
}
|
||||
|
||||
fn main()
|
||||
{
|
||||
}
|
5
tests/ui/from_body_enum.stderr
Normal file
5
tests/ui/from_body_enum.stderr
Normal file
|
@ -0,0 +1,5 @@
|
|||
error: #[derive(FromBody)] only works for structs
|
||||
--> $DIR/from_body_enum.rs:4:1
|
||||
|
|
||||
4 | enum FromBodyEnum
|
||||
| ^^^^
|
Loading…
Add table
Add a link
Reference in a new issue