From bfe8a9febc97c0de542fe01c5ecb05512e7afd12 Mon Sep 17 00:00:00 2001
From: Tim Emiola <temiola@google.com>
Date: Thu, 8 Oct 2015 09:05:28 -0700
Subject: [PATCH] Fixes the broken ruby interop tests

---
 src/ruby/lib/grpc/generic/client_stub.rb  | 22 ++++++++++++++--------
 src/ruby/spec/generic/client_stub_spec.rb | 14 ++++++++++++++
 2 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/src/ruby/lib/grpc/generic/client_stub.rb b/src/ruby/lib/grpc/generic/client_stub.rb
index 8c92384529..b8e33ad295 100644
--- a/src/ruby/lib/grpc/generic/client_stub.rb
+++ b/src/ruby/lib/grpc/generic/client_stub.rb
@@ -176,8 +176,7 @@ module GRPC
                           deadline: deadline,
                           timeout: timeout,
                           parent: parent)
-      kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method)
-      md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri)
+      md = update_metadata(kw, method)
       return c.request_response(req, **md) unless return_op
 
       # return the operation view of the active_call; define #execute as a
@@ -244,8 +243,7 @@ module GRPC
                           deadline: deadline,
                           timeout: timeout,
                           parent: parent)
-      kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method)
-      md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri)
+      md = update_metadata(kw, method)
       return c.client_streamer(requests, **md) unless return_op
 
       # return the operation view of the active_call; define #execute as a
@@ -322,8 +320,7 @@ module GRPC
                           deadline: deadline,
                           timeout: timeout,
                           parent: parent)
-      kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method)
-      md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri)
+      md = update_metadata(kw, method)
       return c.server_streamer(req, **md, &blk) unless return_op
 
       # return the operation view of the active_call; define #execute
@@ -439,8 +436,7 @@ module GRPC
                           deadline: deadline,
                           timeout: timeout,
                           parent: parent)
-      kw_with_jwt_uri = self.class.update_with_jwt_aud_uri(kw, @host, method)
-      md = @update_metadata.nil? ? kw : @update_metadata.call(kw_with_jwt_uri)
+      md = update_metadata(kw, method)
       return c.bidi_streamer(requests, **md, &blk) unless return_op
 
       # return the operation view of the active_call; define #execute
@@ -454,6 +450,16 @@ module GRPC
 
     private
 
+    def update_metadata(kw, method)
+      return kw if @update_metadata.nil?
+      just_jwt_uri = self.class.update_with_jwt_aud_uri({}, @host, method)
+      updated = @update_metadata.call(just_jwt_uri)
+
+      # keys should be lowercase
+      updated = Hash[updated.each_pair.map { |k, v|  [k.downcase, v] }]
+      kw.merge(updated)
+    end
+
     # Creates a new active stub
     #
     # @param method [string] the method being called.
diff --git a/src/ruby/spec/generic/client_stub_spec.rb b/src/ruby/spec/generic/client_stub_spec.rb
index a05433df75..c5173aee1d 100644
--- a/src/ruby/spec/generic/client_stub_spec.rb
+++ b/src/ruby/spec/generic/client_stub_spec.rb
@@ -159,6 +159,20 @@ describe 'ClientStub' do
         th.join
       end
 
+      it 'should downcase the keys provided by the metadata updater' do
+        server_port = create_test_server
+        host = "localhost:#{server_port}"
+        th = run_request_response(@sent_msg, @resp, @pass,
+                                  k1: 'downcased-key-v1', k2: 'v2')
+        update_md = proc do |md|
+          md[:K1] = 'downcased-key-v1'
+          md
+        end
+        stub = GRPC::ClientStub.new(host, @cq, update_metadata: update_md)
+        expect(get_response(stub)).to eq(@resp)
+        th.join
+      end
+
       it 'should send a request when configured using an override channel' do
         server_port = create_test_server
         alt_host = "localhost:#{server_port}"
-- 
GitLab