Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Update MakeRequest examples.

Sebastian Palma783afc61f4d3
diff --git a/api/app/jobs/spree/webhooks/endpoints/make_request_job.rb b/api/app/jobs/spree/webhooks/endpoints/make_request_job.rb
index 900a6ead4d..5a3f49b766 100644
--- a/api/app/jobs/spree/webhooks/endpoints/make_request_job.rb
+++ b/api/app/jobs/spree/webhooks/endpoints/make_request_job.rb
@@ -5,7 +5,7 @@ module Spree
queue_as :spree_webhooks
def perform(body, url)
- Spree::Webhooks::Endpoints::MakeRequest.call(body: body, url: url)
+ Spree::Webhooks::Endpoints::MakeRequest.new(body: body, url: url).call
end
end
end
diff --git a/api/app/services/spree/webhooks/endpoints/make_request.rb b/api/app/services/spree/webhooks/endpoints/make_request.rb
index 570e7da70d..92148c01d7 100644
--- a/api/app/services/spree/webhooks/endpoints/make_request.rb
+++ b/api/app/services/spree/webhooks/endpoints/make_request.rb
@@ -1,53 +1,66 @@
+# frozen_string_literal: true
+
module Spree
module Webhooks
module Endpoints
class MakeRequest
- prepend Spree::ServiceModule::Base
+ def initialize(body:, url:)
+ @body = body
+ @url = url
+ end
+
+ def call
+ return if body == ''
+
+ Rails.logger.debug("Making a request to #{url} with body #{body}")
- # rubocop:disable Lint/UnusedMethodArgument
- def call(body:, url:)
- return failure(false) if body == ''
+ if unprocessable_uri?
+ Rails.logger.warn(UNPROCESSABLE_MSG)
+ return
+ end
+
+ if failed_request?
+ Rails.logger.warn(FAIL_MSG)
+ return
+ end
- run :make_request
+ Rails.logger.debug(SUCCESS_MSG)
end
- # rubocop:enable Lint/UnusedMethodArgument
private
+ attr_reader :body, :url
+
+ FAIL_MSG = 'Webhook request finished with errors'
HEADERS = { 'Content-Type' => 'application/json' }.freeze
- private_constant :HEADERS
-
- def make_request(body:, url:)
- uri = URI(url)
- Rails.logger.debug('logging')
- if uri.path == '' && uri.host.nil? && uri.port.nil?
- Rails.logger.debug('webhook request finished with errors')
- failure(false)
- else
- if request_code_type(body, uri) == Net::HTTPOK
- Rails.logger.debug('webhook sent successfully')
- success(true)
- else
- Rails.logger.warn('webhook request finished with errors')
- success(false)
- end
- end
+ SUCCESS_MSG = 'Webhook request completed successfully'
+ UNPROCESSABLE_MSG = 'Can not make a request to the given URL'
+ private_constant :FAIL_MSG, :HEADERS, :SUCCESS_MSG, :UNPROCESSABLE_MSG
+
+ delegate :host, :path, :port, to: :uri, prefix: true
+
+ def unprocessable_uri?
+ uri_path == '' && uri_host.nil? && uri_port.nil?
+ end
+
+ def failed_request?
+ request_code_type != Net::HTTPOK
end
- def request_code_type(body, uri)
- http = Net::HTTP.new(uri.host, uri.port)
- http.use_ssl = true if ssl?
- http.request(request(body, uri.path)).code_type
+ def request_code_type
+ http = Net::HTTP.new(uri_host, uri_port)
+ http.use_ssl = true unless Rails.env.development? || Rails.env.test?
+ http.request(request).code_type
end
- def request(body, uri_path)
+ def request
req = Net::HTTP::Post.new(uri_path, HEADERS)
req.body = body
req
end
- def ssl?
- !(Rails.env.development? || Rails.env.test?)
+ def uri
+ URI(url)
end
end
end
diff --git a/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb b/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb
index 5205a233c7..cd19eb7a76 100644
--- a/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb
+++ b/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb
@@ -2,7 +2,7 @@ require 'spec_helper'
describe Spree::Webhooks::Endpoints::MakeRequest do
describe '#call' do
- subject { described_class.call(body: body, url: url) }
+ subject { described_class.new(body: body, url: url).call }
let(:url) { 'http://google.com/' }
let(:body) { { foo: :bar }.to_json }
@@ -13,26 +13,28 @@ describe Spree::Webhooks::Endpoints::MakeRequest do
subject
end
- it 'returns a failure' do
- expect(subject.success).to eq(false)
- end
-
- it 'returns false wrapped into a failure' do
- expect(subject.value).to eq(false)
+ it 'returns nil' do
+ expect(subject).to eq(nil)
end
end
- context 'without an empty string as body' do
+ context 'with an unexpected body' do
let(:body) { '' }
include_examples 'returns a failure without making a request'
end
- context 'with a valid body' do
+ context 'with an expected body' do
context 'without a valid URL' do
let(:url) { '' }
include_examples 'returns a failure without making a request'
+
+ it 'debug logs before the request' do
+ allow(Rails.logger).to receive(:warn)
+ subject
+ expect(Rails.logger).to have_received(:warn).with('Can not make a request to the given URL')
+ end
end
context 'with a valid URL' do
@@ -42,51 +44,13 @@ describe Spree::Webhooks::Endpoints::MakeRequest do
it 'makes a post HTTP request to the given url and body' do
subject
- expect(WebMock).to(
- have_requested(:post, url).with(body: body, headers: headers).once
- )
+ expect(WebMock).to have_requested(:post, url).with(body: body, headers: headers).once
end
it 'debug logs before the request' do
allow(Rails.logger).to receive(:debug)
subject
- expect(Rails.logger).to have_received(:debug).with('logging')
- end
-
- context 'logging after the request' do
- context 'when fail' do
- before do
- http_double = instance_double(Net::HTTP)
- allow(Net::HTTP).to receive(:new).and_return(http_double)
- allow(http_double).to(
- receive(:request).and_return(
- double(:request).tap do |request|
- allow(request).to receive(:code_type).and_return(Net::HTTPClientError)
- end
- )
- )
- end
-
- it 'warn logs after the request' do
- allow(Rails.logger).to receive(:warn)
- subject
- expect(Rails.logger).to have_received(:warn).with('webhook request finished with errors')
- end
- end
-
- context 'when success' do
- it 'debug logs after the request' do
- allow(Rails.logger).to receive(:debug)
- subject
- expect(Rails.logger).to have_received(:debug).with('webhook sent successfully')
- end
- end
- end
-
- context 'when request code_type is Net::HTTPOK' do
- it 'returns true wrapped into a success' do
- expect(subject.value).to eq(true)
- end
+ expect(Rails.logger).to have_received(:debug).with("Making a request to #{url} with body #{body}")
end
context 'when request code_type is not Net::HTTPOK' do
@@ -102,12 +66,20 @@ describe Spree::Webhooks::Endpoints::MakeRequest do
)
end
- it 'returns a success' do
- expect(subject.success).to eq(true)
+ it 'warn logs after the request' do
+ allow(Rails.logger).to receive(:warn)
+ subject
+ expect(Rails.logger).to have_received(:warn).with('Webhook request finished with errors')
end
- it 'returns true wrapped into a success' do
- expect(subject.value).to eq(false)
+ it 'returns nil' do
+ expect(subject).to eq(nil)
+ end
+ end
+
+ context 'when request code_type is Net::HTTPOK' do
+ it 'debug logs after the request and returns its value' do
+ expect(subject).to eq(Rails.logger.debug('Webhook request completed successfully'))
end
end
end