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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Add payload/body to HTTP request.
Sebastian Palmafe3076118c6d
diff --git a/api/app/jobs/spree/webhooks/endpoints/make_request_job.rb b/api/app/jobs/spree/webhooks/endpoints/make_request_job.rbindex e7796fae0d..d37ba060f1 100644--- a/api/app/jobs/spree/webhooks/endpoints/make_request_job.rb+++ b/api/app/jobs/spree/webhooks/endpoints/make_request_job.rb@@ -1,9 +1,13 @@module Spree- class Webhooks::Endpoints::MakeRequestJob < Spree::BaseJob- queue_as :spree_webhooks+ module Webhooks+ module Endpoints+ class MakeRequestJob < Spree::BaseJob+ queue_as :spree_webhooks- def perform(url)- Spree::Webhooks::Endpoints::MakeRequest.call(url: url)+ def perform(payload, url)+ Spree::Webhooks::Endpoints::MakeRequest.call(payload: payload, url: url)+ end+ endendendenddiff --git a/api/app/services/spree/webhooks/endpoints/make_request.rb b/api/app/services/spree/webhooks/endpoints/make_request.rbindex b771e4b584..f5f5c5408c 100644--- a/api/app/services/spree/webhooks/endpoints/make_request.rb+++ b/api/app/services/spree/webhooks/endpoints/make_request.rb@@ -4,7 +4,9 @@ module Spreeclass MakeRequestprepend Spree::ServiceModule::Base- def call(url:)+ def call(body:, url:)+ return failure(false) if body == ''+run :make_requestend@@ -13,16 +15,17 @@ module SpreeHEADERS = { 'Content-Type' => 'application/json' }.freezeprivate_constant :HEADERS- def make_request(url:)+ def make_request(body:, url:)uri = URI(url)if uri.path == '' && uri.host.nil? && uri.port.nil?failure(false)elserequest = Net::HTTP::Post.new(uri.path, HEADERS)- request.body = { foo: :bar }.to_json+ request.body = bodyhttp = Net::HTTP.new(uri.host, uri.port)http.use_ssl = true- success(http.request(request))+ code_type = http.request(request).code_type+ success(code_type == Net::HTTPOK)endendenddiff --git a/api/app/services/spree/webhooks/endpoints/queue_requests.rb b/api/app/services/spree/webhooks/endpoints/queue_requests.rbindex aa3f24ec43..f8b1e68b1b 100644--- a/api/app/services/spree/webhooks/endpoints/queue_requests.rb+++ b/api/app/services/spree/webhooks/endpoints/queue_requests.rb@@ -1,28 +1,32 @@module Spree- class Webhooks::Endpoints::QueueRequests- prepend Spree::ServiceModule::Base+ module Webhooks+ module Endpoints+ class QueueRequests+ prepend Spree::ServiceModule::Base- def call(event:, payload:)- urls_subscribed_to(event).each do |url|- Spree::Webhooks::Endpoints::MakeRequestJob.perform_later(url)- end- end+ def call(event:, payload:)+ urls_subscribed_to(event).each do |url|+ Spree::Webhooks::Endpoints::MakeRequestJob.perform_later(payload, url)+ end+ end- private+ private- def urls_subscribed_to(event)- Spree::Webhooks::Endpoint.- where(enabled: true).- where(subscriptions_where_statement(event)).- pluck(:url)- end+ def urls_subscribed_to(event)+ Spree::Webhooks::Endpoint.+ where(enabled: true).+ where(subscriptions_where_statement(event)).+ pluck(:url)+ end- def subscriptions_where_statement(event)- case ActiveRecord::Base.connection.adapter_name- when 'Mysql2'- ["('*' MEMBER OF(subscriptions) OR ? MEMBER OF(subscriptions))", event]- when 'PostgreSQL'- ["subscriptions @> '[\"*\"]' OR subscriptions @> ?", [event].to_json]+ def subscriptions_where_statement(event)+ case ActiveRecord::Base.connection.adapter_name+ when 'Mysql2'+ ["('*' MEMBER OF(subscriptions) OR ? MEMBER OF(subscriptions))", event]+ when 'PostgreSQL'+ ["subscriptions @> '[\"*\"]' OR subscriptions @> ?", [event].to_json]+ end+ endendendenddiff --git a/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb b/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rbindex b2a4851bf5..cc4e4661cf 100644--- a/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb+++ b/api/spec/jobs/spree/webhooks/endpoints/make_request_job_spec.rb@@ -2,9 +2,10 @@ require 'spec_helper'describe Spree::Webhooks::Endpoints::MakeRequestJob, :job dolet(:url) { 'http://url.com/' }+ let(:payload) { {} }it 'it enqueues a HTTP request using Spree::Webhooks::Endpoints::MakeRequest' do- expect { described_class.perform_later(url) }.to(+ expect { described_class.perform_later(payload, url) }.to(have_enqueued_job.on_queue('spree_webhooks'))enddiff --git a/api/spec/models/spree/webhooks/endpoint_spec.rb b/api/spec/models/spree/webhooks/endpoint_spec.rbindex d686e688c6..da2b045ac4 100644--- a/api/spec/models/spree/webhooks/endpoint_spec.rb+++ b/api/spec/models/spree/webhooks/endpoint_spec.rb@@ -1,19 +1,16 @@require 'spec_helper'-FactoryBot.define do- factory :endpoint, class: Spree::Webhooks::Endpoint do- end-end-describe Spree::Store dodescribe 'validations' docontext 'url presence' doit 'is valid with url' do- expect(build(:endpoint, url: 'https://google.com/').valid?).to be(true)+ endpoint = Spree::Webhooks::Endpoint.new(url: 'https://google.com/')+ expect(endpoint.valid?).to be(true)endit 'is invalid without url' do- expect(build(:endpoint, url: nil).valid?).to be(false)+ endpoint = Spree::Webhooks::Endpoint.new(url: nil)+ expect(endpoint.valid?).to be(false)endendenddiff --git a/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb b/api/spec/services/spree/webhooks/endpoints/make_request_spec.rbindex 08397fa4f1..c04c33ec00 100644--- a/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb+++ b/api/spec/services/spree/webhooks/endpoints/make_request_spec.rb@@ -2,30 +2,79 @@ require 'spec_helper'describe Spree::Webhooks::Endpoints::MakeRequest dodescribe '#call' do- context 'with a valid URL' do- let(:url) { 'https://google.com/' }- let(:headers) { { 'Content-Type' => 'application/json' } }+ subject { described_class.call(body: body, url: url) }- before { stub_request(:post, url) }+ let(:url) { 'https://google.com/' }+ let(:body) { { foo: :bar }.to_json }- it 'makes a post HTTP request to the given url' do- described_class.call(url: url)- expect(WebMock).to(- have_requested(:post, url).with(body: {foo: :bar}.to_json, headers: headers).once- )+ shared_examples 'returns a failure without making a request' do+ it 'does not instantiate a Net::HTTP::Post to make a request' do+ expect(Net::HTTP::Post).not_to receive(:new)+ subjectend- it 'returns a success object with true success value' do- expect(described_class.call(url: url).success).to eq(true)+ 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)endend- context 'without a valid URL' do- let(:url) { '' }+ context 'without an empty string as body' do+ let(:body) { '' }- it 'does not instantiate a Net::HTTP::Post to make a request' do- expect(Net::HTTP::Post).not_to receive(:new)- expect(described_class.call(url: url).success).to eq(false)+ include_examples 'returns a failure without making a request'+ end++ context 'with a valid body' do+ context 'without a valid URL' do+ let(:url) { '' }++ include_examples 'returns a failure without making a request'+ end++ context 'with a valid URL' do+ let(:headers) { { 'Content-Type' => 'application/json' } }++ before { stub_request(:post, url) }++ 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+ )+ 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+ end++ context 'when request code_type is not Net::HTTPOK' do+ before do+ http_double = instance_double(Net::HTTP)+ expect(Net::HTTP).to receive(:new).and_return(http_double)+ expect(http_double).to receive(:use_ssl=)+ expect(http_double).to(+ receive(:request).and_return(+ double(:request).tap do |request|+ expect(request).to receive(:code_type).and_return(Net::HTTPClientError)+ end+ )+ )+ end++ it 'returns a success' do+ expect(subject.success).to eq(true)+ end++ it 'returns true wrapped into a success' do+ expect(subject.value).to eq(false)+ end+ endendendenddiff --git a/api/spec/services/spree/webhooks/endpoints/queue_request_spec.rb b/api/spec/services/spree/webhooks/endpoints/queue_request_spec.rbindex 390d522d67..73054255a6 100644--- a/api/spec/services/spree/webhooks/endpoints/queue_request_spec.rb+++ b/api/spec/services/spree/webhooks/endpoints/queue_request_spec.rb@@ -24,7 +24,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job doit 'makes a HTTP request to its URL' doexpect { subject.call(event: event, payload: payload) }.to(have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob)- .with(endpoint.url)+ .with(payload, endpoint.url).on_queue('spree_webhooks'))end@@ -40,7 +40,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job doit 'makes a HTTP request to its URL' doexpect { subject.call(event: event, payload: payload) }.to(have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob)- .with(endpoint.url)+ .with(payload, endpoint.url).on_queue('spree_webhooks'))end@@ -54,7 +54,7 @@ describe Spree::Webhooks::Endpoints::QueueRequests, :job doit 'does not make a HTTP request' doexpect { subject.call(event: event, payload: payload) }.not_to(have_enqueued_job(Spree::Webhooks::Endpoints::MakeRequestJob)- .with(endpoint.url)+ .with(payload, endpoint.url).on_queue('spree_webhooks'))enddiff --git a/core/spec/services/spree/stock_locations/stock_items/create_spec.rb b/core/spec/services/spree/stock_locations/stock_items/create_spec.rbindex dc61c220ea..ab5dd0312a 100644--- a/core/spec/services/spree/stock_locations/stock_items/create_spec.rb+++ b/core/spec/services/spree/stock_locations/stock_items/create_spec.rb@@ -59,6 +59,7 @@ module Spreeendit 'sets the stock location data necessary for the inserted stock items' do+ unrelated_variant.reloadTimecop.freeze(time_current)resultexpect(created_stock_item_attrs).to(