Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cookie kwarg to Ronin::Web::Browser::Agent initializer #20

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/ronin/web/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ module Browser
# @option kwargs [Boolean] headless (true)
# Controls whether the browser will start in headless or visible mode.
#
# @option kwargs [Hash{Symbol => Object}, Ferrum::Cookies::Cookie, nil] cookie
# Provides cookie to set for browser after initialization.
#
# @option kwargs [String, nil] url
# Provides url for browser to navigate to after initialization.
#
Expand Down
6 changes: 6 additions & 0 deletions lib/ronin/web/browser/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ class Agent < Ferrum::Browser
# @param [String, nil] url
# Provides url for browser to navigate to after initialization.
#
# @param [Hash{Symbol => Object}, Ferrum::Cookies::Cookie, nil] cookie
# Provides cookie to set for browser after initialization.
#
# @param [Hash{Symbol => Object}] kwargs
# Additional keyword arguments for `Ferrum::Browser#initialize`.
#
def initialize(visible: false,
headless: !visible,
proxy: Ronin::Support::Network::HTTP.proxy,
cookie: nil,
url: nil,
**kwargs)
proxy = case proxy
Expand Down Expand Up @@ -87,6 +91,8 @@ def initialize(visible: false,
@proxy = proxy

super(headless: headless, proxy: proxy, **kwargs)

cookies.set(cookie) if cookie
go_to(url) if url
end

Expand Down
31 changes: 31 additions & 0 deletions spec/agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,37 @@
after { subject.quit }
end

context "when given the cookie: keyword argument" do
subject { described_class.new(cookie: cookie) }

let(:name) { '_foo_sess' }
let(:value) { 'eyJmb28iOiJiYXIifQ:1pQcTx:UufiSnuPIjNs7zOAJS0UpqnyvRt7KET7BVes0I8LYbA' }
let(:domain) { 'foo-app.com' }
let(:cookie_hash) { { "name" => name, "value" => value, "domain" => domain, "session" => true } }

context 'when the cookie is a hash' do
let(:cookie) { cookie_hash }

it 'sets the cookie with the correct attributes' do
result = subject.cookies[name]
expect(result).to be_a Ferrum::Cookies::Cookie
expect(result.attributes > cookie).to be true
end
end

context 'when the cookie is a Cookie instance' do
let(:cookie) { Ferrum::Cookies::Cookie.new(**cookie_hash) }

it 'sets the cookie with the correct attributes' do
result = subject.cookies[name]
expect(result).to be_a Ferrum::Cookies::Cookie
expect(result.attributes > cookie_hash).to be true
end
end

after { subject.quit }
end

context "when given the proxy: keyword argument" do
let(:proxy_host) { 'example.com' }
let(:proxy_port) { 8080 }
Expand Down