-
Notifications
You must be signed in to change notification settings - Fork 617
Avoid duplicated content-type headers #2962
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -73,20 +73,18 @@ | |||||||||
|
|
||||||||||
| expect_any_instance_of(Faraday::Connection) | ||||||||||
| .to receive(:run_request) | ||||||||||
| .with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') } | ||||||||||
| .with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') } | ||||||||||
| client.search | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| context 'when content-type header is changed' do | ||||||||||
| let!(:client) do | ||||||||||
| described_class.new( | ||||||||||
| host: 'http://localhost:9200', | ||||||||||
| transport_options: { headers: instance_headers } | ||||||||||
| ).tap do |client| | ||||||||||
| described_class.new(transport_options: { headers: instance_headers }).tap do |client| | ||||||||||
| client.instance_variable_set('@verified', true) | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| let(:instance_headers) do | ||||||||||
| { content_type: 'application/json' } | ||||||||||
| end | ||||||||||
|
|
@@ -98,11 +96,88 @@ | |||||||||
|
|
||||||||||
| expect_any_instance_of(Faraday::Connection) | ||||||||||
| .to receive(:run_request) | ||||||||||
| .with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') } | ||||||||||
| .with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') } | ||||||||||
| client.search | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| context 'when Content-Type header is used' do | ||||||||||
| let(:client) do | ||||||||||
| described_class.new(transport_options: { headers: instance_headers }).tap do |client| | ||||||||||
| client.instance_variable_set('@verified', true) | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| let(:instance_headers) do | ||||||||||
| { 'Content-Type' => 'application/text' } | ||||||||||
| end | ||||||||||
|
|
||||||||||
| it 'performs the request with the header' do | ||||||||||
| connection_headers = client.transport.connections.connections.first.connection.headers | ||||||||||
| expect(connection_headers['Accept']).to eq 'application/vnd.elasticsearch+json; compatible-with=9' | ||||||||||
| expect(connection_headers['Content-Type']).to eq 'application/text' | ||||||||||
|
|
||||||||||
| expect_any_instance_of(Faraday::Connection) | ||||||||||
| .to receive(:run_request) | ||||||||||
| .with(:get, 'http://localhost:9200/_search', nil, connection_headers) { OpenStruct.new(body: '') } | ||||||||||
| client.search | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| if defined?(JRUBY_VERSION) | ||||||||||
| context 'Using Manticore with JRuby' do | ||||||||||
| let(:client) do | ||||||||||
| require 'elastic/transport/transport/http/manticore' | ||||||||||
| described_class.new( | ||||||||||
| transport_class: Elastic::Transport::Transport::HTTP::Manticore, | ||||||||||
| transport_options: { headers: instance_headers } | ||||||||||
| ).tap do |client| | ||||||||||
| client.instance_variable_set('@verified', true) | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| context 'when Content-Type header is used' do | ||||||||||
| let(:instance_headers) do | ||||||||||
| { 'Content-Type' => 'application/text' } | ||||||||||
| end | ||||||||||
|
|
||||||||||
| it 'does not duplicate content-type the header' do | ||||||||||
| connection_headers = client.transport.connections.connections.first.connection.instance_variable_get('@options')[:headers] | ||||||||||
| expect(connection_headers['accept']).to eq 'application/vnd.elasticsearch+json; compatible-with=9' | ||||||||||
| expect(connection_headers['content-type']).to be nil | ||||||||||
| expect(connection_headers.fetch('Content-Type')).to eq 'application/text' | ||||||||||
|
|
||||||||||
| expect_any_instance_of(Manticore::Client) | ||||||||||
| .to receive(:get).with( | ||||||||||
| 'http://localhost:9200/_search', | ||||||||||
| { headers: connection_headers } | ||||||||||
| ) { OpenStruct.new(body: '') } | ||||||||||
| client.search | ||||||||||
| end | ||||||||||
|
|
||||||||||
| context 'when content-type header is used' do | ||||||||||
| let(:instance_headers) do | ||||||||||
| { 'content-type' => 'application/text' } | ||||||||||
| end | ||||||||||
|
|
||||||||||
| it 'does not duplicate Content-Type the header' do | ||||||||||
| connection_headers = client.transport.connections.connections.first.connection.instance_variable_get('@options')[:headers] | ||||||||||
| expect(connection_headers['accept']).to eq 'application/vnd.elasticsearch+json; compatible-with=9' | ||||||||||
| expect(connection_headers['content-type']).to be nil | ||||||||||
| expect(connection_headers.fetch('Content-Type')).to eq 'application/text' | ||||||||||
|
Comment on lines
+166
to
+167
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this should be reverse since we are setting
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is being set at the transport level in The code eventually sets There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, thank you for explanation. |
||||||||||
|
|
||||||||||
| expect_any_instance_of(Manticore::Client) | ||||||||||
| .to receive(:get).with( | ||||||||||
| 'http://localhost:9200/_search', | ||||||||||
| { headers: connection_headers } | ||||||||||
| ) { OpenStruct.new(body: '') } | ||||||||||
| client.search | ||||||||||
| end | ||||||||||
| end | ||||||||||
| end | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
| context 'when no header is set, uses v9 content-type and accept' do | ||||||||||
| let!(:client) do | ||||||||||
| described_class.new(host: 'http://localhost:9200').tap do |client| | ||||||||||
|
|
@@ -117,7 +192,7 @@ | |||||||||
|
|
||||||||||
| expect_any_instance_of(Faraday::Connection) | ||||||||||
| .to receive(:run_request) | ||||||||||
| .with(:get, 'http://localhost:9200/_search', nil, expected_headers) { OpenStruct.new(body: '') } | ||||||||||
| .with(:get, 'http://localhost:9200/_search', nil, expected_headers) { OpenStruct.new(body: '') } | ||||||||||
| client.search | ||||||||||
| end | ||||||||||
| end | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️