Dancer2
view release on metacpan or search on metacpan
lib/Dancer2/Core/Response.pm view on Meta::CPAN
$response->error( message => "oops" );
Creates a L<Dancer2::Core::Error> object with the given I<@args> and I<throw()>
it against the response object. Returns the error object.
=head2 serialize( $content )
$response->serialize( $content );
Serialize and return $content with the response's serializer.
set content-type accordingly.
=head2 header($name)
Return the value of the given header, if present. If the header has multiple
values, returns the list of values if called in list context, the first one
if in scalar context.
=head2 push_header
Add the header no matter if it already exists or not.
lib/Dancer2/Serializer/Mutable.pm view on Meta::CPAN
In both directions, a header's value is matched against the mapping on its
content type alone: any C<;>-separated parameters (such as
C<; charset=utf-8>) are stripped, surrounding whitespace is trimmed, and the
result is lowercased before comparison. So C<Content-Type: text/x-yaml>,
C<Content-Type: text/x-yaml; charset=utf-8> and C<Content-Type: TEXT/X-YAML>
are all recognised as C<text/x-yaml>. An C<Accept> header listing several
comma-separated types is not split further - it is matched as a whole after
parameter-stripping, so only a single-type C<Accept> value is recognised;
anything else falls through to the next header or the default.
The content-type/serializer mapping that C<Dancer2::Serializer::Mutable>
uses is
serializer | content types
----------------------------------------------------------
Dancer2::Serializer::YAML | text/x-yaml, text/html
Dancer2::Serializer::JSON | text/x-json, application/json
The keys above are bare, lowercase content types - not raw header values - as
described above.
lib/Dancer2/Serializer/Mutable.pm view on Meta::CPAN
=head2 serialize
Serialize a data structure. The format it is serialized to is determined
automatically as described above. It can be one of YAML, JSON, defaulting
to JSON if there's no clear preference from the request.
=head2 deserialize
Deserialize the provided serialized data to a data structure. The type of
serialization format depends on the request's content-type. For now, it can
be one of YAML, JSON.
=head2 content_type
Returns the content-type that was used during the last C<serialize> /
C<deserialize> call. B<WARNING> : you must call C<serialize> / C<deserialize>
before calling C<content_type>. Otherwise the return value will be C<undef>.
=head1 AUTHOR
Dancer Core Developers
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2026 by Alexis Sukrieh.
lib/Dancer2/Test.pm view on Meta::CPAN
);
## keep temp_fh in scope so it doesn't get deleted too early
## But will get deleted by the time the test is finished.
$upload->{temp_fh} = $temp;
$request->uploads->{ $file->{name} } = $upload;
}
}
# content-type
if ( $options->{content_type} ) {
$request->content_type( $options->{content_type} );
}
return ( $request, $env );
}
sub _build_env_from_request {
my ($request) = @_;
t/dsl/send_as.t view on Meta::CPAN
};
subtest "send_as json object" => sub {
my $res = $test->request( GET '/json-object' );
is $res->code, '200';
is $res->content_type, 'application/json';
is $res->content, '{"data":{"foo":"bar"}}';
};
subtest "send_as json custom content-type" => sub {
my $res = $test->request( GET '/json-utf8/is/wonderful' );
is $res->code, '200';
is $res->content_type, 'application/json';
is $res->content_type_charset, 'UTF-8';
is $res->content, '["is","wonderful"]';
};
subtest "send_as html" => sub {
my $res = $test->request( GET '/html' );
t/redirect.t view on Meta::CPAN
{
my $res = $cb->( GET '/' );
is( $res->code, 200, '[GET /] Correct code' );
is( $res->content, 'home', '[GET /] Correct content' );
is(
$res->headers->content_type,
'text/html',
'[GET /] Correct content-type',
);
is(
$cb->( GET '/bounce' )->code,
302,
'[GET /bounce] Correct code',
);
}
{
t/serializer.t view on Meta::CPAN
my $cb = shift;
{
# Response with implicit call to the serializer
my $res = $cb->( GET '/json' );
is( $res->code, 200, '[/json] Correct status' );
is( $res->content, '{"bar":"baz"}', '[/json] Correct content' );
is(
$res->headers->content_type,
'application/json',
'[/json] Correct content-type headers',
);
}
};
t/serializer_mutable.t view on Meta::CPAN
}
my $test = Plack::Test->create( MyApp->to_app );
subtest "serializer returns to default state" => sub {
my $res = $test->request( GET '/serialize' );
is(
$res->headers->content_type,
'application/json',
"Default content-type header",
);
$res = $test->request( GET '/serialize', 'Accept' => 'text/x-data-dumper' );
is(
$res->headers->content_type,
'application/json',
"Dumper content type falls back to default as it is not enabled",
);
$res = $test->request( GET '/serialize', 'Accept' => 'text/x-json' );
is(
$res->headers->content_type,
'text/x-json',
"Correct content-type header",
);
$res = $test->request( GET '/serialize' );
is(
$res->headers->content_type,
'application/json',
"Correct default content-type header after a request that used another",
);
};
# Configure test content-type cases
my $d = {
yaml => {
types => [ qw(text/x-yaml text/html) ],
value => encode('UTF-8', YAML::Dump({ bar => 'baz' })),
last_val => "---bar:baz",
},
json => {
types => [ qw(text/x-json application/json) ],
value => JSON::MaybeXS::encode_json({ bar => 'baz' }),
last_val => '{"bar":"baz"}',
t/serializer_mutable.t view on Meta::CPAN
for my $ct (qw/Content-Type Accept/) {
# Test getting the value serialized in the correct format
my $res = $test->request( GET '/serialize', $ct => $content_type );
is( $res->code, 200, "[/$format] Correct status" );
is( $res->content, $s->{value}, "[/$format] Correct content" );
is(
$res->headers->content_type,
$s->{return_content_type} || $content_type,
"[/$format] Correct content-type headers",
);
}
# Test sending the value serialized in the correct format
# needs to be de-serialized and returned
my $req = $test->request( POST '/deserialize',
'Content-Type' => $content_type,
content => $s->{value} );
my $content = $req->content;
t/serializer_mutable_custom.t view on Meta::CPAN
for my $ct (qw/Content-Type Accept/) {
# Test getting the value serialized in the correct format
my $res = $cb->( GET '/serialize', $ct => $content_type );
is( $res->code, 200, "status" );
is( $res->content, $s->{value}, "content" );
is(
$res->headers->content_type,
$content_type,
"content-type headers",
);
}
# Test sending the value serialized in the correct format
# needs to be de-serialized and returned
my $req = $cb->( POST '/deserialize',
'Content-Type' => $content_type,
content => $s->{value} );
my $content = $req->content;
( run in 3.037 seconds using v1.01-cache-2.11-cpan-99fa8243a42 )