webdriver: response: add single cookie response type (#102)
authorAndreas Tolfsen <ato@sny.no>
Fri, 16 Jun 2017 17:10:59 +0100
changeset 428154 ac919ddfbcc3db7ca10c0d5219caea795527e79e
parent 428153 4370e7c6af136ae51159bd1716b25c043a9241f5
child 428155 d5dbdb83b0773938c7c20b737ac6e1e68786bda2
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: response: add single cookie response type (#102) * response: add single cookie response type The WebDriver specification mandates that the GetNamedCookie command should return a cookie serialisation object, and not an array of cookies. This patch introduces WebDriverResponse::Cookie and CookieResponse types to allow WebDriver implementations to return single cookie objects. Fixes: https://github.com/mozilla/geckodriver/issues/463 Source-Repo: https://github.com/mozilla/webdriver-rust Source-Revision: 9b49e5a6ec370e0da09521bfedcc596427c93cd5 committer: jgraham <james@hoppipolla.co.uk>
testing/webdriver/src/response.rs
--- a/testing/webdriver/src/response.rs
+++ b/testing/webdriver/src/response.rs
@@ -3,42 +3,45 @@ use rustc_serialize::json::{self, Json, 
 use common::{Nullable, Date};
 use cookie;
 use time;
 
 #[derive(Debug)]
 pub enum WebDriverResponse {
     CloseWindow(CloseWindowResponse),
     Cookie(CookieResponse),
+    Cookies(CookiesResponse),
     DeleteSession,
     ElementRect(RectResponse),
     Generic(ValueResponse),
     NewSession(NewSessionResponse),
     Timeouts(TimeoutsResponse),
     Void,
     WindowRect(RectResponse),
 }
 
 impl WebDriverResponse {
     pub fn to_json_string(self) -> String {
+        use response::WebDriverResponse::*;
+
         let obj = match self {
-            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::WindowRect(ref x) => json::encode(x),
+            CloseWindow(ref x) => json::encode(&x.to_json()),
+            Cookie(ref x) => json::encode(x),
+            Cookies(ref x) => json::encode(x),
+            DeleteSession => Ok("{}".to_string()),
+            ElementRect(ref x) => json::encode(x),
+            Generic(ref x) => json::encode(x),
+            NewSession(ref x) => json::encode(x),
+            Timeouts(ref x) => json::encode(x),
+            Void => Ok("{}".to_string()),
+            WindowRect(ref x) => json::encode(x),
         }.unwrap();
 
         match self {
-            WebDriverResponse::Generic(_) |
-            WebDriverResponse::Cookie(_) => obj,
+            Generic(_) | Cookie(_) | Cookies(_) => obj,
             _ => {
                 let mut data = String::with_capacity(11 + obj.len());
                 data.push_str("{\"value\": ");
                 data.push_str(&*obj);
                 data.push_str("}");
                 data
             }
         }
@@ -124,41 +127,25 @@ impl RectResponse {
             x: x,
             y: y,
             width: width,
             height: height
         }
     }
 }
 
-//TODO: some of these fields are probably supposed to be optional
 #[derive(RustcEncodable, PartialEq, Debug, Clone)]
 pub struct Cookie {
     pub name: String,
     pub value: String,
     pub path: Nullable<String>,
     pub domain: Nullable<String>,
     pub expiry: Nullable<Date>,
     pub secure: bool,
-    pub httpOnly: bool
-}
-
-impl Cookie {
-    pub fn new(name: String, value: String, path: Nullable<String>, domain: Nullable<String>,
-               expiry: Nullable<Date>, secure: bool, http_only: bool) -> Cookie {
-        Cookie {
-            name: name,
-            value: value,
-            path: path,
-            domain: domain,
-            expiry: expiry,
-            secure: secure,
-            httpOnly: http_only
-        }
-    }
+    pub httpOnly: bool,
 }
 
 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 {
@@ -167,50 +154,39 @@ impl Into<cookie::Cookie<'static>> for C
         };
         let cookie = match self.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,
         };
         cookie.finish()
     }
 }
 
 #[derive(RustcEncodable, Debug)]
 pub struct CookieResponse {
-    pub value: Vec<Cookie>
+    pub value: Cookie,
 }
 
-impl CookieResponse {
-    pub fn new(value: Vec<Cookie>) -> CookieResponse {
-        CookieResponse {
-            value: value
-        }
-    }
+#[derive(RustcEncodable, Debug)]
+pub struct CookiesResponse {
+    pub value: Vec<Cookie>,
 }
 
-
 #[cfg(test)]
 mod tests {
-    use std::collections::BTreeMap;
+    use super::{CloseWindowResponse, Cookie, CookieResponse, CookiesResponse, NewSessionResponse,
+                Nullable, RectResponse, TimeoutsResponse, ValueResponse, WebDriverResponse};
     use rustc_serialize::json::Json;
-    use super::{WebDriverResponse,
-                CloseWindowResponse,
-                CookieResponse,
-                RectResponse,
-                NewSessionResponse,
-                ValueResponse,
-                TimeoutsResponse,
-                Cookie,
-                Nullable};
+    use std::collections::BTreeMap;
 
     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);
     }
 
@@ -219,27 +195,46 @@ mod tests {
         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": "/",
+        let cookie = Cookie {
+            name: "name".into(),
+            value: "value".into(),
+            path: Nullable::Value("/".into()),
+            domain: Nullable::Null,
+            expiry: Nullable::Null,
+            secure: true,
+            httpOnly: false,
+        };
+        let resp = WebDriverResponse::Cookie(CookieResponse { value: cookie });
+        let expected = r#"{"value": {"name": "name", "expiry": null, "value": "value",
+"path": "/", "domain": null, "secure": true, "httpOnly": false}}"#;
+        test(resp, expected);
+    }
+
+    #[test]
+    fn test_cookies() {
+        let resp = WebDriverResponse::Cookies(CookiesResponse {
+            value: vec![
+                Cookie {
+                    name: "name".into(),
+                    value: "value".into(),
+                    path: Nullable::Value("/".into()),
+                    domain: Nullable::Null,
+                    expiry: Nullable::Null,
+                    secure: true,
+                    httpOnly: false,
+                }
+            ]});
+        let expected = r#"{"value": [{"name": "name", "value": "value", "path": "/",
 "domain": null, "expiry": null, "secure": true, "httpOnly": false}]}"#;
         test(resp, expected);
     }
 
     #[test]
     fn test_element_rect() {
         let resp = WebDriverResponse::ElementRect(RectResponse::new(
             0f64, 1f64, 2f64, 3f64));