Skip to content

Commit

Permalink
Merge pull request #101 from PinSeeker/support-https-for-redis-task-b…
Browse files Browse the repository at this point in the history
…ackend

Support HTTPS for Redis task backend
  • Loading branch information
alachaum authored Nov 21, 2023
2 parents 6aaa98e + a17be40 commit dd77d7e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
4 changes: 3 additions & 1 deletion lib/cloudtasker/backend/redis_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
65 changes: 39 additions & 26 deletions spec/cloudtasker/backend/redis_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit dd77d7e

Please sign in to comment.