aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/ruby/spec/generic/client_interceptors_spec.rb
blob: f292715e4dfe56740cdc38ed3d8032ef5723089b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Copyright 2017 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'spec_helper'

describe 'Client Interceptors' do
  let(:interceptor) { TestClientInterceptor.new }
  let(:interceptors_opts) { { interceptors: [interceptor] } }
  let(:request) { EchoMsg.new }
  let(:service) { EchoService }

  before(:each) do
    build_rpc_server
  end

  context 'when a client interceptor is added' do
    context 'with a request/response call' do
      it 'should be called', server: true do
        expect(interceptor).to receive(:request_response)
          .once.and_call_original

        run_services_on_server(@server, services: [service]) do
          stub = build_insecure_stub(EchoStub, opts: interceptors_opts)
          expect_any_instance_of(GRPC::ActiveCall).to receive(:request_response)
            .once.and_call_original
          expect(stub.an_rpc(request)).to be_a(EchoMsg)
        end
      end

      it 'can modify outgoing metadata', server: true do
        expect(interceptor).to receive(:request_response)
          .once.and_call_original

        run_services_on_server(@server, services: [service]) do
          stub = build_insecure_stub(EchoStub, opts: interceptors_opts)
          expect_any_instance_of(GRPC::ActiveCall).to receive(:request_response)
            .with(request, metadata: { 'foo' => 'bar_from_request_response' })
            .once.and_call_original
          expect(stub.an_rpc(request)).to be_a(EchoMsg)
        end
      end
    end

    context 'with a client streaming call' do
      it 'should be called', server: true do
        expect(interceptor).to receive(:client_streamer)
          .once.and_call_original

        run_services_on_server(@server, services: [service]) do
          stub = build_insecure_stub(EchoStub, opts: interceptors_opts)
          expect_any_instance_of(GRPC::ActiveCall).to receive(:client_streamer)
            .once.and_call_original
          requests = [EchoMsg.new, EchoMsg.new]
          expect(stub.a_client_streaming_rpc(requests)).to be_a(EchoMsg)
        end
      end

      it 'can modify outgoing metadata', server: true do
        expect(interceptor).to receive(:client_streamer)
          .once.and_call_original

        run_services_on_server(@server, services: [service]) do
          stub = build_insecure_stub(EchoStub, opts: interceptors_opts)
          requests = [EchoMsg.new, EchoMsg.new]
          expect_any_instance_of(GRPC::ActiveCall).to receive(:client_streamer)
            .with(requests, metadata: { 'foo' => 'bar_from_client_streamer' })
            .once.and_call_original
          expect(stub.a_client_streaming_rpc(requests)).to be_a(EchoMsg)
        end
      end
    end

    context 'with a server streaming call' do
      it 'should be called', server: true do
        expect(interceptor).to receive(:server_streamer)
          .once.and_call_original

        run_services_on_server(@server, services: [service]) do
          stub = build_insecure_stub(EchoStub, opts: interceptors_opts)
          request = EchoMsg.new
          expect_any_instance_of(GRPC::ActiveCall).to receive(:server_streamer)
            .once.and_call_original
          responses = stub.a_server_streaming_rpc(request)
          responses.each do |r|
            expect(r).to be_a(EchoMsg)
          end
        end
      end

      it 'can modify outgoing metadata', server: true do
        expect(interceptor).to receive(:server_streamer)
          .once.and_call_original

        run_services_on_server(@server, services: [service]) do
          stub = build_insecure_stub(EchoStub, opts: interceptors_opts)
          request = EchoMsg.new
          expect_any_instance_of(GRPC::ActiveCall).to receive(:server_streamer)
            .with(request, metadata: { 'foo' => 'bar_from_server_streamer' })
            .once.and_call_original
          responses = stub.a_server_streaming_rpc(request)
          responses.each do |r|
            expect(r).to be_a(EchoMsg)
          end
        end
      end
    end

    context 'with a bidi call' do
      it 'should be called', server: true do
        expect(interceptor).to receive(:bidi_streamer)
          .once.and_call_original

        run_services_on_server(@server, services: [service]) do
          stub = build_insecure_stub(EchoStub, opts: interceptors_opts)
          expect_any_instance_of(GRPC::ActiveCall).to receive(:bidi_streamer)
            .once.and_call_original
          requests = [EchoMsg.new, EchoMsg.new]
          responses = stub.a_bidi_rpc(requests)
          responses.each do |r|
            expect(r).to be_a(EchoMsg)
          end
        end
      end

      it 'can modify outgoing metadata', server: true do
        expect(interceptor).to receive(:bidi_streamer)
          .once.and_call_original

        run_services_on_server(@server, services: [service]) do
          stub = build_insecure_stub(EchoStub, opts: interceptors_opts)
          requests = [EchoMsg.new, EchoMsg.new]
          expect_any_instance_of(GRPC::ActiveCall).to receive(:bidi_streamer)
            .with(requests, metadata: { 'foo' => 'bar_from_bidi_streamer' })
            .once.and_call_original
          responses = stub.a_bidi_rpc(requests)
          responses.each do |r|
            expect(r).to be_a(EchoMsg)
          end
        end
      end
    end
  end
end