Skip to content

Commit

Permalink
Merge branch 'content-length-in-bytes' into devel
Browse files Browse the repository at this point in the history
Fixes #980
  • Loading branch information
yanick committed Mar 20, 2014
2 parents 363467e + e51be92 commit 63b78bb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/Dancer/Handler.pm
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ sub render_response {
$response->header( 'Content-Type' => "$ctype; charset=$charset" )
if $ctype !~ /$charset/;
}
$response->header( 'Content-Length' => length($content) )
if !defined $response->header('Content-Length');
if (!defined $response->header('Content-Length')) {
use bytes; # turn off character semantics
$response->header( 'Content-Length' => length($content) );
}
$content = [$content];
}
else {
Expand Down
65 changes: 64 additions & 1 deletion t/12_response/04_charset_server.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plan skip_all => "HTTP::Request::Common is needed for this test"

use LWP::UserAgent;

plan tests => 6;
plan tests => 10;

Test::TCP::test_tcp(
client => sub {
Expand Down Expand Up @@ -54,3 +54,66 @@ Test::TCP::test_tcp(
Dancer->dance();
},
);

Test::TCP::test_tcp(
client => sub {
my $port = shift;
my $ua = LWP::UserAgent->new;

my $req = HTTP::Request::Common::GET(
"http://127.0.0.1:$port/unicode-content-length");
my $res = $ua->request($req);

is $res->content_type, 'text/html';
# UTF-8 seems to be Dancer's default encoding
my $v = "\x{100}0123456789";
utf8::encode($v);
is $res->content, $v;
},
server => sub {
my $port = shift;

use lib "t/lib";
use TestAppUnicode;
Dancer::Config->load;

set(
# no charset
environment => 'production',
port => $port,
startup_info => 0,
);
Dancer->dance;
},
);


Test::TCP::test_tcp(
client => sub {
my $port = shift;
my $ua = LWP::UserAgent->new;

my $req = HTTP::Request::Common::GET(
"http://127.0.0.1:$port/unicode-content-length-json");
my $res = $ua->request($req);

is $res->content_type, 'application/json';
is_deeply(from_json($res->content), { test => "\x{100}" });
},
server => sub {
my $port = shift;

use lib "t/lib";
use TestAppUnicode;
Dancer::Config->load;

set(
# no charset
environment => 'production',
port => $port,
startup_info => 0,
serializer => 'JSON',
);
Dancer->dance;
},
);
8 changes: 8 additions & 0 deletions t/lib/TestAppUnicode.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,13 @@ get '/form' => sub {
})."\x{E9} - string1: ".params->{'string1'}
};

get '/unicode-content-length' => sub {
"\x{100}0123456789";
};

get '/unicode-content-length-json' => sub {
{ test => "\x{100}" };
};


1;

0 comments on commit 63b78bb

Please sign in to comment.