mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Formatting cleanup.
This commit is contained in:
parent
e976c80348
commit
c0f0dc0e7b
11 changed files with 50 additions and 40 deletions
|
@ -1,7 +1,7 @@
|
|||
class HTTParty::CookieHash < Hash #:nodoc:
|
||||
|
||||
|
||||
CLIENT_COOKIES = %w{path expires domain path secure HTTPOnly}
|
||||
|
||||
|
||||
def add_cookies(value)
|
||||
case value
|
||||
when Hash
|
||||
|
|
|
@ -48,4 +48,4 @@ module HTTParty
|
|||
param
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -64,7 +64,7 @@ module HTTParty
|
|||
klass === response
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def respond_to?(name)
|
||||
return true if [:request,:response,:parsed_response,:body,:headers].include?(name)
|
||||
parsed_response.respond_to?(name) or response.respond_to?(name)
|
||||
|
|
|
@ -6,7 +6,6 @@ describe HTTParty::CookieHash do
|
|||
end
|
||||
|
||||
describe "#add_cookies" do
|
||||
|
||||
describe "with a hash" do
|
||||
it "should add new key/value pairs to the hash" do
|
||||
@cookie_hash.add_cookies(:foo => "bar")
|
||||
|
@ -29,7 +28,7 @@ describe HTTParty::CookieHash do
|
|||
@cookie_hash[:second].should == 'two'
|
||||
@cookie_hash[:third].should == nil
|
||||
end
|
||||
|
||||
|
||||
it "should overwrite any existing key" do
|
||||
@cookie_hash[:foo] = 'bar'
|
||||
@cookie_hash.add_cookies("foo=tar")
|
||||
|
@ -37,7 +36,7 @@ describe HTTParty::CookieHash do
|
|||
@cookie_hash[:foo].should eql("tar")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe 'with other class' do
|
||||
it "should error" do
|
||||
lambda {
|
||||
|
@ -61,7 +60,7 @@ describe HTTParty::CookieHash do
|
|||
@s.should match(/rofl=copter/)
|
||||
@s.should match(/^\w+=\w+; \w+=\w+$/)
|
||||
end
|
||||
|
||||
|
||||
it "should not include client side only cookies" do
|
||||
@cookie_hash.add_cookies(:path => "/")
|
||||
@s = @cookie_hash.to_cookie_string
|
||||
|
|
|
@ -13,11 +13,11 @@ describe Net::HTTPHeader::DigestAuthenticator do
|
|||
@digest.authorization_header.join(", ")
|
||||
end
|
||||
|
||||
|
||||
context "with specified quality of protection (qop)" do
|
||||
before do
|
||||
@digest = setup_digest({'www-authenticate' =>
|
||||
'Digest realm="myhost@testrealm.com", nonce="NONCE", qop="auth"'})
|
||||
@digest = setup_digest({
|
||||
'www-authenticate' => 'Digest realm="myhost@testrealm.com", nonce="NONCE", qop="auth"',
|
||||
})
|
||||
end
|
||||
|
||||
it "should set prefix" do
|
||||
|
@ -45,9 +45,7 @@ describe Net::HTTPHeader::DigestAuthenticator do
|
|||
end
|
||||
|
||||
it "should set response" do
|
||||
request_digest =
|
||||
"md5(md5(Mufasa:myhost@testrealm.com:Circle Of Life)" +
|
||||
":NONCE:0:md5(deadbeef):auth:md5(GET:/dir/index.html))"
|
||||
request_digest = "md5(md5(Mufasa:myhost@testrealm.com:Circle Of Life):NONCE:0:md5(deadbeef):auth:md5(GET:/dir/index.html))"
|
||||
authorization_header.should include(%Q(response="#{request_digest}"))
|
||||
end
|
||||
end
|
||||
|
@ -55,8 +53,9 @@ describe Net::HTTPHeader::DigestAuthenticator do
|
|||
|
||||
context "with unspecified quality of protection (qop)" do
|
||||
before do
|
||||
@digest = setup_digest({'www-authenticate' =>
|
||||
'Digest realm="myhost@testrealm.com", nonce="NONCE"'})
|
||||
@digest = setup_digest({
|
||||
'www-authenticate' => 'Digest realm="myhost@testrealm.com", nonce="NONCE"',
|
||||
})
|
||||
end
|
||||
|
||||
it "should set prefix" do
|
||||
|
@ -84,9 +83,7 @@ describe Net::HTTPHeader::DigestAuthenticator do
|
|||
end
|
||||
|
||||
it "should set response" do
|
||||
request_digest =
|
||||
"md5(md5(Mufasa:myhost@testrealm.com:Circle Of Life)" +
|
||||
":NONCE:md5(GET:/dir/index.html))"
|
||||
request_digest = "md5(md5(Mufasa:myhost@testrealm.com:Circle Of Life):NONCE:md5(GET:/dir/index.html))"
|
||||
authorization_header.should include(%Q(response="#{request_digest}"))
|
||||
end
|
||||
end
|
||||
|
|
|
@ -54,7 +54,7 @@ describe HTTParty::Response do
|
|||
response = HTTParty::Response.new(@request_object, @response_object, {'foo' => 'bar'})
|
||||
response['foo'].should == 'bar'
|
||||
end
|
||||
|
||||
|
||||
it "should respond_to? methods it supports" do
|
||||
response = HTTParty::Response.new(@request_object, @response_object, {'foo' => 'bar'})
|
||||
response.respond_to?(:parsed_response).should be_true
|
||||
|
@ -102,9 +102,9 @@ describe HTTParty::Response do
|
|||
|
||||
describe "semantic methods for response codes" do
|
||||
def response_mock(klass)
|
||||
r = klass.new('', '', '')
|
||||
r.stub(:body)
|
||||
r
|
||||
response = klass.new('', '', '')
|
||||
response.stub(:body)
|
||||
response
|
||||
end
|
||||
|
||||
context "major codes" do
|
||||
|
|
|
@ -3,11 +3,11 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
|
|||
describe HTTParty::Request do
|
||||
context "SSL certificate verification" do
|
||||
before do
|
||||
FakeWeb.allow_net_connect = true # enable network connections just for this test
|
||||
FakeWeb.allow_net_connect = true
|
||||
end
|
||||
|
||||
after do
|
||||
FakeWeb.allow_net_connect = false # Restore allow_net_connect value for testing
|
||||
FakeWeb.allow_net_connect = false
|
||||
end
|
||||
|
||||
it "should work with when no trusted CA list is specified" do
|
||||
|
@ -25,6 +25,7 @@ describe HTTParty::Request do
|
|||
it "should work when using ssl_ca_file with a certificate authority" do
|
||||
ssl_verify_test(:ssl_ca_file, "ca.crt", "server.crt").should == {'success' => true}
|
||||
end
|
||||
|
||||
it "should work when using ssl_ca_path with a certificate authority" do
|
||||
ssl_verify_test(:ssl_ca_path, ".", "server.crt").should == {'success' => true}
|
||||
end
|
||||
|
@ -34,6 +35,7 @@ describe HTTParty::Request do
|
|||
ssl_verify_test(:ssl_ca_file, "ca.crt", "selfsigned.crt")
|
||||
end.should raise_error(OpenSSL::SSL::SSLError)
|
||||
end
|
||||
|
||||
it "should fail when using ssl_ca_path and the server uses an unrecognized certificate authority" do
|
||||
lambda do
|
||||
ssl_verify_test(:ssl_ca_path, ".", "selfsigned.crt")
|
||||
|
@ -45,6 +47,7 @@ describe HTTParty::Request do
|
|||
ssl_verify_test(:ssl_ca_file, "ca.crt", "bogushost.crt")
|
||||
end.should raise_error(OpenSSL::SSL::SSLError)
|
||||
end
|
||||
|
||||
it "should fail when using ssl_ca_path and the server uses a bogus hostname" do
|
||||
lambda do
|
||||
ssl_verify_test(:ssl_ca_path, ".", "bogushost.crt")
|
||||
|
|
|
@ -10,6 +10,7 @@ describe HTTParty do
|
|||
before do
|
||||
Kernel.stub(:warn)
|
||||
end
|
||||
|
||||
it "warns with a deprecation message" do
|
||||
Kernel.should_receive(:warn).with("Deprecated: Use HTTParty::Parser::SupportedFormats")
|
||||
HTTParty::AllowedFormats
|
||||
|
@ -21,7 +22,6 @@ describe HTTParty do
|
|||
end
|
||||
|
||||
describe "pem" do
|
||||
|
||||
it 'should set the pem content' do
|
||||
@klass.pem 'PEM-CONTENT'
|
||||
@klass.default_options[:pem].should == 'PEM-CONTENT'
|
||||
|
@ -36,7 +36,6 @@ describe HTTParty do
|
|||
@klass.pem 'PEM-CONTENT', 'PASSWORD'
|
||||
@klass.default_options[:pem_password].should == 'PASSWORD'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe 'http_proxy' do
|
||||
|
|
|
@ -13,9 +13,11 @@ Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].e
|
|||
Spec::Runner.configure do |config|
|
||||
config.include HTTParty::StubResponse
|
||||
config.include HTTParty::SSLTestHelper
|
||||
|
||||
config.before(:suite) do
|
||||
FakeWeb.allow_net_connect = false
|
||||
end
|
||||
|
||||
config.after(:suite) do
|
||||
FakeWeb.allow_net_connect = true
|
||||
end
|
||||
|
|
|
@ -3,19 +3,18 @@ module HTTParty
|
|||
def ssl_verify_test(mode, ca_basename, server_cert_filename)
|
||||
test_server = nil
|
||||
begin
|
||||
# Start an HTTPS server
|
||||
test_server = SSLTestServer.new(
|
||||
:rsa_key => File.read(File.expand_path("../../fixtures/ssl/generated/server.key", __FILE__)),
|
||||
:cert => File.read(File.expand_path("../../fixtures/ssl/generated/#{server_cert_filename}", __FILE__)))
|
||||
:cert => File.read(File.expand_path("../../fixtures/ssl/generated/#{server_cert_filename}", __FILE__)))
|
||||
|
||||
test_server.start
|
||||
|
||||
# Build a request
|
||||
if mode
|
||||
ca_path = File.expand_path("../../fixtures/ssl/generated/#{ca_basename}", __FILE__)
|
||||
raise ArgumentError.new("#{ca_path} does not exist") unless File.exist?(ca_path)
|
||||
return HTTParty.get("https://localhost:#{test_server.port}/", :format => :json, :timeout=>30, mode => ca_path)
|
||||
return HTTParty.get("https://localhost:#{test_server.port}/", :format => :json, :timeout => 30, mode => ca_path)
|
||||
else
|
||||
return HTTParty.get("https://localhost:#{test_server.port}/", :format => :json, :timeout=>30)
|
||||
return HTTParty.get("https://localhost:#{test_server.port}/", :format => :json, :timeout => 30)
|
||||
end
|
||||
ensure
|
||||
test_server.stop if test_server
|
||||
|
|
|
@ -10,26 +10,30 @@ class SSLTestServer
|
|||
attr_reader :port
|
||||
|
||||
def initialize(options={})
|
||||
@ctx = OpenSSL::SSL::SSLContext.new
|
||||
@ctx.cert = OpenSSL::X509::Certificate.new(options[:cert])
|
||||
@ctx.key = OpenSSL::PKey::RSA.new(options[:rsa_key])
|
||||
@ctx = OpenSSL::SSL::SSLContext.new
|
||||
@ctx.cert = OpenSSL::X509::Certificate.new(options[:cert])
|
||||
@ctx.key = OpenSSL::PKey::RSA.new(options[:rsa_key])
|
||||
@ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE # Don't verify client certificate
|
||||
@port = options[:port] || 0
|
||||
@thread = nil
|
||||
@stopping_mutex = Mutex.new
|
||||
@stopping = false
|
||||
@port = options[:port] || 0
|
||||
@thread = nil
|
||||
@stopping_mutex = Mutex.new
|
||||
@stopping = false
|
||||
end
|
||||
|
||||
def start
|
||||
@raw_server = TCPServer.new(@port)
|
||||
|
||||
if @port == 0
|
||||
@port = Socket::getnameinfo(@raw_server.getsockname, Socket::NI_NUMERICHOST|Socket::NI_NUMERICSERV)[1].to_i
|
||||
end
|
||||
|
||||
@ssl_server = OpenSSL::SSL::SSLServer.new(@raw_server, @ctx)
|
||||
|
||||
@stopping_mutex.synchronize{
|
||||
return if @stopping
|
||||
@thread = Thread.new{ thread_main }
|
||||
}
|
||||
|
||||
nil
|
||||
end
|
||||
|
||||
|
@ -46,24 +50,31 @@ class SSLTestServer
|
|||
def thread_main
|
||||
until @stopping_mutex.synchronize{ @stopping }
|
||||
(rr,ww,ee) = select([@ssl_server.to_io], nil, nil, 0.1)
|
||||
|
||||
next unless rr && rr.include?(@ssl_server.to_io)
|
||||
|
||||
socket = @ssl_server.accept
|
||||
|
||||
Thread.new{
|
||||
header = []
|
||||
|
||||
until (line = socket.readline).rstrip.empty?
|
||||
header << line
|
||||
end
|
||||
|
||||
socket.write <<'EOF'.gsub(/\r\n/n, "\n").gsub(/\n/n, "\r\n")
|
||||
response =<<EOF
|
||||
HTTP/1.1 200 OK
|
||||
Connection: close
|
||||
Content-Type: application/json; charset=UTF-8
|
||||
|
||||
{"success":true}
|
||||
EOF
|
||||
|
||||
socket.write(response.gsub(/\r\n/n, "\n").gsub(/\n/n, "\r\n"))
|
||||
socket.close
|
||||
}
|
||||
end
|
||||
|
||||
@ssl_server.close
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue