From 14d074b5385bbfe1416d5465ba96529ea0567c3b Mon Sep 17 00:00:00 2001 From: Thomas Marek Date: Mon, 20 Nov 2023 09:54:44 -0500 Subject: [PATCH] Support HTTPS for Redis task backend --- lib/cloudtasker/backend/redis_task.rb | 4 +- spec/cloudtasker/backend/redis_task_spec.rb | 95 +++++++++++++++------ 2 files changed, 70 insertions(+), 29 deletions(-) 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..a45066bd 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,74 @@ 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) + context 'with HTTP' do + 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 } - 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 + let(:url) { 'https://localhost:300/run' } + let!(:https_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(https_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 end