From f172929975a955818a139d379d43393e83b0d7e6 Mon Sep 17 00:00:00 2001 From: Espen Antonsen Date: Wed, 18 Nov 2020 22:21:03 +0100 Subject: [PATCH] Added Cuprite adapter --- Gemfile | 1 + Gemfile.lock | 12 ++++- lib/show_me_the_cookies.rb | 2 + lib/show_me_the_cookies/adapters/cuprite.rb | 50 +++++++++++++++++++++ spec/drivers/cuprite_spec.rb | 27 +++++++++++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 lib/show_me_the_cookies/adapters/cuprite.rb create mode 100644 spec/drivers/cuprite_spec.rb diff --git a/Gemfile b/Gemfile index 9699122..1c470f5 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,7 @@ group :test do gem 'rspec', '~> 3.0' gem 'poltergeist' gem 'sinatra' + gem 'cuprite' gem 'selenium-webdriver' gem 'webdrivers', '~> 4.0' # chromedriver installer end diff --git a/Gemfile.lock b/Gemfile.lock index b66b56f..d1013bc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -19,7 +19,16 @@ GEM xpath (~> 3.2) childprocess (3.0.0) cliver (0.3.2) + concurrent-ruby (1.1.7) + cuprite (0.11) + capybara (>= 2.1, < 4) + ferrum (~> 0.9.0) diff-lcs (1.3) + ferrum (0.9) + addressable (~> 2.5) + cliver (~> 0.3) + concurrent-ruby (~> 1.1) + websocket-driver (>= 0.6, < 0.8) mini_mime (1.0.2) mini_portile2 (2.4.0) mustermann (1.1.1) @@ -76,6 +85,7 @@ PLATFORMS ruby DEPENDENCIES + cuprite poltergeist rake rspec (~> 3.0) @@ -85,4 +95,4 @@ DEPENDENCIES webdrivers (~> 4.0) BUNDLED WITH - 2.0.2 + 2.1.4 diff --git a/lib/show_me_the_cookies.rb b/lib/show_me_the_cookies.rb index 6964405..5b669b4 100644 --- a/lib/show_me_the_cookies.rb +++ b/lib/show_me_the_cookies.rb @@ -3,6 +3,7 @@ module ShowMeTheCookies require 'show_me_the_cookies/adapters/poltergeist' require 'show_me_the_cookies/adapters/selenium' require 'show_me_the_cookies/adapters/selenium_chrome' + require 'show_me_the_cookies/adapters/cuprite' @adapters = {} class << self @@ -23,6 +24,7 @@ def register_adapter(driver, adapter) register_adapter(:selenium_chrome_headless, ShowMeTheCookies::SeleniumChrome) register_adapter(:rack_test, ShowMeTheCookies::RackTest) register_adapter(:poltergeist, ShowMeTheCookies::Poltergeist) + register_adapter(:cuprite, ShowMeTheCookies::Cuprite) # puts a string summary of the cookie def show_me_the_cookie(cookie_name) diff --git a/lib/show_me_the_cookies/adapters/cuprite.rb b/lib/show_me_the_cookies/adapters/cuprite.rb new file mode 100644 index 0000000..60db4b0 --- /dev/null +++ b/lib/show_me_the_cookies/adapters/cuprite.rb @@ -0,0 +1,50 @@ +module ShowMeTheCookies + class Cuprite + def initialize(driver) + @browser = driver.browser + @driver = driver + end + + def get_me_the_cookie(name) + cookie = cookies_hash[name.to_s] + translate(cookie) unless cookie.nil? + end + + def get_me_the_cookies + cookies_hash.map {|name, cookie| translate(cookie) } + end + + def expire_cookies + cookies_hash.each do |name, cookie| + delete_cookie(name) if (cookie.expires rescue nil).nil? + end + end + + def delete_cookie(name) + @driver.remove_cookie(name.to_s) + end + + def create_cookie(name, value, options) + # see: https://github.com/rubycdp/cuprite#manipulating-cookies + @driver.set_cookie(name, value, options) + end + + private + + def cookies_hash + @driver.cookies + end + + def translate(cookie) + { + :name => cookie.name, + :domain => cookie.domain, + :value => cookie.value, + :expires => (cookie.expires rescue nil), + :path => cookie.path, + :secure => cookie.secure?, + :httponly => cookie.httponly? + } + end + end +end diff --git a/spec/drivers/cuprite_spec.rb b/spec/drivers/cuprite_spec.rb new file mode 100644 index 0000000..ca741b4 --- /dev/null +++ b/spec/drivers/cuprite_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' +require 'shared_examples_for_api' +require 'capybara/cuprite' + +RSpec.describe 'Cuprite', type: :feature do + before(:each) do + Capybara.current_driver = :cuprite + end + + describe 'the testing rig' do + it 'should load the sinatra app' do + visit '/' + expect(page).to have_content('Cookie setter ready') + end + end + + describe 'get_me_the_cookie' do + it 'reads httponly option' do + visit '/set_httponly/foo/bar' + expect(get_me_the_cookie('foo')).to include( + name: 'foo', value: 'bar', httponly: true + ) + end + end + + it_behaves_like 'the API' +end