| 1 | package Test::BDD::Cucumber::Definitions::Zip; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Archive::Zip; | ||
| 7 | use DDP ( show_unicode => 1 ); | ||
| 8 | use Exporter qw(import); | ||
| 9 | use IO::String; | ||
| 10 | use Test::BDD::Cucumber::Definitions qw(S :validator); | ||
| 11 | use Test::More; | ||
| 12 | use Try::Tiny; | ||
| 13 | |||
| 14 | our $VERSION = '0.41'; | ||
| 15 | |||
| 16 | our @EXPORT_OK = qw(Zip); | ||
| 17 | |||
| 18 | ## no critic [Subroutines::RequireArgUnpacking] | ||
| 19 | |||
| 20 | sub Zip { | ||
| 21 | return __PACKAGE__; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub read_http_response_content_as_zip { | ||
| 25 | my $self = shift; | ||
| 26 | |||
| 27 | S->{Zip} = __PACKAGE__; | ||
| 28 | |||
| 29 | # Clean archive | ||
| 30 | S->{_Zip}->{archive} = undef; | ||
| 31 | |||
| 32 | my $error; | ||
| 33 | |||
| 34 | S->{_Zip}->{archive} = try { | ||
| 35 | my $fh = IO::String->new( \S->{HTTP}->content() ); | ||
| 36 | |||
| 37 | # The default error handler is the Carp::carp function (warning, not an exception) | ||
| 38 | Archive::Zip::setErrorHandler( sub { die @_ } ); ## no critic [ErrorHandling::RequireCarping] | ||
| 39 | |||
| 40 | my $zip = Archive::Zip->new(); | ||
| 41 | $zip->readFromFileHandle($fh); | ||
| 42 | |||
| 43 | return $zip; | ||
| 44 | } | ||
| 45 | catch { | ||
| 46 | $error = "Could not read http response content as Zip: $_[0]"; | ||
| 47 | |||
| 48 | return; | ||
| 49 | }; | ||
| 50 | |||
| 51 | if ( !ok( !$error, qq{Http response content was read as Zip} ) ) { | ||
| 52 | diag($error); | ||
| 53 | diag( 'Http response content = ' . np S->{HTTP}->content ); | ||
| 54 | |||
| 55 | return; | ||
| 56 | } | ||
| 57 | |||
| 58 | return 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub member_names { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | return S->{_Zip}->{archive}->memberNames(); | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; |