Skip to content

Commit d408acd

Browse files
Add an api_prefix option (shakacode#130)
We have a Rails server is exposed under a proxy, typically https://my-local.dev/api. It didn't work because the middleware was looking for /__cypress__/commands. This PR supports an api_prefix proxy, so that we can target https://my-local.dev/api/__cypress__/commands
1 parent 151770d commit d408acd

File tree

7 files changed

+31
-7
lines changed

7 files changed

+31
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### Changed
2+
* Add support for proxy routes through `api_prefix` [PR 130](https://github.com/shakacode/cypress-on-rails/pull/130) by [RomainEndelin]
3+
14
### Fixed
25
* Properly copies the cypress_helper file when running the update generator [PR 117](https://github.com/shakacode/cypress-on-rails/pull/117) by [alvincrespo]
36

@@ -40,7 +43,7 @@
4043
[Compare]: https://github.com/shakacode/cypress-on-rails/compare/v1.10.1...v1.11.0
4144

4245
### Changed
43-
* improve app command logging on cypress
46+
* improve app command logging on cypress
4447
* Allow build and build_list commands to be executed against factory bot [PR 87](https://github.com/shakacode/cypress-on-rails/pull/87) by [Alexander-Blair]
4548

4649
## [1.10.1]

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
----
88

9-
This project is sponsored by the software consulting firm [ShakaCode](https://www.shakacode.com), creator of the [React on Rails Gem](https://github.com/shakacode/react_on_rails). We focus on React (with TS or ReScript) front-ends, often with Ruby on Rails or Gatsby. See [our recent work](https://www.shakacode.com/recent-work) and [client engagement model](https://www.shakacode.com/blog/client-engagement-model/). Feel free to engage in discussions around this gem at our [Slack Channel](https://join.slack.com/t/reactrails/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE) or our [forum category for Cypress](https://forum.shakacode.com/c/cypress-on-rails/55).
9+
This project is sponsored by the software consulting firm [ShakaCode](https://www.shakacode.com), creator of the [React on Rails Gem](https://github.com/shakacode/react_on_rails). We focus on React (with TS or ReScript) front-ends, often with Ruby on Rails or Gatsby. See [our recent work](https://www.shakacode.com/recent-work) and [client engagement model](https://www.shakacode.com/blog/client-engagement-model/). Feel free to engage in discussions around this gem at our [Slack Channel](https://join.slack.com/t/reactrails/shared_invite/enQtNjY3NTczMjczNzYxLTlmYjdiZmY3MTVlMzU2YWE0OWM0MzNiZDI0MzdkZGFiZTFkYTFkOGVjODBmOWEyYWQ3MzA2NGE1YWJjNmVlMGE) or our [forum category for Cypress](https://forum.shakacode.com/c/cypress-on-rails/55).
1010

1111
Interested in joining a small team that loves open source? Check our [careers page](https://www.shakacode.com/career/).
1212

@@ -62,6 +62,9 @@ bin/rails g cypress_on_rails:install
6262
# if you have/want a different cypress folder (default is cypress)
6363
bin/rails g cypress_on_rails:install --cypress_folder=spec/cypress
6464

65+
# if you target the Rails server with a path prefix to your URL
66+
bin/rails g cypress_on_rails:install --api_prefix=/api
67+
6568
# if you want to install cypress with npm
6669
bin/rails g cypress_on_rails:install --install_cypress_with=npm
6770

@@ -88,7 +91,7 @@ Now you can create scenarios and commands that are plain Ruby files that get loa
8891
### Update your database.yml
8992

9093
When running `cypress test` on your local computer it's recommended to start your server in development mode so that changes you
91-
make are picked up without having to restart the server.
94+
make are picked up without having to restart the server.
9295
It's recommended you update your `database.yml` to check if the `CYPRESS` environment variable is set and switch it to the test database
9396
otherwise cypress will keep clearing your development database.
9497

@@ -115,9 +118,9 @@ Getting started on your local environment
115118
CYPRESS=1 bin/rails server -p 5017
116119

117120
# in separate window start cypress
118-
yarn cypress open
121+
yarn cypress open
119122
# or for npm
120-
node_modules/.bin/cypress open
123+
node_modules/.bin/cypress open
121124
# or if you changed the cypress folder to spec/cypress
122125
yarn cypress open --project ./spec
123126
```
@@ -130,7 +133,7 @@ How to run cypress on CI
130133

131134
yarn run cypress run
132135
# or for npm
133-
node_modules/.bin/cypress run
136+
node_modules/.bin/cypress run
134137
```
135138

136139
### Example of using factory bot
@@ -399,6 +402,17 @@ beforeEach(() => {
399402
});
400403
```
401404

405+
## API Prefix
406+
407+
If your Rails server is exposed under a proxy, typically https://my-local.dev/api, you can use the `api_prefix` option.
408+
In `config/initializers/cypress_on_rails.rb`, add this line:
409+
```ruby
410+
CypressOnRails.configure do |c|
411+
# ...
412+
c.api_prefix = '/api'
413+
end
414+
```
415+
402416
## Contributing
403417

404418
1. Fork it ( https://github.com/shakacode/cypress-on-rails/fork )

lib/cypress_on_rails/configuration.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module CypressOnRails
44
class Configuration
55
attr_accessor :cypress_folder
6+
attr_accessor :api_prefix
67
attr_accessor :use_middleware
78
attr_accessor :use_vcr_middleware
89
attr_accessor :logger
@@ -16,6 +17,7 @@ def initialize
1617

1718
def reset
1819
self.cypress_folder = 'spec/cypress'
20+
self.api_prefix = ''
1921
self.use_middleware = true
2022
self.use_vcr_middleware = false
2123
self.logger = Logger.new(STDOUT)

lib/cypress_on_rails/middleware.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def initialize(app, command_executor = CommandExecutor, file = ::File)
1616

1717
def call(env)
1818
request = Rack::Request.new(env)
19-
if request.path.start_with?('/__cypress__/command')
19+
if request.path.start_with?("#{configuration.api_prefix}/__cypress__/command")
2020
configuration.tagged_logged { handle_command(request) }
2121
else
2222
@app.call(env)

lib/generators/cypress_on_rails/install_generator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module CypressOnRails
22
class InstallGenerator < Rails::Generators::Base
33
class_option :cypress_folder, type: :string, default: 'cypress'
4+
class_option :api_prefix, type: :string, default: ''
45
class_option :install_cypress, type: :boolean, default: true
56
class_option :install_cypress_with, type: :string, default: 'yarn'
67
class_option :experimental, type: :boolean, default: false

lib/generators/cypress_on_rails/templates/config/initializers/cypress_on_rails.rb.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ if defined?(CypressOnRails)
55
# please use with extra caution if enabling on hosted servers or starting your local server on 0.0.0.0
66
c.use_middleware = !Rails.env.production?
77
<% unless options.experimental %># <% end %> c.use_vcr_middleware = !Rails.env.production?
8+
c.api_prefix = "<%= options.api_prefix %>"
89
c.logger = Rails.logger
910
end
1011

spec/cypress_on_rails/configuration_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
CypressOnRails.configure { |config| config.reset }
66

77
expect(CypressOnRails.configuration.cypress_folder).to eq('spec/cypress')
8+
expect(CypressOnRails.configuration.api_prefix).to eq('')
89
expect(CypressOnRails.configuration.use_middleware?).to eq(true)
910
expect(CypressOnRails.configuration.logger).to_not be_nil
1011
end
@@ -13,10 +14,12 @@
1314
my_logger = Logger.new(STDOUT)
1415
CypressOnRails.configure do |config|
1516
config.cypress_folder = 'my/path'
17+
config.api_prefix = '/api'
1618
config.use_middleware = false
1719
config.logger = my_logger
1820
end
1921
expect(CypressOnRails.configuration.cypress_folder).to eq('my/path')
22+
expect(CypressOnRails.configuration.api_prefix).to eq('/api')
2023
expect(CypressOnRails.configuration.use_middleware?).to eq(false)
2124
expect(CypressOnRails.configuration.logger).to eq(my_logger)
2225
end

0 commit comments

Comments
 (0)