Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add option to disable HTTP compression

  • Loading branch information...
commit 76f5c6b9af55c6251a752c3b3874752ca5431109 1 parent caa2eea
@jou jou authored
Showing with 48 additions and 8 deletions.
  1. +20 −8 lib/stargate/client.rb
  2. +28 −0 spec/hbase-stargate/client_spec.rb
View
28 lib/stargate/client.rb
@@ -15,6 +15,8 @@ class Client
def initialize(url = "http://localhost:8080", opts = {})
@url = URI.parse(url)
+ @default_headers = {}
+
unless @url.kind_of? URI::HTTP
raise "invalid http url: #{url}"
end
@@ -27,38 +29,48 @@ def initialize(url = "http://localhost:8080", opts = {})
@connection = Net::HTTP.new(@url.host, @url.port)
end
@connection.read_timeout = opts[:timeout] if opts[:timeout]
+
+ @default_headers['Accept-Encoding'] = 'identity' if opts.has_key?(:http_compression) && !opts[:http_compression]
end
def get(path, options = {})
- safe_request { @connection.get(@url.path + path, {"Accept" => "application/json"}.merge(options)) }
+ headers = {"Accept" => "application/json"}.merge(@default_headers).merge(options)
+ safe_request { @connection.get(@url.path + path, headers) }
end
def get_response(path, options = {})
- safe_response { @connection.get(@url.path + path, {"Accept" => "application/json"}.merge(options)) }
+ headers = {"Accept" => "application/json"}.merge(@default_headers).merge(options)
+ safe_response { @connection.get(@url.path + path, headers) }
end
def post(path, data = nil, options = {})
- safe_request { @connection.post(@url.path + path, data, {'Content-Type' => 'text/xml'}.merge(options)) }
+ headers = {'Content-Type' => 'text/xml'}.merge(@default_headers).merge(options)
+ safe_request { @connection.post(@url.path + path, data, headers) }
end
def post_response(path, data = nil, options = {})
- safe_response { @connection.post(@url.path + path, data, {'Content-Type' => 'text/xml'}.merge(options)) }
+ headers = {'Content-Type' => 'text/xml'}.merge(@default_headers).merge(options)
+ safe_response { @connection.post(@url.path + path, data, headers) }
end
def delete(path, options = {})
- safe_request { @connection.delete(@url.path + path, options) }
+ headers = @default_headers.merge(options)
+ safe_request { @connection.delete(@url.path + path, headers) }
end
def delete_response(path, options = {})
- safe_response { @connection.delete(@url.path + path, options) }
+ headers = @default_headers.merge(options)
+ safe_response { @connection.delete(@url.path + path, headers) }
end
def put(path, data = nil, options = {})
- safe_request { @connection.put(@url.path + path, data, {'Content-Type' => 'text/xml'}.merge(options)) }
+ headers = {'Content-Type' => 'text/xml'}.merge(@default_headers).merge(options)
+ safe_request { @connection.put(@url.path + path, data, headers) }
end
def put_response(path, data = nil, options = {})
- safe_response { @connection.put(@url.path + path, data, {'Content-Type' => 'text/xml'}.merge(options)) }
+ headers = {'Content-Type' => 'text/xml'}.merge(@default_headers).merge(options)
+ safe_response { @connection.put(@url.path + path, data, headers) }
end
private
View
28 spec/hbase-stargate/client_spec.rb
@@ -0,0 +1,28 @@
+require File.join(File.dirname(__FILE__), "..", "spec_helper")
+
+describe Stargate::Client do
+
+ describe "Init" do
+
+ describe "options" do
+ it "should set Accept-Encoding header if HTTP compression is disabled" do
+ client = Stargate::Client.new("http://localhost:8080", :http_compression => false)
+ client.instance_variable_get("@default_headers")["Accept-Encoding"].should == "identity"
+ end
+
+ it "should not set Accept-Encoding header if HTTP compression is enabled" do
+ client = Stargate::Client.new("http://localhost:8080", :http_compression => true)
+ client.instance_variable_get("@default_headers").should_not have_key("Accept-Encoding")
+ end
+
+ it "should default to compression enabled" do
+ client = Stargate::Client.new("http://localhost:8080", :http_compression => true)
+ client.instance_variable_get("@default_headers").should_not have_key("Accept-Encoding")
+ end
+
+ end
+
+ end
+
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.