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 HTTPS for Redis task backend #101

Merged
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: 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
Loading