From: Bryan Duxbury Date: Thu, 5 Aug 2010 22:12:01 +0000 (+0000) Subject: THRIFT-811. rb: http_client_transport.rb: allow custom http headers X-Git-Tag: 0.4.0~31 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=ad776c14256b3dd0b8d3ff6d466416c3f7f1f642;p=common%2Fthrift.git 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 git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@982804 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/rb/lib/thrift/transport/http_client_transport.rb b/lib/rb/lib/thrift/transport/http_client_transport.rb index e62a51c1..22caf022 100644 --- a/lib/rb/lib/thrift/transport/http_client_transport.rb +++ b/lib/rb/lib/thrift/transport/http_client_transport.rb @@ -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 diff --git a/lib/rb/spec/http_client_spec.rb b/lib/rb/spec/http_client_spec.rb index c5a95343..c919051c 100644 --- a/lib/rb/spec/http_client_spec.rb +++ b/lib/rb/spec/http_client_spec.rb @@ -45,5 +45,20 @@ class ThriftHTTPClientTransportSpec < Spec::ExampleGroup @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