Skip to content
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
8 changes: 4 additions & 4 deletions lib/twingly/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def initialize(logger:, base_user_agent:)
@max_url_size_bytes = DEFAULT_MAX_URL_SIZE_BYTES
end

def get(url, params: {})
http_response_for(:get, url: url, params: params)
def get(url, params: {}, headers: {})
http_response_for(:get, url: url, params: params, headers: headers)
end

def post(url, body:, headers: {})
Expand Down Expand Up @@ -87,11 +87,11 @@ def http_response_for(method, *args)
end
# rubocop:enable all

def http_get_response(url:, params: {})
def http_get_response(url:, params:, headers:)
binary_url = url.dup.force_encoding(Encoding::BINARY)
http_client = create_http_client

headers = default_headers
headers = default_headers.merge(headers)

http_client.get do |request|
request.url(binary_url)
Expand Down
25 changes: 25 additions & 0 deletions spec/lib/twingly/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,31 @@ class CustomError < StandardError; end
end
end

describe "headers" do
let(:headers) do
{
"Content-Type" => "application/json",
}
end

let(:request_response) do
client.get(url, headers: headers)
end

before do
headers_in_body_lamba = lambda do |request|
{ body: request.headers.to_json }
end

stub_request(:get, url)
.to_return(&headers_in_body_lamba)
end

it "does request with specified headers" do
expect(JSON.parse(response.fetch(:body))).to include(headers)
end
end

describe "params" do
let(:params) do
{
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# from https://github.com/rspec/rspec-core/issues/1983#issuecomment-108748690
$LOAD_PATH.delete_if { |p| File.expand_path(p) == File.expand_path("./lib") }

require "json"
require "rspec"
require "toxiproxy"
require "vcr"
Expand Down