diff --git a/lib/cloudtasker/backend/redis_task.rb b/lib/cloudtasker/backend/redis_task.rb index 73334e3e..c8387a9a 100644 --- a/lib/cloudtasker/backend/redis_task.rb +++ b/lib/cloudtasker/backend/redis_task.rb @@ -257,7 +257,9 @@ def http_client @http_client ||= begin uri = URI(http_request[:url]) - Net::HTTP.new(uri.host, uri.port).tap { |e| e.read_timeout = dispatch_deadline } + http = Net::HTTP.new(uri.host, uri.port).tap { |e| e.read_timeout = dispatch_deadline } + http.use_ssl = true if uri.instance_of?(URI::HTTPS) + http end end diff --git a/spec/cloudtasker/backend/redis_task_spec.rb b/spec/cloudtasker/backend/redis_task_spec.rb index 96fcac77..1b3bb8bb 100644 --- a/spec/cloudtasker/backend/redis_task_spec.rb +++ b/spec/cloudtasker/backend/redis_task_spec.rb @@ -4,11 +4,12 @@ RSpec.describe Cloudtasker::Backend::RedisTask do let(:redis) { described_class.redis } + let(:url) { 'http://localhost:300/run' } let(:job_payload) do { http_request: { http_method: 'POST', - url: 'http://localhost:300/run', + url: url, headers: { 'Content-Type': 'application/json', Authorization: 'Bearer 123' @@ -242,36 +243,48 @@ subject { task.deliver } let(:status) { 200 } - let!(:http_stub) do - stub_request(:post, job_payload.dig(:http_request, :url)) - .with( - headers: { - Cloudtasker::Config::TASK_ID_HEADER => task_id, - Cloudtasker::Config::RETRY_HEADER => job_payload[:retries] - }, - body: job_payload.dig(:http_request, :body) - ) - .to_return(status: status) - end - before do - allow(task).to receive(:destroy).and_return(true) - allow(task).to receive(:retry_later).with(described_class::RETRY_INTERVAL).and_return(true) + shared_examples 'of delivering a task' do |protocol| + let(:url) { "#{protocol}://localhost:300/run" } + let!(:http_stub) do + stub_request(:post, job_payload.dig(:http_request, :url)) + .with( + headers: { + Cloudtasker::Config::TASK_ID_HEADER => task_id, + Cloudtasker::Config::RETRY_HEADER => job_payload[:retries] + }, + body: job_payload.dig(:http_request, :body) + ) + .to_return(status: status) + end + + before do + allow(task).to receive(:destroy).and_return(true) + allow(task).to receive(:retry_later).with(described_class::RETRY_INTERVAL).and_return(true) + end + after { expect(http_stub).to have_been_requested } + + context 'with success' do + after { expect(task).to have_received(:destroy) } + after { expect(task).not_to have_received(:retry_later) } + it { is_expected.to be_truthy } + end + + context 'with failure' do + let(:status) { 500 } + + after { expect(task).not_to have_received(:destroy) } + after { expect(task).to have_received(:retry_later) } + it { is_expected.to be_truthy } + end end - after { expect(http_stub).to have_been_requested } - context 'with success' do - after { expect(task).to have_received(:destroy) } - after { expect(task).not_to have_received(:retry_later) } - it { is_expected.to be_truthy } + context 'with HTTP' do + include_examples 'of delivering a task', 'http' end - context 'with failure' do - let(:status) { 500 } - - after { expect(task).not_to have_received(:destroy) } - after { expect(task).to have_received(:retry_later) } - it { is_expected.to be_truthy } + context 'with HTTPS' do + include_examples 'of delivering a task', 'https' end end