Debian Codegraph @

lemonldap-ng-common/t/10-PSGI-Request.t

raw
1 use strict;
2 use Test::More;
3 use Plack::Test;
4 use Lemonldap::NG::Common::PSGI::Request;
5
6 use JSON;
7 use HTTP::Request;
8 use HTTP::Message::PSGI;
9
10 # UTF-8 awareness
11 use utf8;
12 my $builder = Test::More->builder;
13 binmode $builder->output, ":encoding(utf8)";
14 binmode $builder->failure_output, ":encoding(utf8)";
15 binmode $builder->todo_output, ":encoding(utf8)";
16
17 sub build_json_request {
18 my ($href) = @_;
19 my $header = [ 'Content-Type' => 'application/json; charset=UTF-8' ];
20 return HTTP::Request->new( 'POST', "http://www.example.com", $header,
21 encode_json($href) );
22 }
23
24 subtest "Request ID" => sub {
25 my $req = Lemonldap::NG::Common::PSGI::Request->new(
26 HTTP::Request->new( GET => 'http://www.example.com/' )->to_psgi );
27
28 my $request_id = $req->request_id;
29 like( $request_id, qr/^\w{16}$/, "Generated request ID" );
30 is( $req->request_id, $request_id, "Request ID is stable across calls" );
31
32 my $req2 = Lemonldap::NG::Common::PSGI::Request->new(
33 HTTP::Request->new( GET => 'http://www.example.com/' )->to_psgi );
34 isnt( $req2->request_id, $request_id,
35 "Request ID is different for each request" );
36
37 my $req3 = Lemonldap::NG::Common::PSGI::Request->new(
38 HTTP::Request->new( GET => 'http://www.example.com/' )
39 ->to_psgi( UNIQUE_ID => 123456 ) );
40
41 is( $req3->request_id, "123456",
42 "Request ID is read from UNIQUE_ID env if set" );
43 };
44
45 subtest "Request JSON body" => sub {
46 my $req = Lemonldap::NG::Common::PSGI::Request->new(
47 build_json_request( { key1 => 123, key2 => "€ncoded" } )->to_psgi );
48
49 ok(1);
50 ok( my $obj = $req->jsonBodyToObj, "Found JSON body" );
51 is( $obj->{key1}, "123", "Found correct ascii value" );
52
53 TODO: {
54 local $TODO = "UTF-8 handling in request is broken, see #2748";
55 is( $obj->{key2}, "€ncoded", "Found correct UTF-8 value" );
56 }
57 };
58
59 done_testing();