capybara-chrome_response_headers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 40557879afeee7ac4d2e7f78a51091456296d236
4
+ data.tar.gz: 13c0d5b75a95601b898a7550bc45543429caca99
5
+ SHA512:
6
+ metadata.gz: 8eb6d069e6a0041632717f1795c7e1bfe52f5601cc330851d803e4b72733b6be71feb9ea30c1f15310dd4105d6fadcd8a710ea13b0f93093ddd5b7610a2a4c5c
7
+ data.tar.gz: a514f920197fad21066289f813d43d4e7dca6296d4abedd4f1c9bb736c7dc41c395b86d0a2d63e18406386dc8322521cfb24dfc29a8929302051795da2209be7
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.5
7
+ before_install: gem install bundler -v 1.17.1
data/Changelog.md ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in capybara-chrome_response_headers.gemspec
6
+ gemspec
data/License ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2018 Tyler Rick
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
4
+ associated documentation files (the "Software"), to deal in the Software without restriction,
5
+ including without limitation the rights to use, copy, modify, merge, publish, distribute,
6
+ sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7
+ furnished to do so, subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or substantial
10
+ portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/Readme.md ADDED
@@ -0,0 +1,59 @@
1
+ # Capybara::ChromeResponseHeaders
2
+
3
+ Allows you to get HTTP status_code, response_headers, etc. when using Capybara::Selenium::Driver
4
+ just like you can when using Rack::Test.
5
+
6
+ The [Capybara docs]()https://github.com/teamcapybara/capybara) say:
7
+
8
+ > Some drivers allow access to response headers and HTTP status code, but this kind of functionality is not provided by some drivers, such as Selenium.
9
+
10
+ But with this gem, you can have it for your Chrome Selenium driver, too!
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'capybara-chrome_response_headers'
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ Inspect `status_code` or `response_headers` in your tests to get the HTTP status code or headers,
23
+ respectively, for the current URL (the web page that you last visited or were redirected to).
24
+
25
+ You can access the `response` information for other assets using `response_for_url`.
26
+
27
+ Example:
28
+
29
+ ```ruby
30
+ it do
31
+ visit '/test/test_redirect?to=/test/basic_page%3Fmy_param=1'
32
+ expect_uri '/test/basic_page?my_param=1'
33
+
34
+ # Since it automatically follows redirects, you can't check if it was a redirect using this
35
+ expect(status_code).to eq 200
36
+
37
+ expect(response_headers["Content-Type"]).to eq "text/html; charset=utf-8"
38
+ expect(response_headers.keys).to include *["Content-Type", "ETag", "Cache-Control", "Set-Cookie", "X-Meta-Request-Version", "X-Request-Id", "X-Runtime", "X-Frame-Options", "X-Content-Type-Options", "X-XSS-Protection", "Referrer-Policy", "Content-Security-Policy", "Transfer-Encoding"]
39
+
40
+ # Could do the same for asset paths, like response_for_url["#{current_host_with_port}/asset/logo.png"]
41
+ expect(response_for_url["#{current_host_with_port}/users/sign_in"].status).to eq 200
42
+ expect(response_for_url["#{current_host_with_port}/users/sign_in"].headers).to be_a Hash
43
+ end
44
+ ```
45
+
46
+ ## Why?
47
+
48
+ Because it's useful to be able to check the HTTP status code, and many people have wanted that
49
+ ability, and because the WebDriver maintainers have no intention of adding this feature:
50
+
51
+ - https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/141
52
+ - https://github.com/SeleniumHQ/selenium/issues/4976
53
+ - https://github.com/SeleniumHQ/selenium/issues/5194
54
+ - https://groups.google.com/forum/#!msg/selenium-users/mlwxk0jKYtM/gc1ZBwZIRuEJ;context-place=searchin/selenium-users/get$20HTTP$20status$20code%7Csort:date
55
+ - https://stackoverflow.com/questions/6509628/how-to-get-http-response-code-using-selenium-webdriver
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/TylerRick/capybara-chrome_response_headers.
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "capybara/chrome_response_headers"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "capybara/chrome_response_headers/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "capybara-chrome_response_headers"
7
+ spec.version = Capybara::ChromeResponseHeaders.version
8
+ spec.authors = ["Tyler Rick"]
9
+ spec.email = ["tyler@tylerrick.com"]
10
+ spec.license = "MIT"
11
+
12
+ spec.summary = %q{Allows you to get HTTP status_code, response_headers, etc. when using Capybara::Selenium::Driver just like you can when using Rack::Test}
13
+ #spec.description = %q{}
14
+ spec.homepage = "https://github.com/TylerRick/capybara-chrome_response_headers"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "#{spec.metadata["source_code_uri"]}/blob/master/Changelog.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.required_ruby_version = ">= 2.3.0"
30
+ spec.add_dependency "capybara-chrome_dev_tools", [">= 0.1"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.17"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ end
@@ -0,0 +1,48 @@
1
+ require "capybara/chrome_response_headers/version"
2
+
3
+ module Capybara::ChromeResponseHeaders
4
+ autoload :DriverExtensions, 'capybara/chrome_response_headers/driver_extensions'
5
+ autoload :DSL, 'capybara/chrome_response_headers/dsl'
6
+
7
+ class << self
8
+ attr_accessor :verbose
9
+ attr_accessor :ignore_urls
10
+ end
11
+ self.verbose = nil
12
+ # The default is to try to ignore asset files so that hopefully what's left is just HTML/API
13
+ # requests.
14
+ self.ignore_urls = /\.(js|css|png|gif|jpg)$/
15
+ end
16
+
17
+ Capybara::Selenium::Driver.class_eval do
18
+ prepend Capybara::ChromeResponseHeaders::DriverExtensions
19
+ end
20
+
21
+ Capybara::Session.class_eval do
22
+ ##
23
+ # Returns a client for the browser's dev tools protocol. Not supported by all drivers.
24
+ def status_text
25
+ driver.status_text
26
+ end
27
+
28
+ # Can't call it request_for because that conflicts with a method by that name in
29
+ # selenium/webdriver/remote/http/default.rb
30
+ def request_for_url
31
+ driver.request_for_url
32
+ end
33
+
34
+ def response_for_url
35
+ driver.response_for_url
36
+ end
37
+
38
+ # For some reason, current_host includes scheme and host but not port
39
+ def current_host_with_port
40
+ uri = URI.parse(current_url)
41
+ "#{uri.scheme}://#{uri.host}:#{uri.port}"
42
+ end
43
+
44
+ end
45
+
46
+ module Capybara::DSL
47
+ include Capybara::ChromeResponseHeaders::DSL
48
+ end
@@ -0,0 +1,71 @@
1
+ module Capybara::ChromeResponseHeaders
2
+ module DriverExtensions
3
+ attr_reader :listening_to_network_traffic
4
+ attr_reader :listener_thread
5
+
6
+ attr_reader :request_for_url
7
+ attr_reader :response_for_url
8
+
9
+ def initialize(*)
10
+ super
11
+ @request_for_url = {}
12
+ @response_for_url = {}
13
+ end
14
+
15
+ def browser
16
+ super.tap do |browser|
17
+ listen_to_network_traffic unless @listening_to_network_traffic
18
+ end
19
+ end
20
+
21
+ def listen_to_network_traffic
22
+ @listening_to_network_traffic = true
23
+ chrome = dev_tools
24
+ chrome.send_cmd "Network.enable"
25
+
26
+ chrome.on "Network.requestWillBeSent" do |arg|
27
+ request = OpenStruct.new(arg["request"])
28
+ next if Capybara::ChromeResponseHeaders.ignore_urls && request.url.match(Capybara::ChromeResponseHeaders.ignore_urls)
29
+
30
+ @request_for_url[request.url] = request
31
+ puts "Requesting #{request.url}" if Capybara::ChromeResponseHeaders.verbose
32
+ end
33
+
34
+ chrome.on "Network.responseReceived" do |arg|
35
+ response = OpenStruct.new(arg["response"])
36
+ next if Capybara::ChromeResponseHeaders.ignore_urls && response.url.match(Capybara::ChromeResponseHeaders.ignore_urls)
37
+
38
+ # TODO: Use/return a Rack::Response like Rack::Test does
39
+ @response_for_url[response.url] = response
40
+ puts %(Response for #{response.url}: #{response.status} #{response.statusText}) if Capybara::ChromeResponseHeaders.verbose
41
+ end
42
+
43
+ @listener_thread = Thread.new do
44
+ chrome.listen
45
+ @listening_to_network_traffic = false
46
+ end
47
+ end
48
+
49
+ def request
50
+ #@request_for_url[current_url] or byebug
51
+ @request_for_url[current_url]
52
+ end
53
+
54
+ def response
55
+ #@response_for_url[current_url] or byebug
56
+ @response_for_url[current_url]
57
+ end
58
+
59
+ def response_headers
60
+ response&.headers
61
+ end
62
+
63
+ def status_code
64
+ response&.status
65
+ end
66
+
67
+ def status_text
68
+ response&.statusText
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,11 @@
1
+ module Capybara::ChromeResponseHeaders::DSL
2
+ [
3
+ :request_for_url,
4
+ :response_for_url,
5
+ :current_host_with_port,
6
+ ].each do |method|
7
+ define_method method do |*args, &block|
8
+ page.send method, *args, &block
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Capybara
2
+ module ChromeResponseHeaders
3
+ def self.version
4
+ "0.1.0"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capybara-chrome_response_headers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Rick
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capybara-chrome_dev_tools
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - tyler@tylerrick.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Changelog.md
80
+ - Gemfile
81
+ - License
82
+ - Rakefile
83
+ - Readme.md
84
+ - bin/console
85
+ - bin/setup
86
+ - capybara-chrome_response_headers.gemspec
87
+ - lib/capybara/chrome_response_headers.rb
88
+ - lib/capybara/chrome_response_headers/driver_extensions.rb
89
+ - lib/capybara/chrome_response_headers/dsl.rb
90
+ - lib/capybara/chrome_response_headers/version.rb
91
+ homepage: https://github.com/TylerRick/capybara-chrome_response_headers
92
+ licenses:
93
+ - MIT
94
+ metadata:
95
+ homepage_uri: https://github.com/TylerRick/capybara-chrome_response_headers
96
+ source_code_uri: https://github.com/TylerRick/capybara-chrome_response_headers
97
+ changelog_uri: https://github.com/TylerRick/capybara-chrome_response_headers/blob/master/Changelog.md
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 2.3.0
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.6.14.3
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Allows you to get HTTP status_code, response_headers, etc. when using Capybara::Selenium::Driver
118
+ just like you can when using Rack::Test
119
+ test_files: []