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
=item
the B<accept> from the request headers
=item
The default is B<application/json>
=back
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::Dumper | text/x-data-dumper
Dancer2::Serializer::JSON | text/x-json, application/json
A different mapping can be provided via the config file. For example,
the default mapping would be configured as
lib/Dancer2/Serializer/Mutable.pm view on Meta::CPAN
engines:
serializer:
Mutable:
mapping:
'text/x-yaml' : YAML
'text/html' : YAML
'text/x-data-dumper' : Dumper
'text/x-json' : JSON
'application/json' : JSON
The keys of the mapping are the content-types to serialize,
and the values the serializers to use. Serialization for C<YAML>, C<Dumper>
and C<JSON> are done using internal Dancer mechanisms. Any other serializer will
be taken to be as Dancer2 serialization class (minus the C<Dancer2::Serializer::> prefix)
and an instance of it will be used
to serialize/deserialize data. For example, adding L<Dancer2::Serializer::XML>
to the mapping would be:
engines:
serializer:
Mutable:
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, Dumper, 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, Dumper, 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',
);
}
};
my $serializer = Dancer2::Serializer::Dumper->new();
is(
$serializer->content_type,
'text/x-data-dumper',
'content-type is set correctly',
);
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,
'text/x-data-dumper',
"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",
},
dumper => {
types => [ qw(text/x-data-dumper) ],
value => Data::Dumper::Dumper({ bar => 'baz' }),
last_val => "\$VAR1={'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;