Skip to content

Commit

Permalink
Merge pull request Shopify#1396 from Shopify/ruby-cli-support-spin-th…
Browse files Browse the repository at this point in the history
…eme-app-extension-server

Fixed problem with theme app extension server in spin
  • Loading branch information
alvaro-shopify authored Feb 17, 2023
2 parents e703acd + bf53537 commit 7f724e1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-papayas-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-kit': patch
---

Fixed problem with theme app extension server in spin
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ def get_response_headers(response, env)
# (Taken from Rack::Proxy)
response_headers.reject! { |k| HOP_BY_HOP_HEADERS.include?(k.downcase) }

if response_headers["location"]&.include?("myshopify.com")
if response_headers["location"]&.include?("myshopify.com") ||
response_headers["location"]&.include?("spin.dev")
response_headers["location"].gsub!(%r{(https://#{shop})}, "http://#{host(env)}")
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ def setup
super

ShopifyCLI::DB.stubs(:exists?).with(:shop).returns(true)
ShopifyCLI::DB
.stubs(:get)
.with(:shop)
.returns("dev-theme-server-store.myshopify.com")
ShopifyCLI::DB
.stubs(:get)
.with(:development_theme_id)
.returns("123456789")
stub_shop

root = ShopifyCLI::ROOT + "/test/fixtures/theme"

Expand Down Expand Up @@ -206,6 +203,21 @@ def test_storefront_redirect_headers_are_rewritten
assert_equal("http://127.0.0.1:8282/password", response.headers["Location"])
end

def test_storefront_with_spin_redirect_headers_are_rewritten
stub_shop("eu.spin.dev")
stub_request(:get, "https://dev-theme-server-store.eu.spin.dev/?_fd=0&pb=0")
.with(headers: default_proxy_headers("eu.spin.dev"))
.to_return(status: 302, headers: {
"Location" => "https://dev-theme-server-store.eu.spin.dev/password",
})
@proxy.stubs(:host).returns("127.0.0.1:8282")

stub_session_id_request("eu.spin.dev")
response = request.get("/")

assert_equal("http://127.0.0.1:8282/password", response.headers["Location"])
end

def test_non_storefront_redirect_headers_are_not_rewritten
stub_request(:get, "https://dev-theme-server-store.myshopify.com/?_fd=0&pb=0")
.with(headers: default_proxy_headers)
Expand Down Expand Up @@ -440,21 +452,21 @@ def request
Rack::MockRequest.new(@proxy)
end

def default_proxy_headers
def default_proxy_headers(domain = "myshopify.com")
{
"Accept-Encoding" => "none",
"Cookie" => "_secure_session_id=#{SECURE_SESSION_ID}",
"Host" => "dev-theme-server-store.myshopify.com",
"Host" => "dev-theme-server-store.#{domain}",
"X-Forwarded-For" => "",
"User-Agent" => "Shopify CLI",
}
end

def stub_session_id_request
stub_request(:head, "https://dev-theme-server-store.myshopify.com/?_fd=0&pb=0&preview_theme_id=123456789")
def stub_session_id_request(domain = "myshopify.com")
stub_request(:head, "https://dev-theme-server-store.#{domain}/?_fd=0&pb=0&preview_theme_id=123456789")
.with(
headers: {
"Host" => "dev-theme-server-store.myshopify.com",
"Host" => "dev-theme-server-store.#{domain}",
},
)
.to_return(
Expand All @@ -464,6 +476,13 @@ def stub_session_id_request
},
)
end

def stub_shop(domain = "myshopify.com")
ShopifyCLI::DB
.stubs(:get)
.with(:shop)
.returns("dev-theme-server-store.#{domain}")
end
end
end
end
Expand Down

0 comments on commit 7f724e1

Please sign in to comment.