Skip to content

Commit

Permalink
Add is_content_type to Request and a couple of helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrtj committed Jun 29, 2024
1 parent f858d52 commit b365532
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Revision history for Kelp
{NEXT}
[New Interface]
- Kelp::Base can now be imported with -attr
- Added is_content_type method to Kelp::Request
- Added a couple of is_* methods to Kelp::Request to match Kelp::Response's interface
* is_text, is_html and is_xml have been added

[Changes]
- Kelp::Base no longer imports namespace::autoclean with -strict
Expand Down
26 changes: 23 additions & 3 deletions lib/Kelp/Request.pm
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,31 @@ sub is_ajax
return $with =~ m{XMLHttpRequest}i;
}

sub is_json
sub is_content_type
{
my $self = shift;
my ($self, $type) = @_;
return 0 unless $self->content_type;
return $self->content_type =~ m{^application/json}i;
return $self->content_type =~ m{^\Q$type\E}i;
}

sub is_text
{
return $_[0]->is_content_type('text/plain');
}

sub is_html
{
return $_[0]->is_content_type('text/html');
}

sub is_json
{
return $_[0]->is_content_type('application/json');
}

sub is_xml
{
return $_[0]->is_content_type('application/xml');
}

sub charset_decode
Expand Down
44 changes: 33 additions & 11 deletions t/request.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ my $t = Kelp::Test->new(app => $app);

# is_json
$app->add_route(
'/json',
'/req_method',
sub {
return $_[0]->req->is_json ? "ok" : "fail";
my $method = $_[0]->req->query_param('m');
return $_[0]->req->$method ? "ok" : "fail";
}
);
for my $ct (
Expand All @@ -23,22 +24,43 @@ for my $ct (
'APPLICATION/JSON; somethin=blah'
)
{
$t->request(GET '/json', Content_Type => $ct)
$t->request(GET '/req_method?m=is_json', Content_Type => $ct)
->code_is(200)
->content_is('ok');
}

# is_ajax
$app->add_route(
'/ajax',
sub {
return $_[0]->req->is_ajax ? "ok" : "fail";
}
);
$t->request(GET '/ajax', 'X-Requested-With' => 'XMLHttpRequest')
$t->request(GET '/req_method?m=is_ajax', 'X-Requested-With' => 'XMLHttpRequest')
->code_is(200)
->content_is('ok');

$t->request(GET '/req_method?m=is_ajax')
->code_is(200)
->content_is('fail');

$t->request(GET '/req_method?m=is_text', Content_Type => 'text/plain')
->code_is(200)
->content_is('ok');

$t->request(GET '/req_method?m=is_text', Content_Type => 'text/html')
->code_is(200)
->content_is('fail');

$t->request(GET '/req_method?m=is_html', Content_Type => 'text/html')
->code_is(200)
->content_is('ok');

$t->request(GET '/req_method?m=is_html', Content_Type => 'text/plain')
->code_is(200)
->content_is('fail');

$t->request(GET '/req_method?m=is_xml', Content_Type => 'application/xml')
->code_is(200)
->content_is('ok');

$t->request(GET '/req_method?m=is_xml', Content_Type => 'application/json')
->code_is(200)
->content_is('fail');

# param
$app->add_route(
'/param/:n',
Expand Down

0 comments on commit b365532

Please sign in to comment.