-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#1975): Allow to use
before/after/rescue_from
methods in any or…
…der when using `mount`
- Loading branch information
Showing
7 changed files
with
305 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Grape::API do | ||
def app | ||
subject | ||
end | ||
|
||
describe 'rescue_from' do | ||
context 'when the API is mounted AFTER defining the class rescue_from handler' do | ||
class APIRescueFrom < Grape::API | ||
rescue_from :all do | ||
error!({ type: 'all' }, 404) | ||
end | ||
|
||
get do | ||
{ count: 1 / 0 } | ||
end | ||
end | ||
|
||
class MainRescueFromAfter < Grape::API | ||
rescue_from ZeroDivisionError do | ||
error!({ type: 'zero' }, 500) | ||
end | ||
|
||
mount APIRescueFrom | ||
end | ||
|
||
subject { MainRescueFromAfter } | ||
|
||
it 'is rescued by the rescue_from ZeroDivisionError handler from Main class' do | ||
get '/' | ||
|
||
expect(last_response.status).to eq(500) | ||
expect(last_response.body).to eq({ type: 'zero' }.to_json) | ||
end | ||
end | ||
|
||
context 'when the API is mounted BEFORE defining the class rescue_from handler' do | ||
class APIRescueFrom < Grape::API | ||
rescue_from :all do | ||
error!({ type: 'all' }, 404) | ||
end | ||
|
||
get do | ||
{ count: 1 / 0 } | ||
end | ||
end | ||
|
||
class MainRescueFromBefore < Grape::API | ||
mount APIRescueFrom | ||
|
||
rescue_from ZeroDivisionError do | ||
error!({ type: 'zero' }, 500) | ||
end | ||
end | ||
|
||
subject { MainRescueFromBefore } | ||
|
||
it 'is rescued by the rescue_from ZeroDivisionError handler from Main class' do | ||
get '/' | ||
|
||
expect(last_response.status).to eq(500) | ||
expect(last_response.body).to eq({ type: 'zero' }.to_json) | ||
end | ||
end | ||
end | ||
|
||
describe 'before' do | ||
context 'when the API is mounted AFTER defining the before helper' do | ||
class APIBeforeHandler < Grape::API | ||
get do | ||
{ count: @count }.to_json | ||
end | ||
end | ||
|
||
class MainBeforeHandlerAfter < Grape::API | ||
before do | ||
@count = 1 | ||
end | ||
|
||
mount APIBeforeHandler | ||
end | ||
|
||
subject { MainBeforeHandlerAfter } | ||
|
||
it 'is able to access the variables defined in the before helper' do | ||
get '/' | ||
|
||
expect(last_response.status).to eq(200) | ||
expect(last_response.body).to eq({ count: 1 }.to_json) | ||
end | ||
end | ||
|
||
context 'when the API is mounted BEFORE defining the before helper' do | ||
class APIBeforeHandler < Grape::API | ||
get do | ||
{ count: @count }.to_json | ||
end | ||
end | ||
|
||
class MainBeforeHandlerBefore < Grape::API | ||
mount APIBeforeHandler | ||
|
||
before do | ||
@count = 1 | ||
end | ||
end | ||
|
||
subject { MainBeforeHandlerBefore } | ||
|
||
it 'is able to access the variables defined in the before helper' do | ||
get '/' | ||
|
||
expect(last_response.status).to eq(200) | ||
expect(last_response.body).to eq({ count: 1 }.to_json) | ||
end | ||
end | ||
end | ||
|
||
describe 'after' do | ||
context 'when the API is mounted AFTER defining the after handler' do | ||
class APIAfterHandler < Grape::API | ||
get do | ||
{ count: 1 }.to_json | ||
end | ||
end | ||
|
||
class MainAfterHandlerAfter < Grape::API | ||
after do | ||
error!({ type: 'after' }, 500) | ||
end | ||
|
||
mount APIAfterHandler | ||
end | ||
|
||
subject { MainAfterHandlerAfter } | ||
|
||
it 'is able to access the variables defined in the after helper' do | ||
get '/' | ||
|
||
expect(last_response.status).to eq(500) | ||
expect(last_response.body).to eq({ type: 'after' }.to_json) | ||
end | ||
end | ||
|
||
context 'when the API is mounted BEFORE defining the after helper' do | ||
class APIAfterHandler < Grape::API | ||
get do | ||
{ count: 1 }.to_json | ||
end | ||
end | ||
|
||
class MainAfterHandlerBefore < Grape::API | ||
mount APIAfterHandler | ||
|
||
after do | ||
error!({ type: 'after' }, 500) | ||
end | ||
end | ||
|
||
subject { MainAfterHandlerBefore } | ||
|
||
it 'is able to access the variables defined in the after helper' do | ||
get '/' | ||
|
||
expect(last_response.status).to eq(500) | ||
expect(last_response.body).to eq({ type: 'after' }.to_json) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Grape::API do | ||
def app | ||
subject | ||
end | ||
|
||
context 'when multiple classes defines the same rescue_from' do | ||
class AnAPI < Grape::API | ||
rescue_from ZeroDivisionError do | ||
error!({ type: 'an-api-zero' }, 404) | ||
end | ||
|
||
get '/an-api' do | ||
{ count: 1 / 0 } | ||
end | ||
end | ||
|
||
class AnotherAPI < Grape::API | ||
rescue_from ZeroDivisionError do | ||
error!({ type: 'another-api-zero' }, 322) | ||
end | ||
|
||
get '/another-api' do | ||
{ count: 1 / 0 } | ||
end | ||
end | ||
|
||
class OtherMain < Grape::API | ||
mount AnAPI | ||
mount AnotherAPI | ||
end | ||
|
||
subject { OtherMain } | ||
|
||
it 'is rescued by the rescue_from ZeroDivisionError handler defined inside each of the classes' do | ||
get '/an-api' | ||
|
||
expect(last_response.status).to eq(404) | ||
expect(last_response.body).to eq({ type: 'an-api-zero' }.to_json) | ||
|
||
get '/another-api' | ||
|
||
expect(last_response.status).to eq(322) | ||
expect(last_response.body).to eq({ type: 'another-api-zero' }.to_json) | ||
end | ||
|
||
context 'when some class does not define a rescue_from but it was defined in a previous mounted endpoint' do | ||
class AnAPIWithoutDefinedRescueFrom < Grape::API | ||
get '/another-api-without-defined-rescue-from' do | ||
{ count: 1 / 0 } | ||
end | ||
end | ||
|
||
class OtherMainWithNotDefinedRescueFrom < Grape::API | ||
mount AnAPI | ||
mount AnotherAPI | ||
mount AnAPIWithoutDefinedRescueFrom | ||
end | ||
|
||
subject { OtherMainWithNotDefinedRescueFrom } | ||
|
||
it 'is not rescued by any of the previous defined rescue_from ZeroDivisionError handlers' do | ||
get '/an-api' | ||
|
||
expect(last_response.status).to eq(404) | ||
expect(last_response.body).to eq({ type: 'an-api-zero' }.to_json) | ||
|
||
get '/another-api' | ||
|
||
expect(last_response.status).to eq(322) | ||
expect(last_response.body).to eq({ type: 'another-api-zero' }.to_json) | ||
|
||
expect do | ||
get '/another-api-without-defined-rescue-from' | ||
end.to raise_error(ZeroDivisionError) | ||
end | ||
end | ||
end | ||
end |