2018-03-28 23:24:32 +02:00
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_web;
|
2018-04-14 01:02:01 +02:00
|
|
|
extern crate bytes;
|
2018-03-28 23:24:32 +02:00
|
|
|
extern crate futures;
|
|
|
|
extern crate h2;
|
|
|
|
extern crate http;
|
2018-04-14 01:02:01 +02:00
|
|
|
extern crate tokio_core;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
2018-03-28 23:24:32 +02:00
|
|
|
|
|
|
|
use actix_web::*;
|
|
|
|
use bytes::Bytes;
|
|
|
|
use http::StatusCode;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct PParam {
|
|
|
|
username: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_path_extractor() {
|
|
|
|
let mut srv = test::TestServer::new(|app| {
|
2018-04-14 01:02:01 +02:00
|
|
|
app.resource("/{username}/index.html", |r| {
|
|
|
|
r.with(|p: Path<PParam>| format!("Welcome {}!", p.username))
|
|
|
|
});
|
|
|
|
});
|
2018-03-28 23:24:32 +02:00
|
|
|
|
|
|
|
// client request
|
2018-04-29 07:55:47 +02:00
|
|
|
let request = srv.get().uri(srv.url("/test/index.html")).finish().unwrap();
|
2018-03-28 23:24:32 +02:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"Welcome test!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_query_extractor() {
|
|
|
|
let mut srv = test::TestServer::new(|app| {
|
2018-04-14 01:02:01 +02:00
|
|
|
app.resource("/index.html", |r| {
|
|
|
|
r.with(|p: Query<PParam>| format!("Welcome {}!", p.username))
|
|
|
|
});
|
|
|
|
});
|
2018-03-28 23:24:32 +02:00
|
|
|
|
|
|
|
// client request
|
2018-04-29 07:55:47 +02:00
|
|
|
let request = srv.get().uri(srv.url("/index.html?username=test")).finish().unwrap();
|
2018-03-28 23:24:32 +02:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"Welcome test!"));
|
|
|
|
|
|
|
|
// client request
|
2018-04-29 07:55:47 +02:00
|
|
|
let request = srv.get().uri(srv.url("/index.html")).finish().unwrap();
|
2018-03-28 23:24:32 +02:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
|
|
|
}
|
2018-03-29 01:10:58 +02:00
|
|
|
|
2018-03-29 06:33:40 +02:00
|
|
|
#[test]
|
|
|
|
fn test_path_and_query_extractor() {
|
|
|
|
let mut srv = test::TestServer::new(|app| {
|
2018-04-14 01:02:01 +02:00
|
|
|
app.resource("/{username}/index.html", |r| {
|
|
|
|
r.route().with2(|p: Path<PParam>, q: Query<PParam>| {
|
|
|
|
format!("Welcome {} - {}!", p.username, q.username)
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
2018-03-29 06:33:40 +02:00
|
|
|
|
|
|
|
// client request
|
2018-04-29 07:55:47 +02:00
|
|
|
let request =
|
|
|
|
srv.get().uri(srv.url("/test1/index.html?username=test2")).finish().unwrap();
|
2018-03-29 06:33:40 +02:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"Welcome test1 - test2!"));
|
|
|
|
|
|
|
|
// client request
|
2018-04-29 07:55:47 +02:00
|
|
|
let request = srv.get().uri(srv.url("/test1/index.html")).finish().unwrap();
|
2018-03-29 06:33:40 +02:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_path_and_query_extractor2() {
|
|
|
|
let mut srv = test::TestServer::new(|app| {
|
2018-04-14 01:02:01 +02:00
|
|
|
app.resource("/{username}/index.html", |r| {
|
2018-04-29 07:55:47 +02:00
|
|
|
r.route().with3(|_: HttpRequest, p: Path<PParam>, q: Query<PParam>| {
|
|
|
|
format!("Welcome {} - {}!", p.username, q.username)
|
|
|
|
})
|
2018-04-14 01:02:01 +02:00
|
|
|
});
|
|
|
|
});
|
2018-03-29 06:33:40 +02:00
|
|
|
|
|
|
|
// client request
|
2018-04-29 07:55:47 +02:00
|
|
|
let request =
|
|
|
|
srv.get().uri(srv.url("/test1/index.html?username=test2")).finish().unwrap();
|
2018-03-29 06:33:40 +02:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"Welcome test1 - test2!"));
|
|
|
|
|
|
|
|
// client request
|
2018-04-29 07:55:47 +02:00
|
|
|
let request = srv.get().uri(srv.url("/test1/index.html")).finish().unwrap();
|
2018-03-29 06:33:40 +02:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
|
|
|
|
}
|
|
|
|
|
2018-03-29 01:10:58 +02:00
|
|
|
#[test]
|
|
|
|
fn test_non_ascii_route() {
|
|
|
|
let mut srv = test::TestServer::new(|app| {
|
|
|
|
app.resource("/中文/index.html", |r| r.f(|_| "success"));
|
|
|
|
});
|
|
|
|
|
|
|
|
// client request
|
2018-04-29 07:55:47 +02:00
|
|
|
let request = srv.get().uri(srv.url("/中文/index.html")).finish().unwrap();
|
2018-03-29 01:10:58 +02:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
|
|
|
assert_eq!(bytes, Bytes::from_static(b"success"));
|
|
|
|
}
|
2018-04-17 21:55:13 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unsafe_path_route() {
|
|
|
|
let mut srv = test::TestServer::new(|app| {
|
|
|
|
app.resource("/test/{url}", |r| {
|
|
|
|
r.f(|r| format!("success: {}", &r.match_info()["url"]))
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// client request
|
2018-04-29 07:55:47 +02:00
|
|
|
let request =
|
|
|
|
srv.get().uri(srv.url("/test/http%3A%2F%2Fexample.com")).finish().unwrap();
|
2018-04-17 21:55:13 +02:00
|
|
|
let response = srv.execute(request.send()).unwrap();
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
|
|
|
|
// read response
|
|
|
|
let bytes = srv.execute(response.body()).unwrap();
|
2018-04-29 07:55:47 +02:00
|
|
|
assert_eq!(bytes, Bytes::from_static(b"success: http:%2F%2Fexample.com"));
|
2018-04-17 21:55:13 +02:00
|
|
|
}
|