Skip to content

Commit c22ec89

Browse files
committed
[CLIENT] Added correct Accept headers to the Faraday and Curb adapters
Also fixed failing tests on master, where a request header `Content-Type` has been ignored when requesting a YAML formatted response.
1 parent fb9bdd7 commit c22ec89

5 files changed

Lines changed: 34 additions & 9 deletions

File tree

elasticsearch-transport/lib/elasticsearch/transport/transport/http/curb.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ def perform_request(method, path, params={}, body=nil)
2626
else raise ArgumentError, "Unsupported HTTP method: #{method}"
2727
end
2828

29+
request_headers = connection.connection.headers
30+
connection.connection.headers = request_headers.merge('Accept' => 'application/json') unless request_headers['Accept']
31+
2932
connection.connection.http(method.to_sym)
3033

31-
headers = {}
32-
headers['content-type'] = 'application/json' if connection.connection.header_str =~ /\/json/
34+
response_headers = {}
35+
response_headers['content-type'] = 'application/json' if connection.connection.header_str =~ /\/json/
3336

3437
Response.new connection.connection.response_code,
3538
connection.connection.body_str,
36-
headers
39+
response_headers
3740
end
3841
end
3942

elasticsearch-transport/lib/elasticsearch/transport/transport/http/faraday.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ class Faraday
1818
#
1919
def perform_request(method, path, params={}, body=nil)
2020
super do |connection, url|
21+
headers = connection.connection.headers
22+
headers['Content-Type'] ||= 'application/json'
23+
headers['Accept'] ||= 'application/json'
24+
2125
response = connection.connection.run_request \
2226
method.downcase.to_sym,
2327
url,
2428
( body ? __convert_to_json(body) : nil ),
25-
{ 'Content-Type' => 'application/json' }
29+
headers
30+
2631
Response.new response.status, response.body, response.headers
2732
end
2833
end

elasticsearch-transport/test/integration/client_test.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,26 @@ class Elasticsearch::Transport::ClientIntegrationTest < Elasticsearch::Test::Int
5757
@client = Elasticsearch::Client.new \
5858
host: "localhost:#{@port}",
5959
logger: (ENV['QUIET'] ? nil : @logger),
60-
transport_options: { headers: { content_type: 'application/yaml' } }
60+
transport_options: { headers: { accept: 'application/yaml', content_type: 'application/yaml' } }
6161

6262
response = @client.perform_request 'GET', '_cluster/health'
63-
assert_match /---\ncluster_name:/, response.body.to_s
63+
64+
assert response.body.to_s.start_with?("---\n"), "Response body should be YAML: #{response.body.inspect}"
65+
assert_equal 'application/yaml', response.headers['content-type']
6466
end
6567

6668
should "pass options to the Faraday::Connection with a block" do
6769
@client = Elasticsearch::Client.new(
6870
host: "localhost:#{@port}",
6971
logger: (ENV['QUIET'] ? nil : @logger)
7072
) do |client|
71-
client.headers['Content-Type'] = 'application/yaml'
73+
client.headers['Content-Type'] = 'application/yaml' # For ES 2.x
74+
client.headers['Accept'] = 'application/yaml' # For ES 5.x
7275
end
7376

7477
response = @client.perform_request 'GET', '_cluster/health'
7578

76-
assert response.body.start_with?("---\n"), "Response body should be YAML: #{response.body.inspect}"
79+
assert response.body.to_s.start_with?("---\n"), "Response body should be YAML: #{response.body.inspect}"
7780
assert_equal 'application/yaml', response.headers['content-type']
7881
end
7982

elasticsearch-transport/test/unit/transport_curb_test.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Elasticsearch::Transport::Transport::HTTP::FaradayTest < Test::Unit::TestC
5555
@transport.perform_request 'POST', '/', {}, '{"foo":"bar"}'
5656
end
5757

58-
should "set application/json header" do
58+
should "set application/json response header" do
5959
@transport.connections.first.connection.expects(:http).with(:GET).returns(stub_everything)
6060
@transport.connections.first.connection.expects(:body_str).returns('{"foo":"bar"}')
6161
@transport.connections.first.connection.expects(:header_str).returns('HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=UTF-8\r\nContent-Length: 311\r\n\r\n')
@@ -65,6 +65,19 @@ class Elasticsearch::Transport::Transport::HTTP::FaradayTest < Test::Unit::TestC
6565
assert_equal 'application/json', response.headers['content-type']
6666
end
6767

68+
should "set application/json request header" do
69+
@transport.connections.first.connection.expects(:http).with(:GET).returns(stub_everything)
70+
@transport.connections.first.connection.expects(:body_str).returns(stub_everything)
71+
@transport.connections.first.connection.expects(:header_str).returns(stub_everything)
72+
73+
@transport.connections.first.connection.expects(:headers=).with do |headers|
74+
assert_equal 'application/json', headers['Accept']
75+
true
76+
end
77+
78+
response = @transport.perform_request 'GET', '/'
79+
end
80+
6881
should "handle HTTP methods" do
6982
@transport.connections.first.connection.expects(:http).with(:HEAD).returns(stub_everything)
7083
@transport.connections.first.connection.expects(:http).with(:GET).returns(stub_everything)

elasticsearch-transport/test/unit/transport_faraday_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Elasticsearch::Transport::Transport::HTTP::FaradayTest < Test::Unit::TestC
3636
assert_equal :post, method
3737
assert_equal '{"foo":"bar"}', body
3838
assert_equal 'application/json', headers['Content-Type']
39+
assert_equal 'application/json', headers['Accept']
3940
true
4041
end.returns(stub_everything)
4142
@transport.perform_request 'POST', '/', {}, {:foo => 'bar'}

0 commit comments

Comments
 (0)