Created
November 7, 2014 22:55
-
-
Save tourdedave/a0f691abb2da43ce24cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'selenium-webdriver' | |
require 'rspec/expectations' | |
require 'rest-client' | |
include RSpec::Matchers | |
def setup | |
@driver = Selenium::WebDriver.for :firefox | |
end | |
def teardown | |
@driver.quit | |
end | |
def run | |
setup | |
yield | |
teardown | |
end | |
# Option 1: Single file | |
run do | |
@driver.get 'http://admin:admin@localhost:9292/download_secure' | |
cookie = @driver.manage.cookie_named 'rack.session' | |
link = @driver.find_element(css: '.example a').attribute('href') | |
response = RestClient.head link, cookie: "#{cookie[:name]}=#{cookie[:value]};" | |
expect(response.headers[:content_type]).to eql('image/jpeg') | |
expect(response.headers[:content_length].to_i).to be > 0 | |
end | |
# Option 2: All files | |
def content_type(file) | |
file = File.basename(file) | |
if file.include? '.jpg' | |
'image/jpeg' | |
elsif file.include? '.pdf' | |
'application/pdf' | |
else | |
raise 'Unknown file type' | |
end | |
end | |
run do | |
@driver.get 'http://admin:admin@localhost:9292/download_secure' | |
cookie = @driver.manage.cookie_named 'rack.session' | |
links = @driver.find_elements(css: '.example a') | |
links.map! { |link| link.attribute('href') } | |
links.each do |link| | |
response = RestClient.head link, cookie: "#{cookie[:name]}=#{cookie[:value]};" | |
expect(response.headers[:content_type]).to eql(content_type(link)) | |
expect(response.headers[:content_length].to_i).to be > 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment