| File: | t/11-HTTPTiny2h2o.t |
| Coverage: | 100.0% |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 1 | # basically how Util::H2O::h2o does it | ||||||
| 2 | 1 1 1 | 3664 2 24 | use strict; | ||||
| 3 | 1 1 1 | 5 1 36 | use warnings; | ||||
| 4 | |||||||
| 5 | 1 1 1 | 348 67948 5 | use Test::More q//; | ||||
| 6 | 1 1 1 | 419 2206 2 | use Test::Exception; | ||||
| 7 | 1 1 1 | 154 1 50 | use JSON::MaybeXS; | ||||
| 8 | 1 1 1 | 274 2 1277 | use Util::H2O::More qw/ddd HTTPTiny2h2o o2d/; | ||||
| 9 | |||||||
| 10 | 1 | 73503 | my $AoH = [ { four => 4, five => 5, six => 6 }, { seven => 7, eight => 8, nine => 9 }, ]; | ||||
| 11 | |||||||
| 12 | 1 | 17 | my $HTTPTiny_response1 = { | ||||
| 13 | status => 200, | ||||||
| 14 | content => encode_json($AoH), | ||||||
| 15 | }; | ||||||
| 16 | |||||||
| 17 | 1 | 3 | HTTPTiny2h2o $HTTPTiny_response1; | ||||
| 18 | |||||||
| 19 | 1 | 2 | is_deeply o2d($HTTPTiny_response1->content), $AoH, q{Arrays of hashes encoded as JSON was decoded properly}; | ||||
| 20 | |||||||
| 21 | 1 | 903 | my $HoAoH = { | ||||
| 22 | one => [qw/1 2 3 4 5/], | ||||||
| 23 | two => [qw/6 7 8 9 0/], | ||||||
| 24 | three => [ { four => 4, five => 5, six => 6 }, { seven => 7, eight => 8, nine => 9 }, ], | ||||||
| 25 | ten => { | ||||||
| 26 | eleven => [qw/11 12 13 14 15 16 17 18 19 20/], | ||||||
| 27 | twentyone => [ | ||||||
| 28 | { | ||||||
| 29 | twentytwo => 22, | ||||||
| 30 | }, | ||||||
| 31 | { | ||||||
| 32 | twentythree => 23, | ||||||
| 33 | }, | ||||||
| 34 | { | ||||||
| 35 | twentyfour => 24, | ||||||
| 36 | twentyfive => 25, | ||||||
| 37 | twentysix => 26, | ||||||
| 38 | }, | ||||||
| 39 | ], | ||||||
| 40 | thirteen => 13, | ||||||
| 41 | }, | ||||||
| 42 | }; | ||||||
| 43 | |||||||
| 44 | 1 | 10 | my $HTTPTiny_response2 = { | ||||
| 45 | status => 200, | ||||||
| 46 | content => encode_json($HoAoH), | ||||||
| 47 | }; | ||||||
| 48 | |||||||
| 49 | 1 | 2 | HTTPTiny2h2o $HTTPTiny_response2; | ||||
| 50 | |||||||
| 51 | 1 | 5 | is $HTTPTiny_response2->content->ten->thirteen, 13, q{Accessor for deeply nested key found as expected}; | ||||
| 52 | |||||||
| 53 | 1 | 295 | is $HTTPTiny_response2->content->ten->doesntexist, undef, q{'undef' returned for non-existing key, due to internal use of 'd2o -autoundef'}; | ||||
| 54 | |||||||
| 55 | 1 | 355 | is_deeply o2d($HTTPTiny_response2->content), $HoAoH, q{Deep hash of arrays of hashes encoded as JSON was decoded properly}; | ||||
| 56 | |||||||
| 57 | 1 | 2736 | is $HTTPTiny_response2->content->doesnotexist, undef, q{handles 'content' that points to undef setting it -autoundef}; | ||||
| 58 | |||||||
| 59 | # "-autothrow" should also complete normally when the JSON is valid. | ||||||
| 60 | 1 | 350 | my $autothrow_good = { | ||||
| 61 | status => 200, | ||||||
| 62 | content => encode_json({ answer => 42 }), | ||||||
| 63 | }; | ||||||
| 64 | |||||||
| 65 | 1 | 6 | HTTPTiny2h2o -autothrow, $autothrow_good; | ||||
| 66 | |||||||
| 67 | 1 | 3 | is $autothrow_good->content->answer, 42, q{'-autothrow' decodes valid JSON and objectifies content}; | ||||
| 68 | |||||||
| 69 | 1 | 277 | my $bad_json1 = { | ||||
| 70 | status => 200, | ||||||
| 71 | content => 'this is definitely not JSON', | ||||||
| 72 | }; | ||||||
| 73 | |||||||
| 74 | 1 1 | 11 37 | dies_ok { HTTPTiny2h2o -autothrow, $bad_json1 } q{non-JSON in 'content' of the response object causes decode_json to die}; | ||||
| 75 | |||||||
| 76 | 1 | 307 | my $bad_json2 = { | ||||
| 77 | status => 200, | ||||||
| 78 | content => 'this is definitely not JSON', | ||||||
| 79 | }; | ||||||
| 80 | |||||||
| 81 | 1 | 4 | HTTPTiny2h2o $bad_json2; | ||||
| 82 | 1 | 5 | is $bad_json2->content, q{this is definitely not JSON}, q{not using '-autothrow' on bad JSON returns original content via ->content accessor}; | ||||
| 83 | 1 | 261 | like ref $bad_json2, qr/Util::H2O/, q{ref type of response reference is Util::H2O}; | ||||
| 84 | 1 | 258 | is ref $bad_json2->content, "", q{ref type of ->content is empty string}; | ||||
| 85 | |||||||
| 86 | # Exercise the remaining option/precondition short-circuit paths. | ||||||
| 87 | 1 1 | 278 32 | dies_ok { HTTPTiny2h2o() } q{HTTPTiny2h2o without a response dies}; | ||||
| 88 | 1 1 | 257 28 | dies_ok { HTTPTiny2h2o 0 } q{HTTPTiny2h2o with a false non-reference response dies}; | ||||
| 89 | 1 1 | 254 28 | dies_ok { HTTPTiny2h2o q{not-an-option} } q{HTTPTiny2h2o does not mistake an arbitrary string for '-autothrow'}; | ||||
| 90 | 1 1 | 254 27 | dies_ok { HTTPTiny2h2o [] } q{HTTPTiny2h2o rejects a non-HASH response reference}; | ||||
| 91 | |||||||
| 92 | 1 | 260 | my $bad_resp = { | ||||
| 93 | status => 200, | ||||||
| 94 | }; | ||||||
| 95 | |||||||
| 96 | 1 1 | 6 27 | dies_ok { HTTPTiny2h2o $bad_resp} q{missing 'content' HASH key in provided reference cause pre-check to die}; | ||||
| 97 | |||||||
| 98 | # content is an empty | ||||||
| 99 | 1 | 248 | my $HTTPTiny_response3 = { | ||||
| 100 | status => 200, | ||||||
| 101 | content => undef, | ||||||
| 102 | }; | ||||||
| 103 | |||||||
| 104 | 1 | 4 | HTTPTiny2h2o $HTTPTiny_response3; | ||||
| 105 | |||||||
| 106 | 1 | 3 | is $HTTPTiny_response3->content->doesnotexist, undef, q{handles 'content' that points to undef setting it -autoundef}; | ||||
| 107 | |||||||
| 108 | 1 | 346 | note "Testing JSON containers other than expected HASH - still underdeveloped area"; | ||||
| 109 | # content is an empty array | ||||||
| 110 | 1 | 544 | my $HTTPTiny_response4 = { | ||||
| 111 | status => 200, | ||||||
| 112 | content => "[]", | ||||||
| 113 | }; | ||||||
| 114 | |||||||
| 115 | 1 | 3 | HTTPTiny2h2o $HTTPTiny_response4; | ||||
| 116 | 1 | 16 | is $HTTPTiny_response4->content->get(1), undef, q{empty JSON array makes 'get' AREF vmethod return undef }; | ||||
| 117 | 1 | 350 | is $HTTPTiny_response4->content->scalar, 0, q{empty JSON array makes 'scalar' AREF vmethod return undef }; | ||||
| 118 | |||||||
| 119 | 1 | 271 | done_testing; | ||||