summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rust/src/web.rs67
1 files changed, 21 insertions, 46 deletions
diff --git a/rust/src/web.rs b/rust/src/web.rs
index 1be9ebc..70786d3 100644
--- a/rust/src/web.rs
+++ b/rust/src/web.rs
@@ -203,7 +203,7 @@ impl AppState for State {
#[post("/register")]
async fn register(
- app_state: web::Data<Arc<Mutex<dyn AppState>>>,
+ app_state: web::Data<Arc<Mutex<State>>>,
registration: web::Json<Registration>,
) -> impl Responder {
let result = app_state.lock().await.register(&registration);
@@ -265,7 +265,7 @@ struct Options {
async fn main() -> std::io::Result<()> {
let options = Options::parse();
let app_state = Arc::new(Mutex::new(State::new()));
- let http_state: Arc<Mutex<dyn AppState>> = app_state;
+
env_logger::init();
// Handlebars uses a repository for the compiled templates. This object must be
// shared between the application threads, and is therefore passed to the
@@ -285,7 +285,7 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.wrap(Logger::default())
- .app_data(web::Data::new(http_state.clone()))
+ .app_data(web::Data::new(app_state.clone()))
.app_data(web::Data::new(handlebars.clone()))
.service(register)
.service(eval)
@@ -340,43 +340,17 @@ mod app_tests {
use super::*;
- struct DummyAppState {
- id: String,
- }
-
- impl DummyAppState {
- fn new(id: String) -> Self {
- Self { id }
- }
- }
-
- impl AppState for DummyAppState {
- fn register(&mut self, registration: &Registration) -> RegistrationResult {
- if self.id.is_empty() {
- RegistrationResult::UrlAlreadyRegistered {
- url: registration.url.clone(),
- }
- } else {
- RegistrationResult::RegistrationSuccess {
- id: self.id.clone(),
- url: registration.url.clone(),
- }
- }
- }
- }
-
#[actix_web::test]
async fn post_registration_returns_success_with_unique_id() {
let id = "0123456789abcdef0123456789abcdef".to_string();
- let dummy_state: Arc<Mutex<dyn AppState>> =
- Arc::new(Mutex::new(DummyAppState::new(id.clone())));
+ let state = Arc::new(Mutex::new(State::new()));
// FIXME should only be called once, move to setup
env_logger::init();
let app = test::init_service(
App::new()
.wrap(Logger::default())
- .app_data(web::Data::new(dummy_state))
+ .app_data(web::Data::new(state))
.service(register),
)
.await;
@@ -396,32 +370,34 @@ mod app_tests {
let body = resp.into_body();
let bytes = body::to_bytes(body).await;
- assert_eq!(
- RegistrationResult::RegistrationSuccess { id, url },
- serde_json::from_slice(&bytes.unwrap()).unwrap()
- );
+ match serde_json::from_slice::<RegistrationResult>(bytes.as_ref().unwrap()).unwrap() {
+ RegistrationResult::RegistrationSuccess { id: _, url: url1 } => assert_eq!(url1, url),
+ _ => panic!("Expected RegistrationSuccess, got {:?}", bytes.unwrap()),
+ };
}
#[actix_web::test]
async fn post_registration_returns_400_when_register_fails() {
- let dummy_state: Arc<Mutex<dyn AppState>> =
- Arc::new(Mutex::new(DummyAppState::new("".to_string())));
+ let state = Arc::new(Mutex::new(State::new()));
let app = test::init_service(
App::new()
.wrap(Logger::default())
- .app_data(web::Data::new(dummy_state))
+ .app_data(web::Data::new(state.clone()))
.service(register),
)
.await;
let url = "http://192.168.1.1".to_string();
+ let registration = Registration {
+ url: url.clone(),
+ name: "foo".to_string(),
+ };
+
+ state.lock().await.register(&registration);
let req = test::TestRequest::post()
.uri("/register")
- .set_json(Registration {
- url: url.clone(),
- name: "foo".to_string(),
- })
+ .set_json(registration)
.insert_header(ContentType::json())
.to_request();
@@ -634,9 +610,8 @@ mod app_tests {
let (input, _) = client.generate_expr();
let parsed = parse(&input);
- match &parsed[..] {
- [Value::App(_, _)] => assert!(input.split(' ').count() >= 2),
- _ => (),
+ if let [Value::App(_, _)] = &parsed[..] {
+ assert!(input.split(' ').count() >= 2)
}
}
@@ -661,7 +636,7 @@ mod app_tests {
let parsed = parse(&input);
- assert!(parsed.len() > 1);
+ assert!(!parsed.is_empty());
}
#[test]