|
| 1 | +# Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +# license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright |
| 4 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +# the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +require 'spec_helper' |
| 18 | +require 'ostruct' |
| 19 | + |
| 20 | +describe Elasticsearch::Client do |
| 21 | + context 'when a header is set on an endpoint request' do |
| 22 | + let(:client) { described_class.new } |
| 23 | + let(:headers) { { 'user-agent' => 'my ruby app' } } |
| 24 | + |
| 25 | + it 'performs the request with the header' do |
| 26 | + allow(client).to receive(:perform_request) { OpenStruct.new(body: '') } |
| 27 | + expect { client.search(headers: headers) }.not_to raise_error |
| 28 | + expect(client).to have_received(:perform_request) |
| 29 | + .with('GET', '_search', {}, nil, headers) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + context 'when a header is set on an endpoint request and on initialization' do |
| 34 | + let!(:client) do |
| 35 | + described_class.new( |
| 36 | + host: 'http://localhost:9200', |
| 37 | + transport_options: { headers: instance_headers } |
| 38 | + ) |
| 39 | + end |
| 40 | + let(:instance_headers) { { set_in_instantiation: 'header value' } } |
| 41 | + let(:param_headers) {{'user-agent' => 'My Ruby Tests', 'set-on-method-call' => 'header value'}} |
| 42 | + |
| 43 | + it 'performs the request with the header' do |
| 44 | + expected_headers = client.transport.connections.connections.first.connection.headers.merge(param_headers) |
| 45 | + |
| 46 | + expect_any_instance_of(Faraday::Connection) |
| 47 | + .to receive(:run_request) |
| 48 | + .with(:get, "http://localhost:9200/_search", nil, expected_headers) { OpenStruct.new(body: '')} |
| 49 | + |
| 50 | + client.search(headers: param_headers) |
| 51 | + end |
| 52 | + end |
| 53 | +end |
0 commit comments