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

Reset default request headers #508

Closed
Closed
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
6 changes: 6 additions & 0 deletions lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,18 @@ def ensure_method_maintained_across_redirects(options)
end

def perform_request(http_method, path, options, &block) #:nodoc:
reset_default_headers

options = ModuleInheritableAttributes.hash_deep_dup(default_options).merge(options)
process_headers(options)
process_cookies(options)
Request.new(http_method, path, options).perform(&block)
end

def reset_default_headers
default_options.delete(:headers) if default_options[:headers] == {}
end

def process_headers(options)
if options[:headers] && headers.any?
options[:headers] = headers.merge(options[:headers])
Expand Down
19 changes: 17 additions & 2 deletions spec/httparty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@
end

describe "headers" do
def expect_headers(header = {})
def expect_headers(header=nil)
options = header ? hash_including({ headers: header }) : anything
expect(HTTParty::Request).to receive(:new) \
.with(anything, anything, hash_including({ headers: header })) \
.with(anything, anything, options) \
.and_return(double("mock response", perform: nil))
end

Expand Down Expand Up @@ -157,6 +158,20 @@ def expect_headers(header = {})
@klass.get('', headers: {baz: 'spax', foo: 'baz'})
end

it "resets the default options with no custom headers after each request" do
expect_headers
@klass.get("")
expect(@klass.default_options.has_key?(:headers)).to eq(false)

expect_headers(foo: "bar")
@klass.get("", headers: { foo: "bar" })
expect(@klass.default_options.has_key?(:headers)).to eq(true)

expect_headers
@klass.get("")
expect(@klass.default_options.has_key?(:headers)).to eq(false)
end

context "with cookies" do
it 'utilizes the class-level cookies' do
expect_headers(foo: 'bar', 'cookie' => 'type=snickerdoodle')
Expand Down