Skip to content

Commit

Permalink
document and test new accepts idiom
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 7, 2014
1 parent 54b7670 commit c5c4878
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -138,6 +138,9 @@ if no preference could be detected.
# Check if JSON is acceptable
$self->render(json => {hello => 'world'}) if $self->accepts('json');
# Check if JSON was specifically requested
$self->render(json => {hello => 'world'}) if $self->accepts('', 'json');
# Unsupported representation
$self->render(data => '', status => 204)
unless my $format = $self->accepts('html', 'json');
Expand Down
4 changes: 4 additions & 0 deletions t/mojo/json.t
Expand Up @@ -289,6 +289,10 @@ is $json->encode({false => \!!$bytes}), '{"false":false}',
is $json->encode({false => \$bytes}), '{"false":false}',
'encode false boolean from reference';

# Stringify booleans
is(Mojo::JSON->true, 1, 'right value');
is(Mojo::JSON->false, 0, 'right value');

# Upgraded numbers
my $num = 3;
my $str = "$num";
Expand Down
15 changes: 15 additions & 0 deletions t/mojolicious/restful_lite_app.t
Expand Up @@ -19,6 +19,11 @@ get '/accepts' => sub {
$self->render(json => {best => $self->accepts('html', 'json', 'txt')});
};

get '/wants_json' => sub {
my $self = shift;
$self->render(json => {wants_json => \$self->accepts('', 'json')});
};

under '/rest';

get sub {
Expand Down Expand Up @@ -84,6 +89,16 @@ $t->get_ok('/accepts' => {Accept => 'text/plain'})->status_is(200)
$t->get_ok('/accepts.html?format=json' => {Accept => 'text/plain'})
->status_is(200)->json_is({best => 'txt'});

# Nothing
$t->get_ok('/wants_json')->status_is(200)->json_is({wants_json => 0});

# Unsupported
$t->get_ok('/wants_json.xml')->status_is(200)->json_is({wants_json => 0});

# Accept "json"
$t->get_ok('/wants_json' => {Accept => 'application/json'})->status_is(200)
->json_is({wants_json => 1});

# Ajax
my $ajax = 'text/html;q=0.1,application/json';
$t->get_ok(
Expand Down

0 comments on commit c5c4878

Please sign in to comment.