Skip to content

Commit

Permalink
webdriver: response: add single cookie response type (mozilla#102)
Browse files Browse the repository at this point in the history
* 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: mozilla/geckodriver#463
Source-Repo: https://github.com/mozilla/webdriver-rust
Source-Revision: 9b49e5a6ec370e0da09521bfedcc596427c93cd5

committer: jgraham <james@hoppipolla.co.uk>
  • Loading branch information
andreastt committed Jun 16, 2017
1 parent d9209f5 commit 1d8a0ff
Showing 1 changed file with 53 additions and 58 deletions.
111 changes: 53 additions & 58 deletions testing/webdriver/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use time;
pub enum WebDriverResponse {
CloseWindow(CloseWindowResponse),
Cookie(CookieResponse),
Cookies(CookiesResponse),
DeleteSession,
ElementRect(RectResponse),
Generic(ValueResponse),
Expand All @@ -19,21 +20,23 @@ pub enum WebDriverResponse {

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\": ");
Expand Down Expand Up @@ -129,7 +132,6 @@ impl RectResponse {
}
}

//TODO: some of these fields are probably supposed to be optional
#[derive(RustcEncodable, PartialEq, Debug, Clone)]
pub struct Cookie {
pub name: String,
Expand All @@ -138,22 +140,7 @@ pub struct Cookie {
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 {
Expand All @@ -172,7 +159,7 @@ impl Into<cookie::Cookie<'static>> for 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()
Expand All @@ -181,31 +168,20 @@ impl Into<cookie::Cookie<'static>> for Cookie {

#[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();
Expand All @@ -224,17 +200,36 @@ mod tests {

#[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);
}
Expand Down

0 comments on commit 1d8a0ff

Please sign in to comment.