Skip to content

Commit

Permalink
added GET/POST parameter support to respond_to
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 21, 2011
1 parent fb485ef commit 7d45f02
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 7 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@ This file documents the revision history for Perl extension Mojolicious.

1.86 2011-08-21 00:00:00
- Added EXPERIMENTAL support for testing WebSockets with Test::Mojo.
- Added GET/POST parameter support to respond_to.
- Improved documentation.

1.85 2011-08-20 00:00:00
Expand Down
6 changes: 4 additions & 2 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -486,7 +486,9 @@ sub respond_to {
push @formats, @{$app->types->detect($self->req->headers->accept)};
my $stash = $self->stash;
unless (@formats) {
if (my $format = $stash->{format}) { push @formats, $format }
if (my $format = $stash->{format} || $self->req->param('format')) {
push @formats, $format;
}
else { push @formats, $app->renderer->default_format }
}

Expand Down Expand Up @@ -962,7 +964,7 @@ Usually refers to a L<Mojo::Message::Response> object.
);
Automatically select best possible representation for resource from C<Accept>
request header and route C<format>.
request header, C<format> stash value or C<format> GET/POST parameter.
Note that this method is EXPERIMENTAL and might change without warning!
$c->respond_to(
Expand Down
3 changes: 2 additions & 1 deletion lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -269,7 +269,8 @@ C<render>.
);

The best possible representation will be automatically selected from the
C<Accept> request header and route C<format>.
C<Accept> request header, C<format> stash value or C<format> GET/POST
parameter.

$self->respond_to(
json => {json => {hello => 'world'}},
Expand Down
2 changes: 2 additions & 0 deletions lib/Mojolicious/Lite.pm
Expand Up @@ -482,6 +482,8 @@ C<RESTful> content negotiation you can also use C<respond_to>.
# /hello (Accept: text/xml)
# /hello.json
# /hello.xml
# /hello?format=json
# /hello?format=xml
get '/hello' => sub {
my $self = shift;
$self->respond_to(
Expand Down
185 changes: 181 additions & 4 deletions t/mojolicious/restful_lite_app.t
Expand Up @@ -7,7 +7,7 @@ BEGIN {
$ENV{MOJO_IOWATCHER} = 'Mojo::IOWatcher';
}

use Test::More tests => 165;
use Test::More tests => 317;

# "Woohoo, time to go clubbin'! Baby seals here I come!"
use Mojolicious::Lite;
Expand Down Expand Up @@ -75,6 +75,26 @@ $t->get_ok('/rest', {Accept => 'text/html;q=9'})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works');

# GET /rest (html query)
$t->get_ok('/rest?format=html')->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works');

# GET /rest (html format with query)
$t->get_ok('/rest.html?format=html')->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works');

# GET /rest (accept html with query)
$t->get_ok('/rest?format=html', {Accept => 'text/html'})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works');

# GET /rest (accept html with everything)
$t->get_ok('/rest.html?format=html', {Accept => 'text/html'})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works');

# GET /rest.json (json format)
$t->get_ok('/rest.json')->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works'});
Expand All @@ -99,6 +119,24 @@ $t->get_ok('/rest.png', {Accept => 'application/json'})->status_is(200)
$t->get_ok('/rest', {Accept => 'application/json;q=9'})->status_is(200)
->content_type_is('application/json')->json_content_is({just => 'works'});

# GET /rest (json query)
$t->get_ok('/rest?format=json')->status_is(200)
->content_type_is('application/json')->json_content_is({just => 'works'});

# GET /rest (json format with query)
$t->get_ok('/rest.json?format=json')->status_is(200)
->content_type_is('application/json')->json_content_is({just => 'works'});

# GET /rest (accept json with query)
$t->get_ok('/rest?format=json', {Accept => 'application/json'})
->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works'});

# GET /rest (accept json with everything)
$t->get_ok('/rest.json?format=json', {Accept => 'application/json'})
->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works'});

# GET /rest.xml (xml format)
$t->get_ok('/rest.xml')->status_is(200)->content_type_is('text/xml')
->text_is(just => 'works');
Expand All @@ -123,7 +161,23 @@ $t->get_ok('/rest.txt', {Accept => 'text/xml'})->status_is(200)
$t->get_ok('/rest', {Accept => 'text/xml;q=9'})->status_is(200)
->content_type_is('text/xml')->text_is(just => 'works');

# GET /rest (unsupported)
# GET /rest (xml query)
$t->get_ok('/rest?format=xml')->status_is(200)->content_type_is('text/xml')
->text_is(just => 'works');

# GET /rest (xml format with query)
$t->get_ok('/rest.xml?format=xml')->status_is(200)
->content_type_is('text/xml')->text_is(just => 'works');

# GET /rest (accept json with query)
$t->get_ok('/rest?format=xml', {Accept => 'text/xml'})->status_is(200)
->content_type_is('text/xml')->text_is(just => 'works');

# GET /rest (accept json with everything)
$t->get_ok('/rest.xml?format=xml', {Accept => 'text/xml'})->status_is(200)
->content_type_is('text/xml')->text_is(just => 'works');

# GET /rest (unsupported accept)
$t->get_ok('/rest', {Accept => 'image/png'})->status_is(204)->content_is('');

# POST /rest
Expand Down Expand Up @@ -161,6 +215,47 @@ $t->post_ok('/rest', {Accept => 'text/html;q=9'})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works too');

# POST /rest (html query)
$t->post_ok('/rest?format=html')->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works too');

# POST /rest (html format with query)
$t->post_ok('/rest.html?format=html')->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works too');

# POST /rest (accept html with query)
$t->post_ok('/rest?format=html', {Accept => 'text/html'})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works too');

# POST /rest (accept html with everything)
$t->post_ok('/rest.html?format=html', {Accept => 'text/html'})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works too');

# POST /rest (html form)
$t->post_form_ok('/rest' => {format => 'html'})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works too');

# POST /rest (html format with form)
$t->post_form_ok('/rest.html' => {format => 'html'})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works too');

# POST /rest (accept html with form)
$t->post_form_ok('/rest' => {format => 'html'} => {Accept => 'text/html'})
->status_is(200)->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works too');

# POST /rest (accept html with everything, form alternative)
$t->post_form_ok(
'/rest.html' => {format => 'html'} => {Accept => 'text/html'})
->status_is(200)->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works too');

# POST /rest.json (json format)
$t->post_ok('/rest.json')->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works too'});
Expand Down Expand Up @@ -190,6 +285,48 @@ $t->post_ok('/rest', {Accept => 'application/json;q=9'})->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});

# POST /rest (json query)
$t->post_ok('/rest?format=json')->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});

# POST /rest (json format with query)
$t->post_ok('/rest.json?format=json')->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});

# POST /rest (accept json with query)
$t->post_ok('/rest?format=json', {Accept => 'application/json'})
->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works too'});

# POST /rest (accept json with everything)
$t->post_ok('/rest.json?format=json', {Accept => 'application/json'})
->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works too'});

# POST /rest (json form)
$t->post_form_ok('/rest' => {format => 'json'})->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});

# POST /rest (json format with form)
$t->post_form_ok('/rest.json' => {format => 'json'})->status_is(200)
->content_type_is('application/json')
->json_content_is({just => 'works too'});

# POST /rest (accept json with form)
$t->post_form_ok(
'/rest' => {format => 'json'} => {Accept => 'application/json'})
->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works too'});

# POST /rest (accept json with everything, form alternative)
$t->post_form_ok(
'/rest.json' => {format => 'json'} => {Accept => 'application/json'})
->status_is(200)->content_type_is('application/json')
->json_content_is({just => 'works too'});

# POST /rest.xml (xml format)
$t->post_ok('/rest.xml')->status_is(200)->content_type_is('text/xml')
->text_is(just => 'works too');
Expand All @@ -214,13 +351,53 @@ $t->post_ok('/rest.txt', {Accept => 'text/xml'})->status_is(200)
$t->post_ok('/rest', {Accept => 'text/xml;q=9'})->status_is(200)
->content_type_is('text/xml')->text_is(just => 'works too');

# POST /rest (unsupported)
# POST /rest (xml query)
$t->post_ok('/rest?format=xml')->status_is(200)->content_type_is('text/xml')
->text_is(just => 'works too');

# POST /rest (xml format with query)
$t->post_ok('/rest.xml?format=xml')->status_is(200)
->content_type_is('text/xml')->text_is(just => 'works too');

# POST /rest (accept json with query)
$t->post_ok('/rest?format=xml', {Accept => 'text/xml'})->status_is(200)
->content_type_is('text/xml')->text_is(just => 'works too');

# POST /rest (accept json with everything)
$t->post_ok('/rest.xml?format=xml', {Accept => 'text/xml'})->status_is(200)
->content_type_is('text/xml')->text_is(just => 'works too');

# POST /rest (xml form)
$t->post_form_ok('/rest' => {format => 'xml'})->status_is(200)
->content_type_is('text/xml')->text_is(just => 'works too');

# POST /rest (xml format with form)
$t->post_form_ok('/rest.xml' => {format => 'xml'})->status_is(200)
->content_type_is('text/xml')->text_is(just => 'works too');

# POST /rest (accept json with form)
$t->post_form_ok('/rest' => {format => 'xml'} => {Accept => 'text/xml'})
->status_is(200)->content_type_is('text/xml')->text_is(just => 'works too');

# POST /rest (accept json with everything, form alternative)
$t->post_form_ok('/rest.xml' => {format => 'xml'} => {Accept => 'text/xml'})
->status_is(200)->content_type_is('text/xml')->text_is(just => 'works too');

# POST /rest (unsupported accept)
$t->post_ok('/rest', {Accept => 'image/png'})->status_is(201)
->content_type_is('text/html;charset=UTF-8')->content_is('works too');

# POST /rest.png (unsupported)
# POST /rest (unsupported accept with supported query)
$t->post_ok('/rest?format=json', {Accept => 'image/png'})->status_is(201)
->content_type_is('text/html;charset=UTF-8')->content_is('works too');

# POST /rest.png (unsupported format)
$t->post_ok('/rest.png')->status_is(201)
->content_type_is('text/html;charset=UTF-8')->content_is('works too');

# POST /rest.png (unsupported format with supported query)
$t->post_ok('/rest.png?format=json')->status_is(201)
->content_type_is('text/html;charset=UTF-8')->content_is('works too');

# GET /nothing (does not exist)
$t->get_ok('/nothing', {Accept => 'image/png'})->status_is(404);

0 comments on commit 7d45f02

Please sign in to comment.