Skip to content
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

Feature/request response headers #1085

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/Dancer2/Core/DSL.pm
Expand Up @@ -89,10 +89,14 @@ sub dsl_keywords {
prefix => { is_global => 1 },
psgi_app => { is_global => 1 },
push_header => { is_global => 0 },
push_response_header => { is_global => 0 },
put => { is_global => 1 },
redirect => { is_global => 0 },
request => { is_global => 0 },
request_header => { is_global => 0 },
response => { is_global => 0 },
response_header => { is_global => 0 },
response_headers => { is_global => 0 },
runner => { is_global => 1 },
send_error => { is_global => 0 },
send_file => { is_global => 0 },
Expand Down Expand Up @@ -265,16 +269,34 @@ sub status {
}

sub push_header {
# TODO: deprecate old keyword after we have a period of stability
# carp "DEPRECATED: please use the 'push_response_header' keyword instead of 'push_header'";
goto &push_response_header;
}

sub push_response_header {
shift;
$Dancer2::Core::Route::RESPONSE->push_header(@_);
}

sub header {
# TODO: deprecate keyword after a period of stability
# carp "DEPRECATED: please use the 'response_header' keyword instead of 'header'";
goto &response_header;
}

sub response_header {
shift;
$Dancer2::Core::Route::RESPONSE->header(@_);
}

sub headers {
# TODO: deprecate keyword after a period of stability
# carp "DEPRECATED: please use the 'response_headers' keyword instead of 'headers'";
goto &response_headers;
}

sub response_headers {
shift;
$Dancer2::Core::Route::RESPONSE->header(@_);
}
Expand Down Expand Up @@ -377,6 +399,8 @@ sub context {

sub request { $Dancer2::Core::Route::REQUEST }

sub request_header { shift; $Dancer2::Core::Route::REQUEST->headers->header(@_) }

sub response { $Dancer2::Core::Route::RESPONSE }

sub upload { shift; $Dancer2::Core::Route::REQUEST->upload(@_); }
Expand Down
36 changes: 23 additions & 13 deletions lib/Dancer2/Manual.pod
Expand Up @@ -332,7 +332,7 @@ set the content all at once, if you prefer.

get '/status' => sub {
delayed {
add_header 'X-Foo' => 'Bar';
response_header 'X-Foo' => 'Bar';

# flush headers (in case of streaming)
flush;
Expand Down Expand Up @@ -2529,33 +2529,34 @@ B<WARNING:> Issuing a halt immediately exits the current route, and performs
the halt. Thus, any code after a halt is ignored, until the end of the route.
Hence, it's not necessary anymore to use C<return> with halt.

=head2 headers
=head2 response_headers

Adds custom headers to responses:
Adds custom headers to response:

get '/send/headers', sub {
headers 'X-Foo' => 'bar', 'X-Bar' => 'foo';
response_headers 'X-Foo' => 'bar', 'X-Bar' => 'foo';
}

=head2 header
=head2 response_header

adds a custom header to response:
Adds a custom header to response:

get '/send/header', sub {
header 'x-my-header' => 'shazam!';
response_header 'x-my-header' => 'shazam!';
}

Note that it will overwrite the old value of the header, if any. To avoid
that, see L</push_header>.
that, see L</push_response_header>.

=head2 push_header
=head2 push_response_header

Do the same as C<header>, but allow for multiple headers with the same name.
Do the same as C<response_header>, but allow for multiple headers with the same
name.

get '/send/header', sub {
push_header 'x-my-header' => '1';
push_header 'x-my-header' => '2';
will result in two headers "x-my-header" in the response
push_response_header 'x-my-header' => '1';
push_response_header 'x-my-header' => '2';
# will result in two headers "x-my-header" in the response
}

=head2 hook
Expand Down Expand Up @@ -2810,6 +2811,15 @@ call, for example:
request->remote_address; # user's IP address
request->user_agent; # User-Agent header value

=head2 request_header

Returns request header(s).

get '/get/headers' => sub {
my $xfoo = request_header 'X-Foo';
...
};

=head2 send_error

Returns a HTTP error. By default the HTTP code returned is 500:
Expand Down
22 changes: 0 additions & 22 deletions t/dsl.t

This file was deleted.

2 changes: 1 addition & 1 deletion t/dsl/halt.t
Expand Up @@ -12,7 +12,7 @@ subtest 'halt within routes' => sub {

get '/' => sub { 'hello' };
get '/halt' => sub {
header 'X-Foo' => 'foo';
response_header 'X-Foo' => 'foo';
halt;
};
get '/shortcircuit' => sub {
Expand Down
2 changes: 1 addition & 1 deletion t/dsl/halt_with_param.t
Expand Up @@ -12,7 +12,7 @@ subtest 'halt with parameter within routes' => sub {

get '/' => sub { 'hello' };
get '/halt' => sub {
header 'X-Foo' => 'foo';
response_header 'X-Foo' => 'foo';
halt;
};
get '/shortcircuit' => sub {
Expand Down
2 changes: 1 addition & 1 deletion t/dsl/pass.t
Expand Up @@ -12,7 +12,7 @@ subtest 'pass within routes' => sub {

get '/' => sub { 'hello' };
get '/**' => sub {
header 'X-Pass' => 'pass';
response_header 'X-Pass' => 'pass';
pass;
redirect '/'; # won't get executed as pass returns immediately.
};
Expand Down
42 changes: 42 additions & 0 deletions t/dsl/request.t
@@ -0,0 +1,42 @@
use strict;
use warnings;
use Test::More;
use Plack::Test;
use HTTP::Request::Common;

{
package App::DSL::Request;
use Dancer2;

any [ 'get', 'post' ], '/' => sub {
request->method;
};

get 'headers' => sub {
request_header 'X-Foo';
};
}

subtest 'Testing an app with request keyword' => sub {
my $test = Plack::Test->create( App::DSL::Request->to_app );
{
my $res = $test->request( GET '/' );
ok( $res->is_success, 'Successful GET request' );
is( $res->content, 'GET', 'GET / correct content' );
}
{
my $res = $test->request( POST '/' );
ok( $res->is_success, 'Successful POST request' );
is( $res->content, 'POST', 'POST / correct content' );
}
};

subtest 'Testing app with request_header heyword' => sub {
my $test = Plack::Test->create( App::DSL::Request->to_app );
my $res = $test->request( GET '/headers', 'X-Foo' => 'Bar' );
ok( $res->is_success, 'Successful GET request' );
is( $res->content, 'Bar', 'GET /headers correct content' );
};

done_testing;

10 changes: 5 additions & 5 deletions t/lib/TestApp.pm
Expand Up @@ -72,21 +72,21 @@ post '/dirname' => sub {

# header
get '/header/:name/:value' => sub {
header param('name') => param('value');
response_header param('name') => param('value');
1;
};

# push_header
get '/header/:name/:valueA/:valueB' => sub {
push_header param('name') => param('valueA');
push_header param('name') => param('valueB');
push_response_header param('name') => param('valueA');
push_response_header param('name') => param('valueB');
1;
};

# header
get '/header_twice/:name/:valueA/:valueB' => sub {
header param('name') => param('valueA');
header param('name') => param('valueB');
response_header param('name') => param('valueA');
response_header param('name') => param('valueB');
1;
};

Expand Down
2 changes: 1 addition & 1 deletion t/redirect.t
Expand Up @@ -12,7 +12,7 @@ subtest 'basic redirects' => sub {

get '/' => sub {'home'};
get '/bounce' => sub { redirect '/' };
get '/redirect' => sub { header 'X-Foo' => 'foo'; redirect '/'; };
get '/redirect' => sub { response_header 'X-Foo' => 'foo'; redirect '/'; };
get '/redirect_querystring' => sub { redirect '/login?failed=1' };
}

Expand Down