Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Proxy setting. #78

Merged
merged 1 commit into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ Apnotic::Connection.new(options)
| :cert_pass | Optional. The certificate's password.
| :url | Optional. Defaults to https://api.push.apple.com:443.
| :connect_timeout | Optional. Expressed in seconds, defaults to 30.
| :proxy_addr | Optional. Proxy server. e.g. http://proxy.example.com
| :proxy_port | Optional. Proxy port. e.g. 8080
| :proxy_user | Optional. User name for proxy authentication. e.g. user_name
| :proxy_pass | Optional. Password for proxy authentication. e.g. pass_word

Note that since `:cert_path` can be any object that responds to `:read`, it is possible to pass in a certificate string directly by wrapping it up in a `StringIO` object:

Expand Down
11 changes: 10 additions & 1 deletion lib/apnotic/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Apnotic

APPLE_DEVELOPMENT_SERVER_URL = "https://api.sandbox.push.apple.com:443"
APPLE_PRODUCTION_SERVER_URL = "https://api.push.apple.com:443"
PROXY_SETTINGS_KEYS = [:proxy_addr, :proxy_port, :proxy_user, :proxy_pass]

class Connection
attr_reader :url, :cert_path
Expand All @@ -28,7 +29,15 @@ def initialize(options={})

raise "Cert file not found: #{@cert_path}" unless @cert_path && (@cert_path.respond_to?(:read) || File.exist?(@cert_path))

@client = NetHttp2::Client.new(@url, ssl_context: ssl_context, connect_timeout: @connect_timeout)
http2_options = {
ssl_context: ssl_context,
connect_timeout: @connect_timeout
}
PROXY_SETTINGS_KEYS.each do |key|
http2_options[key] = options[key] if options[key]
end

@client = NetHttp2::Client.new(@url, http2_options)
end

def push(notification, options={})
Expand Down
25 changes: 25 additions & 0 deletions spec/apnotic/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@
describe Apnotic::Connection do
let(:url) { "https://localhost" }
let(:cert_path) { apn_file_path }
let(:proxy_settings) {
{
proxy_addr: "http://proxy",
proxy_port: "8080",
proxy_user: "proxy-user",
proxy_pass: "proxy-pass"
}
}
let(:connection) do
Apnotic::Connection.new({
url: url,
cert_path: cert_path
})
end
let(:connection_proxy) do
Apnotic::Connection.new({
url: url,
cert_path: cert_path
}.merge(proxy_settings))
end

describe ".new" do

Expand Down Expand Up @@ -64,6 +78,17 @@
end
end
end

describe "option: proxy family" do
context "when proxy is set" do
it "has proxy-assigned NetHttp2 instance" do
client = connection_proxy.instance_variable_get(:@client)
proxy_settings.each do |k, v|
expect(client.instance_variable_get("@#{k}")).to eq v
end
end
end
end
end

describe ".development" do
Expand Down