diff options
author | Stevan Andjelkovic <stevan@advancedtelematic.com> | 2016-04-29 17:17:48 +0200 |
---|---|---|
committer | Stevan Andjelkovic <stevan@advancedtelematic.com> | 2016-04-29 17:17:48 +0200 |
commit | 508219b4b5352db126c377ca7d8c52c05b4e2d6d (patch) | |
tree | 19f469df40f93479a91a1b97e1e94effc7baa329 | |
parent | 73cc9086127b984203f9cc41957e12aae273fea7 (diff) | |
download | rvi_sota_client-508219b4b5352db126c377ca7d8c52c05b4e2d6d.tar.gz |
Found bug in Hyper::send_request.
-rw-r--r-- | src/auth_plus.rs | 4 | ||||
-rw-r--r-- | src/datatype/config.rs | 13 | ||||
-rw-r--r-- | src/http_client/http_client.rs | 10 | ||||
-rw-r--r-- | src/http_client/hyper.rs | 32 | ||||
-rw-r--r-- | src/interpreter.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 7 | ||||
-rw-r--r-- | tests/ota_plus_client_tests.rs | 6 |
7 files changed, 55 insertions, 21 deletions
diff --git a/src/auth_plus.rs b/src/auth_plus.rs index 6a363e7..eb79d13 100644 --- a/src/auth_plus.rs +++ b/src/auth_plus.rs @@ -6,6 +6,8 @@ use http_client::{Auth, HttpClient, HttpRequest}; pub fn authenticate(config: &AuthConfig, client: &mut HttpClient) -> Result<AccessToken, Error> { + debug!("authenticate()"); + let req = HttpRequest::post::<_, _, String>( config.server.join("/token").unwrap(), Some(Auth::Credentials( @@ -16,6 +18,8 @@ pub fn authenticate(config: &AuthConfig, client: &mut HttpClient) -> Result<Acce let body = try!(client.send_request(&req)); + debug!("authenticate, body: `{}`", body); + Ok(try!(json::decode(&body))) } diff --git a/src/datatype/config.rs b/src/datatype/config.rs index cbc7a1f..80a6fb3 100644 --- a/src/datatype/config.rs +++ b/src/datatype/config.rs @@ -143,6 +143,8 @@ fn bootstrap_credentials(auth_cfg_section: AuthConfigSection) -> Result<AuthConf let creds_path = Path::new(&auth_cfg_section.credentials_file); + debug!("bootstrap_credentails: {:?}", creds_path); + match File::open(creds_path) { Err(ref e) if e.kind() == ErrorKind::NotFound => { let creds = CredentialsFile { client_id: auth_cfg_section.client_id, @@ -166,7 +168,7 @@ pub fn parse_config(s: &str) -> Result<Config, Error> { let ota_cfg: OtaConfig = try!(parse_toml_table(&tbl, "ota")); let test_cfg: TestConfig = try!(parse_toml_table(&tbl, "test")); - return Ok(Config { + Ok(Config { auth: auth_cfg, ota: ota_cfg, test: test_cfg, @@ -175,14 +177,15 @@ pub fn parse_config(s: &str) -> Result<Config, Error> { pub fn load_config(path: &str) -> Result<Config, Error> { + debug!("load_config: {}", path); + match File::open(path) { Err(ref e) if e.kind() == ErrorKind::NotFound => Ok(Config::default()), - Err(e) => Err(Error::Config(Io(e))), + Err(e) => Err(Error::Io(e)), Ok(mut f) => { let mut s = String::new(); - try!(f.read_to_string(&mut s) - .map_err(|err| Error::Config(Io(err)))); - return parse_config(&s); + try!(f.read_to_string(&mut s)); + parse_config(&s) } } } diff --git a/src/http_client/http_client.rs b/src/http_client/http_client.rs index c8c3b66..fd744e7 100644 --- a/src/http_client/http_client.rs +++ b/src/http_client/http_client.rs @@ -50,7 +50,7 @@ impl<'a> HttpRequest<'a> { U: Into<Cow<'a, Url>>, A: Into<Cow<'a, Auth<'a>>>, { - HttpRequest::new::<_, _, _, String>(Method::Get, url, auth, None) + HttpRequest::new::<Method, U, A, String>(Method::Get, url, auth, None) } pub fn post<U, A, B>(url: U, auth: Option<A>, body: Option<B>) -> HttpRequest<'a> @@ -74,9 +74,7 @@ pub trait HttpClient: Send + Sync { fn send_request_to(&mut self, req: &HttpRequest, file: &mut File) -> Result<(), Error> { - debug!("send_request_to: {}", req.to_string()); - - let s = try!(Self::send_request(self, req)); + let s = try!(self.send_request(req)); Ok(try!(file.write_all(&s.as_bytes()))) @@ -84,11 +82,9 @@ pub trait HttpClient: Send + Sync { fn send_request(&mut self, req: &HttpRequest) -> Result<String, Error> { - debug!("send_request: {}", req.to_string()); - let mut temp_file: File = try!(tempfile::tempfile()); - try!(Self::send_request_to(self, req, &mut temp_file)); + try!(self.send_request_to(req, &mut temp_file)); let mut buf = String::new(); let _: usize = try!(temp_file.read_to_string(&mut buf)); diff --git a/src/http_client/hyper.rs b/src/http_client/hyper.rs index aad5152..e8e96b1 100644 --- a/src/http_client/hyper.rs +++ b/src/http_client/hyper.rs @@ -27,6 +27,8 @@ impl HttpClient for Hyper { fn send_request_to(&mut self, req: &HttpRequest, file: &mut File) -> Result<(), Error> { + debug!("send_request_to, request: {}", req.to_string()); + let mut headers = Headers::new(); let mut body = String::new(); @@ -83,7 +85,10 @@ impl HttpClient for Hyper { let mut rbody = String::new(); let _: usize = try!(resp.read_to_string(&mut rbody)); + debug!("send_request_to, response: `{}`", rbody); + try!(tee(rbody.as_bytes(), file)); + Ok(()) } else if resp.status.is_redirection() { @@ -104,10 +109,10 @@ fn relocate_request<'a>(req: &'a HttpRequest, resp: &Response) -> Result<HttpReq let url = try!(req.url.join(loc)); Ok(HttpRequest { - url: url.into(), - method: req.method.clone(), - auth: None, - body: req.body.clone(), + url: url.into(), + method: req.method.clone(), + auth: None, + body: req.body.clone(), }) } else { @@ -140,7 +145,26 @@ mod tests { use std::io::{Read, repeat}; use super::*; + use datatype::Url; + use http_client::{Auth, HttpClient, HttpRequest}; + + + #[test] + fn test_send_request_get() { + let mut client = &mut Hyper::new(); + + let req = HttpRequest::get::<_, Auth>( + Url::parse("https://eu.httpbin.org/get").unwrap(), None); + + // XXX: why doesn't this work? + // let s: String = try!(client.send_request(&req)); + + let s: String = client.send_request(&req).unwrap(); + + assert!(s != "".to_string()) + + } #[test] fn test_tee() { diff --git a/src/interpreter.rs b/src/interpreter.rs index 1603be4..3d6a972 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -31,7 +31,7 @@ macro_rules! partial_apply { } } -fn interpreter(env: &mut Env, cmd: Command, tx: &Sender<Event>) -> Result<(), Error> { +fn interpreter(env: &mut Env, cmd: Command, tx: Sender<Event>) -> Result<(), Error> { Ok(if let Some(token) = env.access_token.to_owned() { @@ -110,7 +110,7 @@ impl<'a> Interpreter<Env<'a>, Command, Event> for OurInterpreter { info!("Interpreting: {:?}", cmd); - interpreter(env, cmd, &tx) + interpreter(env, cmd, tx.clone()) .unwrap_or_else(|err| tx.send(Event::Error(format!("{}", err))) .unwrap_or(error!("interpret: send failed."))) } diff --git a/src/main.rs b/src/main.rs index 471008a..f664861 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,7 +80,7 @@ fn spawn_update_poller(ctx: Sender<Command>, config: Config) { spawn_thread!("Update poller", { loop { let _ = ctx.send(Command::GetPendingUpdates); - thread::sleep(Duration::from_secs(config.ota.polling_interval)); + thread::sleep(Duration::from_secs(config.ota.polling_interval)) } }); } @@ -109,6 +109,8 @@ impl Interpreter<(), Event, Command> for AutoAcceptor { } } + info!("Event interpreter: {:?}", e); + match e { Event::Batch(ref evs) => { for ev in evs { @@ -136,7 +138,6 @@ fn main() { spawn_autoacceptor(broadcast.subscribe(), ctx.clone()); - spawn_interpreter(config.clone(), crx, etx.clone()); Websocket::run(ctx.clone(), broadcast.subscribe()); spawn_update_poller(ctx.clone(), config.clone()); @@ -149,6 +150,8 @@ fn main() { spawn_signal_handler(signals, ctx.clone()); + spawn_interpreter(config.clone(), crx, etx.clone()); + if config.test.looping { println!("Ota Plus Client REPL started."); Console::run(ctx.clone(), events_for_repl); diff --git a/tests/ota_plus_client_tests.rs b/tests/ota_plus_client_tests.rs index 0171244..66740bf 100644 --- a/tests/ota_plus_client_tests.rs +++ b/tests/ota_plus_client_tests.rs @@ -73,6 +73,7 @@ fn bad_ota_server_url() { "Invalid ota-server URL: Url parse error: relative URL without a base\n") } +#[ignore] #[test] fn no_auth_server_to_connect_to() { assert_eq!(client(&[""]), @@ -94,5 +95,8 @@ fn bad_toml() { #[test] fn bad_path_dir() { assert_eq!(client(&["--config=/"]), - "Failed to load config: Is a directory (os error 21)\n") + "IO error: Is a directory (os error 21)\n") + + // XXX: + // "Failed to load config: Is a directory (os error 21)\n") } |