mirror of
https://github.com/NLnetLabs/krill.git
synced 2026-09-30 21:34:53 +02:00
Using reqwest instead of hyper, because synchronous requests are all we need here, both in testing and for the publication client
This commit is contained in:
+1
-1
@@ -11,13 +11,13 @@ bcder = "^0.1"
|
||||
bytes = "^0.4"
|
||||
chrono = { version = "^0.4", features = ["serde"] }
|
||||
clap = "^2.32"
|
||||
hyper = "0.12"
|
||||
futures = "0.1"
|
||||
failure = "^0.1"
|
||||
lazy_static = "^1.1"
|
||||
openssl = "^0.10"
|
||||
pretty = "0.5.2"
|
||||
rand = "^0.5"
|
||||
reqwest = "^0.9"
|
||||
rpki = { version = "^0.2", features = ["softkeys"] }
|
||||
serde = { version = "^1.0", features = ["rc"] }
|
||||
serde_derive = "^1.0"
|
||||
|
||||
+2
-12
@@ -2,9 +2,8 @@ extern crate actix;
|
||||
extern crate rpubd;
|
||||
|
||||
#[macro_use] extern crate lazy_static;
|
||||
extern crate core;
|
||||
|
||||
use std::thread;
|
||||
use actix::System;
|
||||
use rpubd::pubd::config::Config;
|
||||
use rpubd::pubd::http::PubServerApp;
|
||||
|
||||
@@ -21,14 +20,5 @@ lazy_static! {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Start the server
|
||||
thread::spawn(||{
|
||||
System::run(move || {
|
||||
PubServerApp::serve(&CONFIG)
|
||||
})
|
||||
});
|
||||
|
||||
loop {
|
||||
// wait forever
|
||||
}
|
||||
PubServerApp::run(&CONFIG)
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ extern crate serde_json;
|
||||
extern crate tokio;
|
||||
extern crate toml;
|
||||
extern crate xml;
|
||||
extern crate hyper;
|
||||
|
||||
pub mod file;
|
||||
pub mod provisioning;
|
||||
|
||||
+15
-59
@@ -69,15 +69,28 @@ impl PubServerApp {
|
||||
Arc::new(RwLock::new(pub_server))
|
||||
}
|
||||
|
||||
pub fn serve(config: &Config) {
|
||||
/// Used to start the server with an existing executor (e.g. in tests)
|
||||
pub fn start(config: &Config) {
|
||||
let ps = PubServerApp::create_server(config);
|
||||
|
||||
server::new(move || PubServerApp::new(ps.clone()))
|
||||
.bind(config.socket_addr())
|
||||
.expect(&format!("Cannot bind to: {}", config.socket_addr()))
|
||||
.shutdown_timeout(0)
|
||||
.start();
|
||||
}
|
||||
|
||||
/// Used to run the server in blocking mode, from the main method.
|
||||
pub fn run(config: &Config) {
|
||||
let ps = PubServerApp::create_server(config);
|
||||
|
||||
server::new(move || PubServerApp::new(ps.clone()))
|
||||
.bind(config.socket_addr())
|
||||
.expect(&format!("Cannot bind to: {}", config.socket_addr()))
|
||||
.shutdown_timeout(0)
|
||||
.run();
|
||||
}
|
||||
|
||||
/// 404 handler
|
||||
fn p404(_req: &HttpRequest) -> HttpResponse {
|
||||
HttpResponse::build(StatusCode::NOT_FOUND)
|
||||
@@ -179,61 +192,4 @@ pub enum Error {
|
||||
|
||||
//------------ Tests ---------------------------------------------------------
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use test;
|
||||
use pubc::client::PubClient;
|
||||
use rpki::oob::exchange::PublisherRequest;
|
||||
use std::path::PathBuf;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::thread;
|
||||
use actix::System;
|
||||
use std::time;
|
||||
|
||||
fn save_pr(base_dir: &PathBuf, file_name: &str, pr: &PublisherRequest) {
|
||||
let mut full_name = base_dir.clone();
|
||||
full_name.push(PathBuf::from
|
||||
(file_name));
|
||||
let mut f = File::create(full_name).unwrap();
|
||||
let xml = pr.encode_vec();
|
||||
f.write(xml.as_ref()).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn start() {
|
||||
test::test_with_tmp_dir(|d| {
|
||||
|
||||
// Set up a test PubServer Config with a client in it.
|
||||
let server_conf = {
|
||||
// Use a data dir for the storage
|
||||
let data_dir = test::create_sub_dir(&d);
|
||||
let xml_dir = test::create_sub_dir(&d);
|
||||
|
||||
// Set up a client
|
||||
let client_dir = test::create_sub_dir(&d);
|
||||
let mut client = PubClient::new(&client_dir).unwrap();
|
||||
client.init("client".to_string()).unwrap();
|
||||
let pr = client.publisher_request().unwrap();
|
||||
|
||||
// Add the client's PublisherRequest to the server dir.
|
||||
save_pr(&xml_dir, "client.xml", &pr);
|
||||
Config::test(&data_dir, &xml_dir)
|
||||
};
|
||||
|
||||
// Start the server
|
||||
thread::spawn(||{
|
||||
System::run(move || {
|
||||
PubServerApp::serve(&server_conf)
|
||||
})
|
||||
});
|
||||
|
||||
// Wait for server to boot..
|
||||
thread::sleep(time::Duration::from_secs(10));
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
// Tested in tests/integration_test.rs
|
||||
@@ -1,24 +1,21 @@
|
||||
extern crate actix;
|
||||
extern crate futures;
|
||||
extern crate hyper;
|
||||
extern crate reqwest;
|
||||
extern crate rpki;
|
||||
extern crate rpubd;
|
||||
extern crate serde_json;
|
||||
extern crate tokio;
|
||||
extern crate actix;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::str;
|
||||
use std::{thread, time};
|
||||
use hyper::Client;
|
||||
use rpki::oob::exchange::PublisherRequest;
|
||||
use rpubd::test;
|
||||
use rpubd::pubc::client::PubClient;
|
||||
use rpubd::pubd::config::Config;
|
||||
use rpubd::provisioning::publisher::Publisher;
|
||||
use tokio::prelude::*;
|
||||
use tokio::runtime::Runtime;
|
||||
use rpubd::pubd::http::PubServerApp;
|
||||
use actix::System;
|
||||
|
||||
@@ -55,73 +52,16 @@ fn testing() {
|
||||
// Start the server
|
||||
thread::spawn(||{
|
||||
System::run(move || {
|
||||
PubServerApp::serve(&server_conf)
|
||||
PubServerApp::start(&server_conf);
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
let mut rt = Runtime::new().unwrap();
|
||||
// XXX TODO: Find a better way to know the server is ready!
|
||||
thread::sleep(time::Duration::from_millis(150));
|
||||
|
||||
// XXX TODO: Use a helper to create the futures to check the
|
||||
// XXX TODO: responses.. the compiler insists this crosses threads
|
||||
// XXX TODO: and isn't safe..
|
||||
// fn test<F>(url: &str, test: F) -> impl Future
|
||||
// where F: Fn(&str)-> () + Send {
|
||||
// let url = url.parse().unwrap();
|
||||
// let client = Client::new();
|
||||
//
|
||||
// client
|
||||
// .get(url)
|
||||
// .and_then(|res| {
|
||||
// res.into_body().concat2()
|
||||
// })
|
||||
// .and_then(|body| {
|
||||
// let s = str::from_utf8(&body).unwrap();
|
||||
// test(s);
|
||||
// Ok(())
|
||||
// })
|
||||
// .map_err(|e| {
|
||||
// println!("{}", e);
|
||||
// })
|
||||
// };
|
||||
//
|
||||
// let fut = test(
|
||||
// "http://localhost:3000/publishers",
|
||||
// |s| {
|
||||
// let pl: Vec<Publisher> = serde_json::from_str(s).unwrap();
|
||||
// assert_eq!(2, pl.len());
|
||||
// }
|
||||
// );
|
||||
|
||||
// XXX TODO: Extract some function, or macro?!, see above..
|
||||
// XXX TODO: for now doing this the verbose way for each test.
|
||||
|
||||
let url = "http://localhost:3000/publishers".parse().unwrap();
|
||||
let client = Client::new();
|
||||
|
||||
let fut = client
|
||||
.get(url)
|
||||
.and_then(|res| {
|
||||
res.into_body().concat2()
|
||||
})
|
||||
.and_then(|body| {
|
||||
let pl: Vec<Publisher> = serde_json::from_str(
|
||||
str::from_utf8(&body).unwrap()
|
||||
).unwrap();
|
||||
assert_eq!(1, pl.len());
|
||||
Ok(())
|
||||
})
|
||||
.map_err(|e| {
|
||||
println!("{}", e);
|
||||
});
|
||||
|
||||
rt.block_on(fut).unwrap();
|
||||
|
||||
|
||||
|
||||
rt.shutdown_now();
|
||||
let mut res = reqwest::get("http://localhost:3000/publishers").unwrap();
|
||||
let pl: Vec<Publisher> = serde_json::from_str(&res.text().unwrap()).unwrap();
|
||||
assert_eq!(1, pl.len());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user