-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
method_routes.rs
137 lines (118 loc) · 4.25 KB
/
method_routes.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Method route
//
// This demo that shows how to use Rocket HTTP macros for HTTP routes:
//
// * get /users = typically returns a list of users
// * get /users/id = typically returns a given user by id
// * post /users/id = typically creates/updates a given user by id
// * put /users/id = typically creates a given user by id
// * patch /users/id = typically updates a given user by id
// * delete /users/id = typically deletes a given user by id
// * head /users
// * options /users
//
// This method route demo is solely to show the macros;
// this method route demo doesn't connect to a database,
// or do any data processing of users or structures.
#[get("/users")]
pub fn get_users() -> String {
"Get".into()
}
#[get("/users/<_id>")]
pub fn get_user(_id: i32) -> String {
format!("Get id:{}", _id)
}
#[post("/users/<_id>")]
pub fn post_user(_id: i32) -> String {
format!("Post id:{}", _id)
}
#[put("/users/<_id>")]
pub fn put_user(_id: i32) -> String {
format!("Put id:{}", _id)
}
#[patch("/users/<_id>")]
pub fn patch_user(_id: i32) -> String {
format!("Patch id:{}", _id)
}
#[delete("/users/<_id>")]
pub fn delete_user(_id: i32) -> String {
format!("Delete id:{}", _id)
}
#[head("/users")]
pub fn head_users() -> String {
"Head".into()
}
#[options("/users")]
pub fn options_users() -> String {
"Options".into()
}
#[cfg(test)]
mod tests {
use crate::rocket;
use rocket::local::blocking::Client;
use rocket::http::{ContentType, Status};
#[test]
fn get_users() {
let client = Client::tracked(rocket()).expect("rocket");
let response = client.get("/users").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::Plain));
assert_eq!(response.into_string(), Some("Get".into()));
}
#[test]
fn get_user() {
let client = Client::tracked(rocket()).expect("rocket");
let response = client.get("/users/1").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::Plain));
assert_eq!(response.into_string(), Some("Get id:1".into()));
}
#[test]
fn post_user() {
let client = Client::tracked(rocket()).expect("rocket");
let response = client.post("/users/1").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::Plain));
assert_eq!(response.into_string(), Some("Post id:1".into()));
}
#[test]
fn put_user() {
let client = Client::tracked(rocket()).expect("rocket");
let response = client.put("/users/1").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::Plain));
assert_eq!(response.into_string(), Some("Put id:1".into()));
}
#[test]
fn patch_user() {
let client = Client::tracked(rocket()).expect("rocket");
let response = client.patch("/users/1").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::Plain));
assert_eq!(response.into_string(), Some("Patch id:1".into()));
}
#[test]
fn delete_user() {
let client = Client::tracked(rocket()).expect("rocket");
let response = client.delete("/users/1").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::Plain));
assert_eq!(response.into_string(), Some("Delete id:1".into()));
}
#[test]
fn head_users() {
let client = Client::tracked(rocket()).expect("rocket");
let response = client.head("/users").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::Plain));
assert_eq!(response.into_string(), Some("".into()));
}
#[test]
fn options_users() {
let client = Client::tracked(rocket()).expect("rocket");
let response = client.options("/users").dispatch();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.content_type(), Some(ContentType::Plain));
assert_eq!(response.into_string(), Some("Options".into()));
}
}