|
| 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 | + |
| 18 | +require 'spec_helper' |
| 19 | + |
| 20 | +describe Elasticsearch::Transport::Client do |
| 21 | + context 'meta-header' do |
| 22 | + let(:client) do |
| 23 | + described_class.new.tap do |klient| |
| 24 | + allow(klient).to receive(:__build_connections) |
| 25 | + end |
| 26 | + end |
| 27 | + let(:subject) { client.transport.connections.first.connection.headers } |
| 28 | + let(:regexp) { /^[a-z]{1,}=[a-z0-9.\-]{1,}(?:,[a-z]{1,}=[a-z0-9.\-]+)*$/ } |
| 29 | + let(:meta_header) do |
| 30 | + if RUBY_ENGINE == 'jruby' |
| 31 | + "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION},jr=#{JRUBY_VERSION}" |
| 32 | + else |
| 33 | + "es=#{Elasticsearch::VERSION},rb=#{RUBY_VERSION},t=#{Elasticsearch::Transport::VERSION}" |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + context 'single use of meta header' do |
| 38 | + it 'x-elastic-client-header value matches regexp' do |
| 39 | + expect(subject['x-elastic-client-meta']).to match(regexp) |
| 40 | + expect(subject).to include('x-elastic-client-meta' => meta_header) |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + context 'when using user-agent headers' do |
| 45 | + let(:client) do |
| 46 | + transport_options = { headers: { user_agent: 'My Ruby App' } } |
| 47 | + described_class.new(transport_options: transport_options).tap do |klient| |
| 48 | + allow(klient).to receive(:__build_connections) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + it 'is friendly to previously set headers' do |
| 53 | + expect(subject).to include(user_agent: 'My Ruby App') |
| 54 | + expect(subject).to include('x-elastic-client-meta' => meta_header) |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context 'when using API Key' do |
| 59 | + let(:client) do |
| 60 | + described_class.new(api_key: 'an_api_key') |
| 61 | + end |
| 62 | + |
| 63 | + let(:authorization_header) do |
| 64 | + client.transport.connections.first.connection.headers['Authorization'] |
| 65 | + end |
| 66 | + |
| 67 | + it 'Adds the ApiKey header to the connection' do |
| 68 | + expect(authorization_header).to eq('ApiKey an_api_key') |
| 69 | + expect(subject['x-elastic-client-meta']).to match(regexp) |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | +end |
0 commit comments