2019-05-07 01:14:51 +02:00
|
|
|
use actions::{create_subscription, remove_subscription};
|
2019-06-30 01:25:33 +02:00
|
|
|
use actix_web::{http, web, HttpRequest, HttpResponse};
|
2019-04-25 00:37:29 +02:00
|
|
|
use askama::Template;
|
2019-05-07 01:14:51 +02:00
|
|
|
use url::form_urlencoded;
|
|
|
|
use utils::{validate_parsed_data, AppState};
|
2019-03-24 21:17:55 +01:00
|
|
|
|
2019-03-31 20:14:56 +02:00
|
|
|
#[derive(Template)]
|
|
|
|
#[template(path = "index.html")]
|
|
|
|
struct IndexView;
|
|
|
|
|
2019-06-30 01:25:33 +02:00
|
|
|
pub fn index(state: web::Data<AppState>, _req: HttpRequest) -> HttpResponse {
|
2019-04-25 00:37:29 +02:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("text/html")
|
|
|
|
.body(IndexView.render().unwrap())
|
2019-03-30 23:49:47 +01:00
|
|
|
}
|
|
|
|
|
2019-06-30 01:25:33 +02:00
|
|
|
pub fn hub(state: web::Data<AppState>, _req: HttpRequest, params: String) -> HttpResponse {
|
|
|
|
let log = &state.log;
|
2019-05-07 01:14:51 +02:00
|
|
|
info!(log, "Received Request");
|
|
|
|
debug!(log, "Content: {}", params);
|
|
|
|
let parsed_data = form_urlencoded::parse(params.as_bytes());
|
|
|
|
|
|
|
|
if !validate_parsed_data(parsed_data) {
|
|
|
|
return HttpResponse::Ok()
|
|
|
|
.status(http::StatusCode::from_u16(400).unwrap())
|
|
|
|
.finish();
|
|
|
|
}
|
|
|
|
|
2019-03-31 20:14:56 +02:00
|
|
|
HttpResponse::Ok().body("Hello World!")
|
2019-03-24 21:17:55 +01:00
|
|
|
}
|
2019-03-26 00:10:36 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-06-30 01:25:33 +02:00
|
|
|
use actix::{SyncArbiter, System};
|
|
|
|
use actix_web::{http, test, web};
|
2019-04-25 23:52:17 +02:00
|
|
|
use diesel::prelude::*;
|
|
|
|
use utils::{setup_logging, DbExecutor};
|
2019-03-26 00:10:36 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index() {
|
2019-04-25 23:52:17 +02:00
|
|
|
let _sys = System::new("rusty-hub-test");
|
|
|
|
let addr = SyncArbiter::start(1, || {
|
|
|
|
DbExecutor(SqliteConnection::establish("test.db").unwrap())
|
|
|
|
});
|
|
|
|
|
2019-06-30 01:25:33 +02:00
|
|
|
let data = web::Data::new(AppState {
|
|
|
|
log: setup_logging(),
|
|
|
|
db: addr.clone(),
|
|
|
|
});
|
|
|
|
|
|
|
|
let resp = index(data, test::TestRequest::get().to_http_request());
|
2019-03-31 20:14:56 +02:00
|
|
|
assert_eq!(resp.status(), http::StatusCode::OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hub() {
|
2019-04-25 23:52:17 +02:00
|
|
|
let _sys = System::new("rusty-hub-test");
|
|
|
|
let addr = SyncArbiter::start(1, || {
|
|
|
|
DbExecutor(SqliteConnection::establish("test.db").unwrap())
|
|
|
|
});
|
|
|
|
|
2019-06-30 01:25:33 +02:00
|
|
|
let data = web::Data::new(AppState {
|
2019-04-25 00:37:29 +02:00
|
|
|
log: setup_logging(),
|
2019-04-25 23:52:17 +02:00
|
|
|
db: addr.clone(),
|
2019-06-30 01:25:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let resp = hub(
|
|
|
|
data,
|
|
|
|
test::TestRequest::post().to_http_request(),
|
|
|
|
"key=value".to_string(),
|
|
|
|
);
|
|
|
|
assert_eq!(resp.status(), http::StatusCode::BAD_REQUEST);
|
2019-03-26 00:10:36 +01:00
|
|
|
}
|
|
|
|
}
|