Skip to content

Commit

Permalink
Merge pull request #93 from Bugagazavr/proxy-support
Browse files Browse the repository at this point in the history
Add proxy support
  • Loading branch information
zaru authored Nov 16, 2020
2 parents d130e76 + 11e09e2 commit 2ff0bd2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/webpush/request.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'uri'
require 'jwt'
require 'base64'

Expand All @@ -20,21 +21,30 @@ def initialize(message: '', subscription:, vapid:, **options)

# rubocop:disable Metrics/AbcSize
def perform
http = Net::HTTP.new(uri.host, uri.port)
http = Net::HTTP.new(uri.host, uri.port, *proxy_options)
http.use_ssl = true
http.ssl_timeout = @options[:ssl_timeout] unless @options[:ssl_timeout].nil?
http.open_timeout = @options[:open_timeout] unless @options[:open_timeout].nil?
http.read_timeout = @options[:read_timeout] unless @options[:read_timeout].nil?

req = Net::HTTP::Post.new(uri.request_uri, headers)
req.body = body

resp = http.request(req)
verify_response(resp)

resp
end
# rubocop:enable Metrics/AbcSize

def proxy_options
return [] unless @options[:proxy]

proxy_uri = URI.parse(@options[:proxy])

[proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password]
end

# rubocop:disable Metrics/MethodLength
def headers
headers = {}
Expand Down
14 changes: 14 additions & 0 deletions spec/webpush/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ def build_request_with_api_key(endpoint, options = {})
end
end

describe '#proxy_options' do
it 'returns an array of proxy options' do
request = build_request(proxy: 'http://user:password@proxy_addr:8080')

expect(request.proxy_options).to eq(['proxy_addr', 8080, 'user', 'password'])
end

it 'returns empty array' do
request = build_request

expect(request.proxy_options).to be_empty
end
end

def build_request(options = {})
subscription = {
endpoint: endpoint,
Expand Down

0 comments on commit 2ff0bd2

Please sign in to comment.