|
| 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 | + |
| 19 | +describe Elasticsearch::Client do |
| 20 | + let(:user_agent) { |
| 21 | + "elasticsearch-ruby/#{Elasticsearch::VERSION}; elastic-transport-ruby/#{Elastic::Transport::VERSION}; RUBY_VERSION: #{RUBY_VERSION}; #{RbConfig::CONFIG['host_os'].split('_').first[/[a-z]+/i].downcase} #{RbConfig::CONFIG['target_cpu']}" |
| 22 | + } |
| 23 | + |
| 24 | + context 'when no user-agent is set on initialization' do |
| 25 | + let(:client) { described_class.new } |
| 26 | + |
| 27 | + it 'has the expected header' do |
| 28 | + expect(client.transport.options[:transport_options][:headers][:user_agent]).to eq user_agent |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + context 'when a header is specified on initialization' do |
| 33 | + let(:client) do |
| 34 | + described_class.new( |
| 35 | + transport_options: { headers: { 'X-Test-Header' => 'Test' } } |
| 36 | + ) |
| 37 | + end |
| 38 | + |
| 39 | + it 'has the expected header' do |
| 40 | + expect(client.transport.options[:transport_options][:headers][:user_agent]).to eq user_agent |
| 41 | + expect(client.transport.options[:transport_options][:headers]['X-Test-Header']).to eq 'Test' |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + context 'when other transport_options are specified on initialization' do |
| 46 | + let(:client) do |
| 47 | + described_class.new( |
| 48 | + transport_options: { params: { format: 'yaml' } } |
| 49 | + ) |
| 50 | + end |
| 51 | + |
| 52 | + it 'has the expected header' do |
| 53 | + expect(client.transport.options[:transport_options][:headers][:user_agent]).to eq user_agent |
| 54 | + expect(client.transport.options[:transport_options][:params][:format]).to eq 'yaml' |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context 'when :user_agent is specified on initialization' do |
| 59 | + let(:client) do |
| 60 | + described_class.new( |
| 61 | + transport_options: { headers: { user_agent: 'TestApp' } } |
| 62 | + ) |
| 63 | + end |
| 64 | + |
| 65 | + it 'has the expected header' do |
| 66 | + expect(client.transport.options[:transport_options][:headers][:user_agent]).to eq 'TestApp' |
| 67 | + end |
| 68 | + end |
| 69 | +end |
0 commit comments