Definitions/HTTP.pm untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 129 CC 3 SCAR 9.9
CC
3
Cov
0%
request_send
6.0
_encode_utf8
6.0
HTTP
2.0
CRAP 2.7
1 package Test::BDD::Cucumber::Definitions::HTTP;
2  
3 use strict;
4 use warnings;
5  
6 use Const::Fast;
7 use DDP ( show_unicode => 1 );
8 use Exporter qw(import);
9 use Hash::MultiValue;
10 use HTTP::Response;
11 use HTTP::Tiny;
12 use Test::BDD::Cucumber::Definitions qw(S :validator);
13 use Test::More;
14  
15 our $VERSION = '0.41';
16  
17 our @EXPORT_OK = qw(HTTP);
18  
19 my $http = HTTP::Tiny->new();
20  
21 # Exceptions will result in a pseudo-HTTP status code of 599 and a reason of "Internal Exception".
22 # The content field in the response will contain the text of the exception.
23 const my $HTTP_INTERNAL_EXCEPTION => 599;
24  
25 ## no critic [Subroutines::RequireArgUnpacking]
26  
27 sub HTTP {
28 return __PACKAGE__;
29 }
30  
31 sub request_header_set {
32 my $self = shift;
33 my ( $header, $value ) = validator_ns->(@_);
34  
35 S->{HTTP} = __PACKAGE__;
36  
37 S->{_HTTP}->{request}->{headers}->{$header} = $value;
38  
39 return;
40 }
41  
42 sub request_content_set {
43 my $self = shift;
44 my ($content) = validator_s->(@_);
45  
46 S->{HTTP} = __PACKAGE__;
47  
48 S->{_HTTP}->{request}->{content} = $content;
49  
50 return;
51 }
52  
53 sub request_send {
54 my $self = shift;
55 my ( $method, $url ) = validator_nn->(@_);
56  
57 S->{HTTP} = __PACKAGE__;
58  
59 my $options = {
60 headers => S->{_HTTP}->{request}->{headers},
61 content => $self->_encode_utf8( S->{_HTTP}->{request}->{content} ),
62 };
63  
64 S->{_HTTP}->{response} = $http->request( $method, $url, $options );
65  
66 if ( S->{_HTTP}->{response}->{status} == $HTTP_INTERNAL_EXCEPTION ) {
67 fail('Http request was sent');
68 diag( S->{_HTTP}->{response}->{content} );
69 }
70  
71 diag( 'Http request method = ' . np $method);
72 diag( 'Http request url = ' . np $url );
73 diag( 'Http request headers = ' . np S->{_HTTP}->{request}->{headers} );
74 diag( 'Http request content = ' . np S->{_HTTP}->{request}->{content} );
75  
76 # Clean http request
77 S->{_HTTP}->{request} = undef;
78  
79 S->{_HTTP}->{response_object} = HTTP::Response->new(
80 S->{_HTTP}->{response}->{status},
81 S->{_HTTP}->{response}->{reason},
82 [ Hash::MultiValue->from_mixed( S->{_HTTP}->{response}->{headers} )->flatten ],
83 S->{_HTTP}->{response}->{content},
84 );
85  
86 return;
87 }
88  
89 sub response_code_eq {
90 my $self = shift;
91 my ($code) = validator_i->(@_);
92  
93 is( S->{_HTTP}->{response}->{status}, $code, qq{Http response code eq "$code"} );
94  
95 diag( sprintf 'Http response status = "%s %s"', S->{_HTTP}->{response}->{status},
96 S->{_HTTP}->{response}->{reason} );
97  
98 return;
99 }
100  
101 sub response_header_eq {
102 my $self = shift;
103 my ( $header, $value ) = validator_ns->(@_);
104  
105 is( S->{_HTTP}->{response}->{headers}->{ lc $header }, $value, qq{Http response header "$header" eq "$value"} );
106  
107 diag( 'Http response headers = ' . np S->{_HTTP}->{response}->{headers} );
108  
109 return;
110 }
111  
112 sub response_header_re {
113 my $self = shift;
114 my ( $header, $value ) = validator_nr->(@_);
115  
116 like( S->{_HTTP}->{response}->{headers}->{ lc $header }, $value, qq{Http response header "$header" re "$value"} );
117  
118 diag( 'Http response headers = ' . np S->{_HTTP}->{response}->{headers} );
119  
120 return;
121 }
122  
123 sub response_content_eq {
124 my $self = shift;
125 my ($content) = validator_s->(@_);
126  
127 is( S->{_HTTP}->{response_object}->decoded_content(), $content, qq{Http response content eq "$content"} );
128  
129 diag( 'Http response charset = ' . np S->{_HTTP}->{response_object}->headers->content_type_charset );
130  
131 return;
132 }
133  
134 sub response_content_re {
135 my $self = shift;
136 my ($content) = validator_r->(@_);
137  
138 like( S->{_HTTP}->{response_object}->decoded_content(), $content, qq{Http response content re "$content"} );
139  
140 diag( 'Http response charset = ' . np S->{_HTTP}->{response_object}->headers->content_type_charset );
141  
142 return;
143 }
144  
145 sub content {
146 my $self = shift;
147  
148 return S->{_HTTP}->{response_object}->decoded_content;
149 }
150  
151 sub _encode_utf8 {
152 my $self = shift;
153 my ($str) = @_;
154  
155 if ( utf8::is_utf8 $str ) {
156 utf8::encode $str;
157 }
158  
159 return $str;
160 }
161  
162 1;