Skip to content
Snippets Groups Projects
Commit aa4a7f52 authored by Jan Tattermusch's avatar Jan Tattermusch
Browse files

Merge pull request #5528 from rafaelsales/raise-on-unexpected-metadata

Raise on unexpected metadata values
parents 2c7f8f1f bc846f72
No related branches found
No related tags found
No related merge requests found
...@@ -359,7 +359,7 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) { ...@@ -359,7 +359,7 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) {
md_ary->metadata[md_ary->count].value_length = value_len; md_ary->metadata[md_ary->count].value_length = value_len;
md_ary->count += 1; md_ary->count += 1;
} }
} else { } else if (TYPE(val) == T_STRING) {
value_str = RSTRING_PTR(val); value_str = RSTRING_PTR(val);
value_len = RSTRING_LEN(val); value_len = RSTRING_LEN(val);
if (!grpc_is_binary_header(key_str, key_len) && if (!grpc_is_binary_header(key_str, key_len) &&
...@@ -373,6 +373,10 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) { ...@@ -373,6 +373,10 @@ static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) {
md_ary->metadata[md_ary->count].value = value_str; md_ary->metadata[md_ary->count].value = value_str;
md_ary->metadata[md_ary->count].value_length = value_len; md_ary->metadata[md_ary->count].value_length = value_len;
md_ary->count += 1; md_ary->count += 1;
} else {
rb_raise(rb_eArgError,
"Header values must be of type string or array");
return ST_STOP;
} }
return ST_CONTINUE; return ST_CONTINUE;
......
...@@ -193,44 +193,45 @@ describe 'ClientStub' do ...@@ -193,44 +193,45 @@ describe 'ClientStub' do
describe '#client_streamer' do describe '#client_streamer' do
shared_examples 'client streaming' do shared_examples 'client streaming' do
before(:each) do before(:each) do
server_port = create_test_server
host = "localhost:#{server_port}"
@stub = GRPC::ClientStub.new(host, @cq, :this_channel_is_insecure)
@options = { k1: 'v1', k2: 'v2' }
@sent_msgs = Array.new(3) { |i| 'msg_' + (i + 1).to_s } @sent_msgs = Array.new(3) { |i| 'msg_' + (i + 1).to_s }
@resp = 'a_reply' @resp = 'a_reply'
end end
it 'should send requests to/receive a reply from a server' do it 'should send requests to/receive a reply from a server' do
server_port = create_test_server
host = "localhost:#{server_port}"
th = run_client_streamer(@sent_msgs, @resp, @pass) th = run_client_streamer(@sent_msgs, @resp, @pass)
stub = GRPC::ClientStub.new(host, @cq, :this_channel_is_insecure) expect(get_response(@stub)).to eq(@resp)
expect(get_response(stub)).to eq(@resp)
th.join th.join
end end
it 'should send metadata to the server ok' do it 'should send metadata to the server ok' do
server_port = create_test_server th = run_client_streamer(@sent_msgs, @resp, @pass, @options)
host = "localhost:#{server_port}" expect(get_response(@stub)).to eq(@resp)
th = run_client_streamer(@sent_msgs, @resp, @pass,
k1: 'v1', k2: 'v2')
stub = GRPC::ClientStub.new(host, @cq, :this_channel_is_insecure)
expect(get_response(stub)).to eq(@resp)
th.join th.join
end end
it 'should raise an error if the status is not ok' do it 'should raise an error if the status is not ok' do
server_port = create_test_server
host = "localhost:#{server_port}"
th = run_client_streamer(@sent_msgs, @resp, @fail) th = run_client_streamer(@sent_msgs, @resp, @fail)
stub = GRPC::ClientStub.new(host, @cq, :this_channel_is_insecure) blk = proc { get_response(@stub) }
blk = proc { get_response(stub) }
expect(&blk).to raise_error(GRPC::BadStatus) expect(&blk).to raise_error(GRPC::BadStatus)
th.join th.join
end end
it 'should raise ArgumentError if metadata contains invalid values' do
@options.merge!(k3: 3)
expect do
get_response(@stub)
end.to raise_error(ArgumentError,
/Header values must be of type string or array/)
end
end end
describe 'without a call operation' do describe 'without a call operation' do
def get_response(stub) def get_response(stub)
stub.client_streamer(@method, @sent_msgs, noop, noop, stub.client_streamer(@method, @sent_msgs, noop, noop, @options)
k1: 'v1', k2: 'v2')
end end
it_behaves_like 'client streaming' it_behaves_like 'client streaming'
...@@ -239,7 +240,7 @@ describe 'ClientStub' do ...@@ -239,7 +240,7 @@ describe 'ClientStub' do
describe 'via a call operation' do describe 'via a call operation' do
def get_response(stub) def get_response(stub)
op = stub.client_streamer(@method, @sent_msgs, noop, noop, op = stub.client_streamer(@method, @sent_msgs, noop, noop,
return_op: true, k1: 'v1', k2: 'v2') @options.merge(return_op: true))
expect(op).to be_a(GRPC::ActiveCall::Operation) expect(op).to be_a(GRPC::ActiveCall::Operation)
op.execute op.execute
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment