You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by br...@apache.org on 2010/08/06 00:12:02 UTC

svn commit: r982804 - in /incubator/thrift/trunk/lib/rb: lib/thrift/transport/http_client_transport.rb spec/http_client_spec.rb

Author: bryanduxbury
Date: Thu Aug  5 22:12:01 2010
New Revision: 982804

URL: http://svn.apache.org/viewvc?rev=982804&view=rev
Log:
THRIFT-811. rb: http_client_transport.rb: allow custom http headers

Allows setting of custom http headers in http_client_transport.rb 

Patch: Tony Kamenick

Modified:
    incubator/thrift/trunk/lib/rb/lib/thrift/transport/http_client_transport.rb
    incubator/thrift/trunk/lib/rb/spec/http_client_spec.rb

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/transport/http_client_transport.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/transport/http_client_transport.rb?rev=982804&r1=982803&r2=982804&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/transport/http_client_transport.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/transport/http_client_transport.rb Thu Aug  5 22:12:01 2010
@@ -1,5 +1,5 @@
 # encoding: ascii-8bit
-# 
+#
 # Licensed to the Apache Software Foundation (ASF) under one
 # or more contributor license agreements. See the NOTICE file
 # distributed with this work for additional information
@@ -7,16 +7,16 @@
 # to you 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 'net/http'
 require 'net/https'
@@ -25,19 +25,25 @@ require 'stringio'
 
 module Thrift
   class HTTPClientTransport < BaseTransport
+
     def initialize(url)
       @url = URI url
+      @headers = {'Content-Type' => 'application/x-thrift'}
       @outbuf = ""
     end
 
     def open?; true end
     def read(sz); @inbuf.read sz end
     def write(buf); @outbuf << buf end
+
+    def add_headers(headers)
+      @headers = @headers.merge(headers)
+    end
+
     def flush
       http = Net::HTTP.new @url.host, @url.port
       http.use_ssl = @url.scheme == "https"
-      headers = { 'Content-Type' => 'application/x-thrift' }
-      resp, data = http.post(@url.request_uri, @outbuf, headers)
+      resp, data = http.post(@url.request_uri, @outbuf, @headers)
       @inbuf = StringIO.new data
       @outbuf = ""
     end

Modified: incubator/thrift/trunk/lib/rb/spec/http_client_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/http_client_spec.rb?rev=982804&r1=982803&r2=982804&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/http_client_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/http_client_spec.rb Thu Aug  5 22:12:01 2010
@@ -45,5 +45,20 @@ class ThriftHTTPClientTransportSpec < Sp
       @client.flush
       @client.read(10).should == "data"
     end
+
+    it "should send custom headers if defined" do
+      @client.write "test"
+      custom_headers = {"Cookie" => "Foo"}
+      headers = {"Content-Type"=>"application/x-thrift"}.merge(custom_headers)
+
+      @client.add_headers(custom_headers)
+      Net::HTTP.should_receive(:new).with("my.domain.com", 80).and_return do
+        mock("Net::HTTP").tee do |http|
+          http.should_receive(:use_ssl=).with(false)
+          http.should_receive(:post).with("/path/to/service?param=value", "test", headers).and_return([nil, "data"])
+        end
+      end
+      @client.flush
+    end
   end
 end