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
Stabilize routing #22
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
use Plack::Test; | ||
use HTTP::Request::Common qw(GET POST PUT DELETE); | ||
|
||
{ | ||
package SampleApp; | ||
use Kossy; | ||
|
||
get '/' => sub { $_[1]->halt_text(200, 'GET /') }; | ||
get '/user/me' => sub { $_[1]->halt_text(200, 'GET /user/me') }; | ||
get '/user/:name' => sub { $_[1]->halt_text(200, 'GET /user/:name') }; | ||
get '/user/:name/likes' => sub { $_[1]->halt_text(200, 'GET /user/:name/likes') }; | ||
post '/user/:name/likes' => sub { $_[1]->halt_text(200, 'POST /user/:name/likes') }; | ||
router 'DELETE', '/user/:name/likes' => sub { $_[1]->halt_text(200, 'DELETE /user/:name/likes') }; | ||
|
||
get '/entry/:id' => sub { $_[1]->halt_text(200, 'GET /entry/:id') }; | ||
router 'PUT', '/entry/:id' => sub { $_[1]->halt_text(200, 'PUT /entry/:id') }; | ||
get '/entry/foo' => sub { $_[1]->halt_text(200, 'GET /entry/foo') }; | ||
|
||
get '/org/{org_id:[0-9]+}' => sub { $_[1]->halt_text(200, 'GET /org/{org_id}') }; | ||
get '/org/foo' => sub { $_[1]->halt_text(200, 'GET /org/foo') }; | ||
} | ||
|
||
my $app = SampleApp->psgi; | ||
|
||
test_psgi $app, sub { | ||
my $cb = shift; | ||
|
||
is $cb->(GET "/")->content, 'GET /'; | ||
is $cb->(GET "/user/me")->content, 'GET /user/me'; | ||
is $cb->(GET "/user/foo")->content, 'GET /user/:name'; | ||
is $cb->(GET "/user/foo/likes")->content, 'GET /user/:name/likes'; | ||
is $cb->(POST "/user/foo/likes")->content, 'POST /user/:name/likes'; | ||
is $cb->(DELETE "/user/foo/likes")->content, 'DELETE /user/:name/likes'; | ||
|
||
is $cb->(GET "/entry/bar")->content, 'GET /entry/:id'; | ||
is $cb->(PUT "/entry/bar")->content, 'PUT /entry/:id'; | ||
is $cb->(GET "/entry/foo")->content, 'GET /entry/:id', 'Not match /entry/foo'; | ||
|
||
is $cb->(GET "/org/123")->content, 'GET /org/{org_id}'; | ||
is $cb->(GET "/org/foo")->content, 'GET /org/foo'; | ||
is $cb->(GET "/org/bar")->code, 404, 'Not match since `bar` is not number'; | ||
|
||
is $cb->(GET "/not_found")->code, 404, 'Not found path'; | ||
is $cb->(POST "/")->code, 405, 'Method not allowed'; | ||
}; | ||
|
||
done_testing; |
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,30 @@ | ||
use strict; | ||
use warnings; | ||
|
||
use Test::More; | ||
use Kossy; | ||
use Plack::Test; | ||
use HTTP::Request::Common; | ||
|
||
get '/api/me' => sub { | ||
my ($self, $c) = @_; | ||
$c->halt_text(200, '/api/me'); | ||
}; | ||
|
||
get '/api/:name' => sub { | ||
my ($self, $c) = @_; | ||
$c->halt_text(200, '/api/:name'); | ||
}; | ||
|
||
my $app = __PACKAGE__->psgi; | ||
|
||
test_psgi $app, sub { | ||
my $cb = shift; | ||
|
||
# Matching routing was unstable due to random Hash keys. | ||
# This change gives priority to the previously defined routing | ||
my $res = $cb->(GET "/api/me"); | ||
is $res->content, '/api/me'; | ||
}; | ||
|
||
done_testing; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The order of
keys %{$self->_router}
is random