如何在HTTParty中设置端口

时间:2012-10-29 15:42:56

标签: ruby ssl httparty

我读到HTTParty使用SSL if the port is set to 443,但我没有看到如何设置端口。有人可以为我澄清一下吗?

2 个答案:

答案 0 :(得分:5)

检查规格:

https://github.com/jnunemaker/httparty/blob/82a90c1b93b076f9d2d7410123c2b5d609401a1f/spec/httparty/request_spec.rb#L41

目标URL应使用端口443.只需在目标URI末尾添加:443即可使HTTParty使用SSL。

顺便说一下,HTTPS URL也会使用SSL。

示例:

http://foo.com     => no SSL
https://foo.com    => SSL
http://foo.com:443 => SSL

答案 1 :(得分:3)

除非端口为80且请求为HTTP:

,否则它实际上默认使用HTTPS
context "using port 80" do
  let(:uri) { URI 'http://foobar.com' }
  it { should_not use_ssl }
end

context "using port 443 for ssl" do
  let(:uri) { URI 'https://api.foo.com/v1:443' }
  it { should use_ssl }
end

context "https scheme with default port" do
  it { should use_ssl }
end

context "https scheme with non-standard port" do
  let(:uri) { URI 'https://foobar.com:123456' }
  it { should use_ssl }
end

https://github.com/jnunemaker/httparty/blob/master/spec/httparty/connection_adapter_spec.rb

这是我的规格:

describe Foo do
  it 'is https' do
    adapter = HTTParty::ConnectionAdapter.new(URI(subject.base_uri))
    expect(adapter.connection.use_ssl?).to eq(true)
  end
end