Skip to content

Commit

Permalink
Add post_cookies and http_post_cookies methods (#436).
Browse files Browse the repository at this point in the history
  • Loading branch information
AI-Mozi authored and postmodern committed Dec 16, 2023
1 parent 19f454a commit 9c9081d
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 0 deletions.
70 changes: 70 additions & 0 deletions lib/ronin/support/network/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,29 @@ def get_cookies(path, **kwargs)
end
end

#
# Sends an HTTP request and returns the parsed `Set-Cookie` header(s).
#
# @param [String] path
# The path to to make the request for.
#
# @!macro request_kwargs
#
# @return [Array<SetCookie>, nil]
# The parsed `Cookie` headers.
#
# @since 1.1.0
#
def post_cookies(path, **kwargs)
response = request(:post,path,**kwargs)

if (set_cookies = response.get_fields('Set-Cookie'))
set_cookies.map do |cookie|
SetCookie.parse(cookie)
end
end
end

#
# Sends a `GET` HTTP request and returns the response body.
#
Expand Down Expand Up @@ -2866,6 +2889,53 @@ def self.unlock(url, proxy: self.proxy,

http.unlock(path,**kwargs,&block)
end

#
# Sends an HTTP request and returns the parsed `Set-Cookie` header(s).
#
# @param [URI::HTTP, Addressable::URI, String] url
# Optional URL to create the HTTP request for.
#
# @!macro request_kwargs
# @!macro initialize_kwargs
#
# @return [Array<SetCookie>, nil]
# The parsed `Cookie` header(s).
#
# @see connect_uri
# @see #post_cookies
#
# @since 1.1.0
#
def self.post_cookies(url, proxy: self.proxy,
ssl: nil,
headers: {},
user_agent: nil,
cookie: nil,
user: nil,
password: nil,
**kwargs,
&block)
uri = case url
when Addressable::URI, URI::HTTP
url
when String
Addressable::URI.parse(url)
else
raise(ArgumentError,"URL argument must be either a Addressable::URI, URI::HTTP, or a String: #{url.inspect}")
end

path = uri.request_uri
http = connect_uri(url, proxy: proxy,
ssl: ssl,
headers: headers,
user_agent: user_agent,
cookie: cookie,
user: user,
password: password)

http.post_cookies(path,**kwargs)
end
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions lib/ronin/support/network/http/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,26 @@ def http_get_cookies(url, ssl: nil, **kwargs)
Network::HTTP.get_cookies(url, ssl: ssl, **kwargs)
end

#
# Sends an HTTP request and returns the parsed `Set-Cookie` header(s).
#
# @param [URI::HTTP, Addressable::URI, String] url
# Optional URL to create the HTTP request for.
#
# @!macro request_kwargs
# @!macro connect_kwargs
#
# @return [Array<SetCookie>, nil]
# The parsed `Cookie` header(s).
#
# @see Network::HTTP.post_cookies
#
# @api public
#
def http_post_cookies(url, ssl: nil, **kwargs)
Network::HTTP.post_cookies(url, ssl: ssl, **kwargs)
end

#
# Performs a `GET` request for the given URI and returns the response
# body.
Expand Down
62 changes: 62 additions & 0 deletions spec/network/http/mixin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,68 @@
end
end

describe "#http_post_cookies" do
it "must send a HTTP POST request for the given URI" do
stub_request(:post,uri)

subject.http_post_cookies(uri)
end

context "when the response contains a Set-Cookie header" do
let(:name) { 'foo' }
let(:value) { 'bar' }

let(:headers) do
{'Set-Cookie' => "#{name}=#{value}"}
end

it "must return an Array containing the parsed Set-Cookie header" do
stub_request(:post,uri).to_return(headers: headers)

cookies = subject.http_post_cookies(uri)

expect(cookies).to be_kind_of(Array)
expect(cookies.length).to eq(1)
expect(cookies[0]).to be_kind_of(Ronin::Support::Network::HTTP::SetCookie)
expect(cookies[0][name]).to eq(value)
end
end

context "when the response contains multiple Set-Cookie headers" do
let(:name1) { 'foo' }
let(:value1) { 'bar' }
let(:name2) { 'baz' }
let(:value2) { 'qux' }

let(:headers) do
{'Set-Cookie' => ["#{name1}=#{value1}", "#{name2}=#{value2}"]}
end

it "must return an Array containing the parsed Set-Cookie headers" do
stub_request(:post,uri).to_return(headers: headers)

cookies = subject.http_post_cookies(uri)

expect(cookies).to be_kind_of(Array)
expect(cookies.length).to eq(2)
expect(cookies[0]).to be_kind_of(Ronin::Support::Network::HTTP::SetCookie)
expect(cookies[0][name2]).to eq(value2)
expect(cookies[1]).to be_kind_of(Ronin::Support::Network::HTTP::SetCookie)
expect(cookies[1][name1]).to eq(value1)
end
end

context "when the response contains no Set-Cookie headers" do
let(:headers) { {} }

it "must return nil" do
stub_request(:post,uri).to_return(headers: headers)

expect(subject.http_post_cookies(uri)).to be(nil)
end
end
end

describe "#http_get_body" do
let(:body) { 'Test body' }

Expand Down
129 changes: 129 additions & 0 deletions spec/network/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,68 @@
end
end

describe "#post_cookies" do
it "must send a HTTP POST request for the path" do
stub_request(:post,uri)

subject.post_cookies(path)
end

context "when the response contains a Set-Cookie header" do
let(:name) { 'foo' }
let(:value) { 'bar' }

let(:headers) do
{'Set-Cookie' => "#{name}=#{value}"}
end

it "must return an Array containing the parsed Set-Cookie header" do
stub_request(:post,uri).to_return(headers: headers)

cookies = subject.post_cookies(path)

expect(cookies).to be_kind_of(Array)
expect(cookies.length).to eq(1)
expect(cookies[0]).to be_kind_of(described_class::SetCookie)
expect(cookies[0][name]).to eq(value)
end
end

context "when the response contains multiple Set-Cookie headers" do
let(:name1) { 'foo' }
let(:value1) { 'bar' }
let(:name2) { 'baz' }
let(:value2) { 'qux' }

let(:headers) do
{'Set-Cookie' => ["#{name1}=#{value1}", "#{name2}=#{value2}"]}
end

it "must return an Array containing the parsed Set-Cookie headers" do
stub_request(:post,uri).to_return(headers: headers)

cookies = subject.post_cookies(path)

expect(cookies).to be_kind_of(Array)
expect(cookies.length).to eq(2)
expect(cookies[0]).to be_kind_of(described_class::SetCookie)
expect(cookies[0][name2]).to eq(value2)
expect(cookies[1]).to be_kind_of(described_class::SetCookie)
expect(cookies[1][name1]).to eq(value1)
end
end

context "when the response contains no Set-Cookie headers" do
let(:headers) { {} }

it "must return nil" do
stub_request(:post,uri).to_return(headers: headers)

expect(subject.post_cookies(path)).to be(nil)
end
end
end

describe "#get_body" do
let(:body) { 'Test body' }

Expand Down Expand Up @@ -1580,6 +1642,73 @@
end
end

describe ".post_cookies" do
subject { described_class }

it "must send a HTTP POST request for the URI" do
stub_request(:post,uri)

subject.post_cookies(uri)
end

context "when the response contains a Set-Cookie header" do
let(:name) { 'foo' }
let(:value) { 'bar' }

let(:headers) do
{'Set-Cookie' => "#{name}=#{value}"}
end

it "must return an Array containing the parsed Set-Cookie header" do
stub_request(:post,uri).to_return(headers: headers)

cookies = subject.post_cookies(uri)

expect(cookies).to be_kind_of(Array)
expect(cookies.length).to eq(1)
expect(cookies[0]).to be_kind_of(described_class::Cookie)
expect(cookies[0][name]).to eq(value)
end
end

context "when the response contains multiple Set-Cookie headers" do
let(:name1) { 'foo' }
let(:value1) { 'bar' }
let(:name2) { 'baz' }
let(:value2) { 'qux' }

let(:headers) do
{'Set-Cookie' => ["#{name1}=#{value1}", "#{name2}=#{value2}"]}
end

it "must return an Array containing the parsed Set-Cookie headers" do
stub_request(:post,uri).to_return(headers: headers)

cookies = subject.post_cookies(uri)

expect(cookies).to be_kind_of(Array)
expect(cookies.length).to eq(2)
expect(cookies[0]).to be_kind_of(described_class::Cookie)
expect(cookies[0][name2]).to eq(value2)
expect(cookies[1]).to be_kind_of(described_class::Cookie)
expect(cookies[1][name1]).to eq(value1)
end
end

context "when the response contains no Set-Cookie headers" do
let(:name) { 'foo' }
let(:value) { 'bar' }

let(:headers) { {} }

it "must return nil" do
stub_request(:post,uri).to_return(headers: headers)

expect(subject.post_cookies(uri)).to be(nil)
end
end
end

describe ".get_body" do
subject { described_class }

Expand Down

0 comments on commit 9c9081d

Please sign in to comment.