This commit is contained in:
James Graham 2015-04-08 18:53:24 +01:00
Родитель 4f543416a2
Коммит 435bc91f12
6 изменённых файлов: 52 добавлений и 69 удалений

Просмотреть файл

@ -1,12 +1,12 @@
[package]
name = "webdriver"
version = "0.0.1"
version = "0.1.0"
authors = ["James Graham <james@hoppipolla.co.uk>"]
[dependencies]
log = "0.2.5"
regex = "0.1.18"
rustc-serialize = "0.3.4"
uuid = "0.1.11"
hyper = "0.3.0"
log = "0.3.1"
regex = "0.1.27"
rustc-serialize = "0.3.12"
uuid = "0.1.16"
hyper = "0.3.10"

Просмотреть файл

@ -1,7 +1,6 @@
use regex::Captures;
use rustc_serialize::json::{ToJson, Json};
use std::collections::BTreeMap;
use std::str::StrExt;
use common::{Date, Nullable, WebElement, FrameId, LocatorStrategy};
use error::{WebDriverResult, WebDriverError, ErrorStatus};
@ -548,11 +547,12 @@ impl Parameters for SendKeysParameters {
let str_value = try_opt!(x.as_string(),
ErrorStatus::InvalidArgument,
"Value was not a string");
if str_value.chars().collect::<Vec<char>>().len() != 1 {
let chars = str_value.chars().collect::<Vec<char>>();
if chars.len() != 1 {
return Err(WebDriverError::new(ErrorStatus::InvalidArgument,
"Value was not a string"));
}
Ok(str_value.char_at(0))
Ok(chars[0])
}).collect::<Result<Vec<_>, _>>());
Ok(SendKeysParameters {

Просмотреть файл

@ -1,8 +1,7 @@
use rustc_serialize::json::{Json, ToJson};
use rustc_serialize::{Encodable, Encoder};
use std::collections::BTreeMap;
use std::error::{Error, FromError};
use std::num::ToPrimitive;
use std::error::Error;
use error::{WebDriverResult, WebDriverError, ErrorStatus};
@ -122,10 +121,11 @@ impl FrameId {
pub fn from_json(data: &Json) -> WebDriverResult<FrameId> {
match data {
&Json::U64(x) => {
let id = try_opt!(x.to_u16(),
ErrorStatus::NoSuchFrame,
"frame id out of range");
Ok(FrameId::Short(id))
if x > u16::max_value() as u64 || x < u16::min_value() as u64 {
return Err(WebDriverError::new(ErrorStatus::NoSuchFrame,
"frame id out of range"))
};
Ok(FrameId::Short(x as u16))
},
&Json::Null => Ok(FrameId::Null),
&Json::Object(_) => Ok(FrameId::Element(

Просмотреть файл

@ -1,7 +1,8 @@
use rustc_serialize::json::{Json, ToJson, ParserError};
use rustc_serialize::json::{Json, ToJson};
use std::collections::BTreeMap;
use std::error::{Error, FromError};
use std::error::Error;
use std::fmt;
use hyper::status::StatusCode;
#[derive(PartialEq, Debug)]
pub enum ErrorStatus {
@ -82,32 +83,32 @@ impl WebDriverError {
}
}
pub fn http_status(&self) -> u32 {
pub fn http_status(&self) -> StatusCode {
match self.status {
ErrorStatus::ElementNotSelectable => 400,
ErrorStatus::ElementNotVisible => 400,
ErrorStatus::InvalidArgument => 400,
ErrorStatus::InvalidCookieDomain => 400,
ErrorStatus::InvalidElementCoordinates => 400,
ErrorStatus::InvalidElementState => 400,
ErrorStatus::InvalidSelector => 400,
ErrorStatus::InvalidSessionId => 404,
ErrorStatus::JavascriptError => 500,
ErrorStatus::MoveTargetOutOfBounds => 500,
ErrorStatus::NoSuchAlert => 400,
ErrorStatus::NoSuchElement => 404,
ErrorStatus::NoSuchFrame => 400,
ErrorStatus::NoSuchWindow => 400,
ErrorStatus::ScriptTimeout => 408,
ErrorStatus::SessionNotCreated => 500,
ErrorStatus::StaleElementReference => 400,
ErrorStatus::Timeout => 408,
ErrorStatus::UnableToSetCookie => 500,
ErrorStatus::UnexpectedAlertOpen => 500,
ErrorStatus::UnknownError => 500,
ErrorStatus::UnknownPath => 404,
ErrorStatus::UnknownMethod => 405,
ErrorStatus::UnsupportedOperation => 500,
ErrorStatus::ElementNotSelectable => StatusCode::BadRequest,
ErrorStatus::ElementNotVisible => StatusCode::BadRequest,
ErrorStatus::InvalidArgument => StatusCode::BadRequest,
ErrorStatus::InvalidCookieDomain => StatusCode::BadRequest,
ErrorStatus::InvalidElementCoordinates => StatusCode::BadRequest,
ErrorStatus::InvalidElementState => StatusCode::BadRequest,
ErrorStatus::InvalidSelector => StatusCode::BadRequest,
ErrorStatus::InvalidSessionId => StatusCode::NotFound,
ErrorStatus::JavascriptError => StatusCode::InternalServerError,
ErrorStatus::MoveTargetOutOfBounds => StatusCode::InternalServerError,
ErrorStatus::NoSuchAlert => StatusCode::BadRequest,
ErrorStatus::NoSuchElement => StatusCode::NotFound,
ErrorStatus::NoSuchFrame => StatusCode::BadRequest,
ErrorStatus::NoSuchWindow => StatusCode::BadRequest,
ErrorStatus::ScriptTimeout => StatusCode::RequestTimeout,
ErrorStatus::SessionNotCreated => StatusCode::InternalServerError,
ErrorStatus::StaleElementReference => StatusCode::BadRequest,
ErrorStatus::Timeout => StatusCode::RequestTimeout,
ErrorStatus::UnableToSetCookie => StatusCode::InternalServerError,
ErrorStatus::UnexpectedAlertOpen => StatusCode::InternalServerError,
ErrorStatus::UnknownError => StatusCode::InternalServerError,
ErrorStatus::UnknownPath => StatusCode::NotFound,
ErrorStatus::UnknownMethod => StatusCode::MethodNotAllowed,
ErrorStatus::UnsupportedOperation => StatusCode::InternalServerError,
}
}
@ -134,10 +135,3 @@ impl Error for WebDriverError {
None
}
}
impl FromError<ParserError> for WebDriverError {
fn from_error(err: ParserError) -> WebDriverError {
let msg = format!("{:?}", err);
WebDriverError::new(ErrorStatus::UnknownError, &msg[..])
}
}

Просмотреть файл

@ -1,16 +1,11 @@
#![feature(io)]
#![feature(net)]
#![feature(core)]
#![feature(collections)]
#![allow(non_snake_case)]
#[macro_use]
extern crate log;
extern crate "rustc-serialize" as rustc_serialize;
extern crate rustc_serialize;
extern crate hyper;
extern crate regex;
#[macro_use] pub mod macros;
mod httpapi;
pub mod command;

Просмотреть файл

@ -1,5 +1,3 @@
use std::net::IpAddr;
use std::num::FromPrimitive;
use std::io::{Write, Read};
use std::sync::Mutex;
use std::sync::mpsc::{channel, Receiver, Sender};
@ -9,6 +7,7 @@ use hyper::header::ContentLength;
use hyper::method::Method;
use hyper::server::{Server, Handler, Request, Response};
use hyper::uri::RequestUri::AbsolutePath;
use hyper::status::StatusCode;
use command::{WebDriverMessage, WebDriverCommand};
use error::{WebDriverResult, WebDriverError, ErrorStatus};
@ -193,7 +192,7 @@ impl Handler for HttpHandler {
}
match recv_res.recv() {
Ok(data) => match data {
Ok(response) => (200, response.to_json_string()),
Ok(response) => (StatusCode::Ok, response.to_json_string()),
Err(err) => (err.http_status(), err.to_json_string()),
},
Err(_) => panic!("Error reading response")
@ -203,16 +202,11 @@ impl Handler for HttpHandler {
(err.http_status(), err.to_json_string())
}
};
if status != 200 {
error!("Returning status code {}", status);
error!("Returning body {}", resp_body);
} else {
debug!("Returning status code {}", status);
debug!("Returning body {}", resp_body);
}
error!("Returning status {:?}", status);
error!("Returning body {}", resp_body);
{
let status_code = res.status_mut();
*status_code = FromPrimitive::from_u32(status).unwrap();
let resp_status = res.status_mut();
*resp_status = status;
}
res.headers_mut().set(ContentLength(resp_body.len() as u64));
let mut stream = res.start().unwrap();
@ -224,7 +218,7 @@ impl Handler for HttpHandler {
}
}
pub fn start<T: 'static+WebDriverHandler>(ip_address: IpAddr, port: u16, handler: T) {
pub fn start<T: 'static+WebDriverHandler>(ip_address: &str, port: u16, handler: T) {
let (msg_send, msg_recv) = channel();
let api = WebDriverHttpApi::new();
@ -235,5 +229,5 @@ pub fn start<T: 'static+WebDriverHandler>(ip_address: IpAddr, port: u16, handler
let mut dispatcher = Dispatcher::new(handler);
dispatcher.run(msg_recv)
});
server.listen(ip_address, port).unwrap();
server.listen(&(ip_address, port)).unwrap();
}