webdriver: Fix CookieResponse and CloseWindowResponse serialization. (#79)
authorjgraham <james@hoppipolla.co.uk>
Tue, 07 Mar 2017 18:47:18 +0000
changeset 428123 03796a3f3336b53bc782b31a82696fc226f73028
parent 428122 b3a63663bd81129e1705b4a2b16e3e4a30157e5b
child 428124 225d1faf513b334e89ed8105873ed2954f1d6add
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
webdriver: Fix CookieResponse and CloseWindowResponse serialization. (#79) Source-Repo: https://github.com/mozilla/webdriver-rust Source-Revision: b2490b29e071d9760ba1005055b087218c41dba2 committer: GitHub <noreply@github.com>
testing/webdriver/src/response.rs
--- a/testing/webdriver/src/response.rs
+++ b/testing/webdriver/src/response.rs
@@ -1,9 +1,9 @@
-use rustc_serialize::json;
+use rustc_serialize::json::{self, Json, ToJson};
 
 use common::{Nullable, Date};
 use cookie;
 use time;
 
 #[derive(Debug)]
 pub enum WebDriverResponse {
     CloseWindow(CloseWindowResponse),
@@ -16,30 +16,31 @@ pub enum WebDriverResponse {
     Void,
     WindowPosition(WindowPositionResponse),
     WindowSize(WindowSizeResponse),
 }
 
 impl WebDriverResponse {
     pub fn to_json_string(self) -> String {
         let obj = match self {
-            WebDriverResponse::CloseWindow(ref x) => json::encode(x),
+            WebDriverResponse::CloseWindow(ref x) => json::encode(&x.to_json()),
             WebDriverResponse::Cookie(ref x) => json::encode(x),
             WebDriverResponse::DeleteSession => Ok("{}".to_string()),
             WebDriverResponse::ElementRect(ref x) => json::encode(x),
             WebDriverResponse::Generic(ref x) => json::encode(x),
             WebDriverResponse::NewSession(ref x) => json::encode(x),
             WebDriverResponse::Timeouts(ref x) => json::encode(x),
             WebDriverResponse::Void => Ok("{}".to_string()),
             WebDriverResponse::WindowPosition(ref x) => json::encode(x),
             WebDriverResponse::WindowSize(ref x) => json::encode(x),
         }.unwrap();
 
         match self {
-            WebDriverResponse::Generic(_) => obj,
+            WebDriverResponse::Generic(_) |
+            WebDriverResponse::Cookie(_) => obj,
             _ => {
                 let mut data = String::with_capacity(11 + obj.len());
                 data.push_str("{\"value\": ");
                 data.push_str(&*obj);
                 data.push_str("}");
                 data
             }
         }
@@ -52,16 +53,25 @@ pub struct CloseWindowResponse {
 }
 
 impl CloseWindowResponse {
     pub fn new(handles: Vec<String>) -> CloseWindowResponse {
         CloseWindowResponse { window_handles: handles }
     }
 }
 
+impl ToJson for CloseWindowResponse {
+    fn to_json(&self) -> Json {
+        Json::Array(self.window_handles
+                    .iter()
+                    .map(|x| Json::String(x.clone()))
+                    .collect::<Vec<Json>>())
+    }
+}
+
 #[derive(RustcEncodable, Debug)]
 pub struct NewSessionResponse {
     pub sessionId: String,
     pub value: json::Json
 }
 
 impl NewSessionResponse {
     pub fn new(session_id: String, value: json::Json) -> NewSessionResponse {
@@ -74,16 +84,26 @@ impl NewSessionResponse {
 
 #[derive(RustcEncodable, Debug)]
 pub struct TimeoutsResponse {
     pub script: u64,
     pub pageLoad: u64,
     pub implicit: u64,
 }
 
+impl TimeoutsResponse {
+    pub fn new(script: u64, page_load: u64, implicit: u64) -> TimeoutsResponse {
+        TimeoutsResponse {
+            script: script,
+            pageLoad: page_load,
+            implicit: implicit,
+        }
+    }
+}
+
 #[derive(RustcEncodable, Debug)]
 pub struct ValueResponse {
     pub value: json::Json
 }
 
 impl ValueResponse {
     pub fn new(value: json::Json) -> ValueResponse {
         ValueResponse {
@@ -166,21 +186,21 @@ impl Cookie {
 }
 
 impl Into<cookie::Cookie<'static>> for Cookie {
     fn into(self) -> cookie::Cookie<'static> {
         let cookie = cookie::Cookie::build(self.name, self.value)
             .secure(self.secure)
             .http_only(self.httpOnly);
         let cookie = match self.domain {
-            Nullable::Value(domain) => cookie.domain(domain), 
+            Nullable::Value(domain) => cookie.domain(domain),
             Nullable::Null => cookie,
         };
         let cookie = match self.path {
-            Nullable::Value(path) => cookie.path(path), 
+            Nullable::Value(path) => cookie.path(path),
             Nullable::Null => cookie,
         };
         let cookie = match self.expiry {
             Nullable::Value(Date(expiry)) => {
                 cookie.expires(time::at(time::Timespec::new(expiry as i64, 0)))
             },
             Nullable::Null => cookie,
         };
@@ -195,8 +215,109 @@ pub struct CookieResponse {
 
 impl CookieResponse {
     pub fn new(value: Vec<Cookie>) -> CookieResponse {
         CookieResponse {
             value: value
         }
     }
 }
+
+
+#[cfg(test)]
+mod tests {
+    use std::collections::BTreeMap;
+    use rustc_serialize::json::Json;
+    use super::{WebDriverResponse,
+                CloseWindowResponse,
+                CookieResponse,
+                ElementRectResponse,
+                NewSessionResponse,
+                ValueResponse,
+                TimeoutsResponse,
+                WindowPositionResponse,
+                WindowSizeResponse,
+                Cookie,
+                Nullable};
+
+    fn test(resp: WebDriverResponse, expected_str: &str) {
+        let data = resp.to_json_string();
+        let actual = Json::from_str(&*data).unwrap();
+        let expected = Json::from_str(expected_str).unwrap();
+        assert_eq!(actual, expected);
+    }
+
+    #[test]
+    fn test_close_window() {
+        let resp = WebDriverResponse::CloseWindow(
+            CloseWindowResponse::new(vec!["test".into()]));
+        let expected = r#"{"value": ["test"]}"#;
+        test(resp, expected);
+    }
+
+    #[test]
+    fn test_cookie() {
+        let resp = WebDriverResponse::Cookie(CookieResponse::new(
+            vec![
+                Cookie::new("test".into(),
+                            "test_value".into(),
+                            Nullable::Value("/".into()),
+                            Nullable::Null,
+                            Nullable::Null,
+                            true,
+                            false)
+            ]));
+        let expected = r#"{"value": [{"name": "test", "value": "test_value", "path": "/",
+"domain": null, "expiry": null, "secure": true, "httpOnly": false}]}"#;
+        test(resp, expected);
+    }
+
+    #[test]
+    fn test_element_rect() {
+        let resp = WebDriverResponse::ElementRect(ElementRectResponse::new(
+            0f64, 1f64, 2f64, 3f64));
+        let expected = r#"{"value": {"x": 0.0, "y": 1.0, "width": 2.0, "height": 3.0}}"#;
+        test(resp, expected);
+    }
+
+    #[test]
+    fn test_new_session() {
+        let resp = WebDriverResponse::NewSession(
+            NewSessionResponse::new("test".into(),
+                                    Json::Object(BTreeMap::new())));
+        let expected = r#"{"value": {"sessionId": "test", "value": {}}}"#;
+        test(resp, expected);
+    }
+
+    #[test]
+    fn test_timeouts() {
+         let resp = WebDriverResponse::Timeouts(TimeoutsResponse::new(
+            1, 2, 3));
+        let expected = r#"{"value": {"script": 1, "pageLoad": 2, "implicit": 3}}"#;
+        test(resp, expected);
+    }
+
+    #[test]
+    fn test_value() {
+        let mut value = BTreeMap::new();
+        value.insert("example".into(), Json::Array(vec![Json::String("test".into())]));
+        let resp = WebDriverResponse::Generic(ValueResponse::new(
+            Json::Object(value)));
+        let expected = r#"{"value": {"example": ["test"]}}"#;
+        test(resp, expected);
+    }
+
+    #[test]
+    fn test_window_position() {
+         let resp = WebDriverResponse::WindowPosition(WindowPositionResponse::new(
+            1, 2));
+        let expected = r#"{"value": {"x": 1, "y": 2}}"#;
+        test(resp, expected);
+    }
+
+    #[test]
+    fn test_window_size() {
+         let resp = WebDriverResponse::WindowSize(WindowSizeResponse::new(
+            1, 2));
+        let expected = r#"{"value": {"width": 1, "height": 2}}"#;
+        test(resp, expected);
+    }
+}