• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15require 'spec_helper'
16
17# a test service that checks the cert of its peer
18class UserAgentEchoService
19  include GRPC::GenericService
20  rpc :an_rpc, EchoMsg, EchoMsg
21
22  def an_rpc(_req, call)
23    EchoMsg.new(msg: call.metadata['user-agent'])
24  end
25end
26
27UserAgentEchoServiceStub = UserAgentEchoService.rpc_stub_class
28
29describe 'user agent' do
30  RpcServer = GRPC::RpcServer
31
32  before(:all) do
33    server_opts = {
34      poll_period: 1
35    }
36    @srv = new_rpc_server_for_testing(**server_opts)
37    @port = @srv.add_http2_port('0.0.0.0:0', :this_port_is_insecure)
38    @srv.handle(UserAgentEchoService)
39    @srv_thd = Thread.new { @srv.run }
40    @srv.wait_till_running
41  end
42
43  after(:all) do
44    expect(@srv.stopped?).to be(false)
45    @srv.stop
46    @srv_thd.join
47  end
48
49  it 'client sends expected user agent' do
50    stub = UserAgentEchoServiceStub.new("localhost:#{@port}",
51                                        :this_channel_is_insecure,
52                                        {})
53    response = stub.an_rpc(EchoMsg.new)
54    expected_user_agent_prefix = "grpc-ruby/#{GRPC::VERSION}"
55    expect(response.msg.start_with?(expected_user_agent_prefix)).to be true
56    # check that the expected user agent prefix occurs in the real user agent exactly once
57    expect(response.msg.split(expected_user_agent_prefix).size).to eq 2
58  end
59
60  it 'user agent header does not grow when the same channel args hash is used across multiple stubs' do
61    shared_channel_args_hash = {}
62    10.times do
63      stub = UserAgentEchoServiceStub.new("localhost:#{@port}",
64                                          :this_channel_is_insecure,
65                                          channel_args: shared_channel_args_hash)
66      response = stub.an_rpc(EchoMsg.new)
67      puts "got echo response: #{response.msg}"
68      expected_user_agent_prefix = "grpc-ruby/#{GRPC::VERSION}"
69      expect(response.msg.start_with?(expected_user_agent_prefix)).to be true
70      # check that the expected user agent prefix occurs in the real user agent exactly once
71      expect(response.msg.split(expected_user_agent_prefix).size).to eq 2
72    end
73  end
74end
75