Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): added error handling for undefined api routes #187

Merged
merged 6 commits into from
Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions api/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ class ApplicationController < ActionController::Base
include Response
include ExceptionHandler

def index
render :file => 'public/index.html'
end

def index
render :file => 'public/index.html'
end

def not_found
render_error('unknown route')
end
end
11 changes: 11 additions & 0 deletions api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,19 @@
post '/email_offer', to: 'offers#email_offer'
post '/ta/offers/:offer_id/respond_to_offer', to: 'offers#respond'
end

# This route makes sure that any requests with URLs of the form '/api/*'
# with no corresponding route gets a 404 instead of the frontend index file.
# Redirecting to a specialized 404 route because this route is namespaced and
# cannot call actions from the application controller.
get '*path', to: redirect('/404')
end

# This route returns a 404 error status by throwing a rails routing error.
get '/404', to: 'application#not_found'

# This matches all routes that do not begin with '/api' and returns the frontend
# index file. The request URL is then given to react router.
get '*path', to: "application#index", constraints: ->(request) do
!request.xhr? && request.format.html?
end
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/__tests__/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,15 @@ function reportingTagsTests({ apiGET, apiPOST }) {
// eslint-disable-next-line
function applicationsTests({ apiGET, apiPOST }) {}

function unknownRouteTests(api = { apiGet, apiPost }) {
const { apiGet, apiPost } = api;

it("should fail GET request with unknown '/api' routes", async () => {
const resp = await apiGET("/some_string");
expect(resp).toMatchObject({ status: "error" });
});
}

// Run the actual tests for both the API and the Mock API
describe("API tests", () => {
describe("`/sessions` tests", () => {
Expand Down Expand Up @@ -352,6 +361,9 @@ describe("API tests", () => {
describe("`/applications` tests", () => {
applicationsTests({ apiGET, apiPOST });
});
describe("unknown api route tests", () => {
unknownRouteTests({ apiGET, apiPOST });
});
});

describe("Mock API tests", () => {
Expand Down