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.

Handle properly `Store#url` with port supplied (#12477)

Damian Legawiecd83288fd3c12
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 557e9f9319..d8b0567475 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -266,20 +266,21 @@ module Spree
@formatted_url ||= if Rails.env.development? || Rails.env.test?
scheme = Rails.application.routes.default_url_options[:protocol] || :http
port = Rails.application.routes.default_url_options[:port].presence || (Rails.env.development? ? 3000 : nil)
+ clean_url = url.to_s.sub(%r{^https?://}, '').split(':').first
if scheme.to_sym == :https
URI::HTTPS.build(
- host: url.to_s.sub(%r{^https?://}, ''),
+ host: clean_url,
port: port
).to_s
else
URI::HTTP.build(
- host: url.to_s.sub(%r{^https?://}, '').split(':').first,
+ host: clean_url,
port: port
).to_s
end
else
URI::HTTPS.build(
- host: url.to_s.sub(%r{^https?://}, '')
+ host: clean_url
).to_s
end
end
diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rb
index ec4916f19a..b1c0533a40 100644
--- a/core/spec/models/spree/store_spec.rb
+++ b/core/spec/models/spree/store_spec.rb
@@ -785,13 +785,19 @@ describe Spree::Store, type: :model do
end
describe '#formatted_url' do
- let(:store) { create(:store, code: 'mystore', url: nil) }
+ let(:store) { build(:store, code: 'mystore', url: 'mystore.mydomain.dev') }
it { expect(store.formatted_url).to eq('http://mystore.mydomain.dev:3000') }
+
+ context 'url with port' do
+ let(:store) { build(:store, code: 'mystore', url: 'localhost:3000') }
+
+ it { expect(store.formatted_url).to eq('http://localhost:3000') }
+ end
end
describe '#formatted_url_or_custom_domain' do
- let(:store) { create(:store, code: 'mystore', url: nil) }
+ let(:store) { build(:store, code: 'mystore', url: 'mystore.mydomain.dev:3000') }
context 'without custom domain' do
it { expect(store.formatted_url_or_custom_domain).to eq('http://mystore.mydomain.dev:3000') }