testdouble/cypress-rails

Using (multiple) subdomains; change base URL like Cypress’ Config API (ish) …?

Closed this issue · 4 comments

First of all, thank you for putting this out in the world 🙏

What I'm trying to do:
Run specs across two different subdomains, eg.

// app_area_1.spec.js
context('Trying', () => {
  it('tries', () => {
    cy.visit("http://subdomain1.lvh.me")
    
  });
});

// app_area_2.spec.js
   
   cy.visit("http://subdomain2.lvh.me")
   

My app has two routing constraints, based on subdomain. They are distinct parts of the app, ie. different controllers, views, etc. Somewhat similar to multi-tenancy, or admin/public namespaced apps, so I'd suspect this use case of testing across subdomains might come up for others?

What I've tried:
I've tried this option , continuing from the example above:

// app_area_1.spec.js
Cypress.config("baseUrl", "http://subdomain1.lvh.me")

context('Trying', () => {
  it('tries', () => { 

// app_area_2.spec.js
Cypress.config("baseUrl", "http://subdomain2.lvh.me")

context('Trying', () => {
   

The base URL is changed alright, but Cypress isn't going anywhere, since Puma was booted on default 127.0.0.1.

image

(To be clear, the spec runs correctly whenever ENV['CYPRESS_RAILS_HOST'] matches config.baseUrl.)

Where I'm stuck:

  1. Is ENV['CYPRESS_RAILS_HOST'] the only way to set config.host ? I realize this gem says on the tin, “The cypress-rails gem is configured entirely via environment variables.”, so I could see how it would be?
  2. What would be necessary to support (some of) Cypress’ options to override eg. baseUrl?

Thanks in advance for considering my issue!


Background/paper trail:

  • I've read up on configuration for this gem, env vars specifically
  • Tried combinations of CYPRESS_RAILS_HOST and baseUrl, omitting one, the other, then both, etc. 👯‍♂️
  • Then read up on config.rb, and on how puma starts up to support Cypress
  • Familiarized myself with Cypress' general restrictions on changing hosts, eg. across multiple visit‘s in one example
  • And finally looked at the options for overriding config in Cypress

Also, I've seen #25 and its follow-up #26 — seems related? You mention this in a closing remark:

(By the way, it occurred to me if you set Cypress.config('baseUrl', '/blah') in a support file instead of Cypress.json, you shouldn't need this option since it'll win over either cypress.json or the ENV flag)

Is that similar to my issue here?

Hey thanks for posting this. I haven't gotten a chance to formulate a response yet because issues of domain-level routing logic are complex enough (is it client-side or server-side routing? what kind of request is being made to the domain?) that it's not super clear what fixes I could propose that would fix you.

Can you try to reproduce the scenario you're describing in a minimal repo for me to look at?

@cabgfx don't you just need to boot it on -b 0.0.0.0 so the websocket can receive requests from any IP address?

boot it on -b 0.0.0.0

Seems possible, @yagudaev and thanks for that! Just so I'm clear, does it here refer to booting Cypres-rails' puma server on 0.0.0.0, or did you mean something else?

And thanks @searls I'll try to elaborate with a repo to show my issue.
FWIW, I mean server side routing, with this in my routes.rb:

Rails.application.routes.draw do
  def subdomain_for(env_var)
    # in development, I have eg. subdomain1.myapp.test (using puma-dev),
    # so when I use subdomain1.lvh.me in specs,
    # the lvh.me domain points back to localhost
    # and Rails will match subdomain1 or subdomain2, with these constraints below
    ActionDispatch::Http::URL.extract_subdomain(ENV[env_var], 1)
  end

  constraints subdomain: subdomain_for("PreshopDomain") do
     # app routing
  end

  constraints subdomain: subdomain_for("WebshopDomain") do
     # app routing
  end

  constraints subdomain: "" do
    get "/", to: redirect(domain: ENV["WebshopDomain"])
  end
end

Yeah I did mean the puma server on 0.0.0.0.

What was the 500 error in the server logs?