workarea-commerce / workarea-api

Adds JSON REST APIs to the Workarea Commerce Platform
https://www.workarea.com
Other
7 stars 1 forks source link

Document Integration Testing Using Subdomain Constraints #8

Closed tubbo closed 4 years ago

tubbo commented 4 years ago

The README describes how to set up an API using a subdomain constraint, so the API would be available at api.yourdomain.com. However, this causes all integration tests in the API to fail because the subdomain is not being used when looking up routes to send requests to. To remedy this, a note has been added to the README explaining how to configure Workarea::IntegrationTest so that subdomains can be used in the API without integration tests suddenly failing.

More Info: https://discourse.workarea.com/t/build-failing-workarea-api-plugin/1652

tubbo commented 4 years ago

While doing this ticket I realized what a gotcha this can be...so I got an idea based on how Devise does their routing to create a DSL method in the Rails route mapper class to define API routes, like this:

Rails.application.routes.draw do
  mount_workarea_api to: '/api'
end

This would give us the information needed to construct API routes in testing, without the user having to override Workarea::IntegrationTest or create their own Rails routing constraint class.

If routes were mounted like this:

Rails.application.routes.draw do
  mount_workarea_api subdomain: 'api'
end

The routing constraint parameters would be saved in Workarea::Api.routing_constraints, and then subsequently used in tests that have Workarea::Api::IntegrationTest included, which would be all the integration tests in API.

Note that this is a totally backwards-compatible approach...current users of the API will not need to change anything, it's just a neater way of doing the same thing without having to know so much about the Rails routing DSL.

bencrouse commented 4 years ago

Nice, that would be a good enhancement.