File Coverage

blib/lib/swat.pm
Criterion Covered Total %
statement 30 170 17.6
branch 0 70 0.0
condition 0 18 0.0
subroutine 10 18 55.5
pod 0 1 0.0
total 40 277 14.4


line stmt bran cond sub pod time code
1             package swat;
2              
3             our $VERSION = '0.1.96';
4              
5 1     1   15206 use base 'Exporter';
  1         1  
  1         119  
6              
7             our @EXPORT = qw{version};
8              
9             sub version {
10 0     0 0   print $VERSION, "\n"
11             }
12              
13              
14             1;
15              
16             package main;
17              
18 1     1   4 use strict;
  1         1  
  1         30  
19              
20 1     1   3 use Carp;
  1         4  
  1         65  
21 1     1   674 use File::Temp qw/ tempfile /;
  1         16420  
  1         79  
22              
23 1     1   6 use Test::More;
  1         2  
  1         7  
24 1     1   554 use swat::story;
  1         3  
  1         99  
25              
26 1     1   5 use Carp;
  1         1  
  1         40  
27 1     1   517 use Config::Tiny;
  1         731  
  1         39  
28 1     1   465 use YAML qw{LoadFile};
  1         4968  
  1         47  
29              
30 1     1   597 use Term::ANSIColor;
  1         5197  
  1         1213  
31              
32             my $config;
33              
34             sub config {
35              
36 0 0   0     unless ($config){
37 0 0 0       if (get_prop('suite_ini_file_path') and -f get_prop('suite_ini_file_path') ){
    0 0        
    0          
    0          
38 0           my $path = get_prop('suite_ini_file_path');
39 0 0         $config = $config = Config::Tiny->read($path) or confess "file $path is not valid .ini file";
40             }elsif(get_prop('suite_yaml_file_path') and -f get_prop('suite_yaml_file_path')){
41 0           my $path = get_prop('suite_yaml_file_path');
42 0           ($config) = LoadFile($path);
43             }elsif ( -f 'suite.ini' ){
44 0           my $path = 'suite.ini';
45 0 0         $config = $config = Config::Tiny->read($path) or confess "file $path is not valid .ini file";
46             }elsif ( -f 'suite.yaml'){
47 0           my $path = 'suite.yaml';
48 0           ($config) = LoadFile($path);
49             }else{
50 0           confess "configuration file is not found"
51             }
52             }
53              
54 0           return $config;
55             }
56              
57             sub make_http_request {
58              
59 0 0   0     return if get_prop('response_done');
60              
61 0           my ($fh, $content_file) = tempfile( DIR => get_prop('test_root_dir') );
62            
63 0           my $try_i;
64              
65 0           my $try = get_prop('try_num');
66              
67 0 0 0       if (get_prop('response') and @{get_prop('response')} ){
  0            
68              
69 0           ok(1,'server response is spoofed');
70              
71 0 0         open F, ">", $content_file or die $!;
72 0           print F ( join "\n", @{get_prop('response')});
  0            
73 0           close F;
74              
75 0 0         open F, ">", "$content_file.stderr" or die $!;
76 0           close F;
77              
78 0 0         open F, ">", "$content_file.hdr" or die $!;
79 0           close F;
80              
81 0           note "response saved to $content_file";
82              
83             }else{
84              
85 0           my $curl_cmd = get_prop('curl_cmd');
86 0           my $hostname = get_prop('hostname');
87 0           my $resource = get_prop('resource');
88 0           my $http_method = get_prop('http_method');
89              
90 0           my $curl_runner = "$curl_cmd -w '%{response_code}' -D $content_file.hdr -o $content_file --stderr $content_file.stderr '$hostname$resource' > $content_file.http_status";
91 0           my $curl_runner_short = tapout( "$curl_cmd -D - '$hostname$resource'", ['cyan'] );
92 0           my $http_status = 0;
93              
94 0           TRY: for my $i (1..$try){
95 0 0         note("try N [$i] $curl_runner") if debug_mod12();
96 0           $try_i = $i;
97 0           system($curl_runner);
98 0 0         if(open HTTP_STATUS, "$content_file.http_status"){
99 0           $http_status = ;
100 0           close HTTP_STATUS;
101 0           chomp $http_status;
102 0 0         note("got http status: $http_status") if debug_mod12();
103 0 0 0       last TRY if $http_status < 400 and $http_status > 0;
104 0 0 0       last TRY if $http_status >= 400 and ignore_http_err();
105              
106             }
107 0           my $delay = ($i)**2;
108 0 0         note("sleep for $delay seconds before next try") if debug_mod12();
109 0           sleep $delay;
110              
111             }
112              
113            
114             #note($curl_runner);
115              
116 0 0 0       if ( $http_status < 400 and $http_status > 0 ) {
    0          
117              
118 0           ok(1, tapout( $http_status, ['green'] )." / $try_i of $try ".$curl_runner_short);
119              
120             }elsif(ignore_http_err()){
121              
122 0           ok(1, tapout( $http_status, ['red'] )." / $try_i of $try ".$curl_runner_short);
123 0           note(
124             tapout(
125             "server returned bad response, ".
126             "but we still continue due to ignore_http_err set to 1",
127             ['red']
128             )
129             );
130              
131             }else{
132              
133 0           ok(1, tapout( $http_status, ['red'] )." / $try_i of $try ".$curl_runner_short);
134              
135 0           note "stderr:";
136              
137 0 0         open CURL_ERR, "$content_file.stderr" or die $!;
138 0           while ( my $i = ){
139 0           chomp $i;
140 0           note($i);
141             }
142 0           close CURL_ERR;
143              
144 0           note "http headers:";
145 0 0         open CURL_HDR, "$content_file.hdr" or die $!;
146 0           while ( my $i = ){
147 0           chomp $i;
148 0           note($i);
149             }
150 0           close CURL_HDR;
151              
152 0           note "http body:";
153 0 0         open CURL_RSP, "$content_file" or die $!;
154 0           while ( my $i = ){
155 0           chomp $i;
156 0           note($i);
157             }
158 0           close CURL_RSP;
159              
160 0           note("can't continue here due to unsuccessfull http status code");
161 0           exit(1);
162             }
163              
164 0 0         if (debug_mod12()) {
165 0           note tapout( "http headers saved to $content_file.hdr", ['bright_blue'] );
166 0           note tapout( "body saved to $content_file", ['bright_blue'] );
167             }
168              
169             }
170              
171              
172 0 0         open F, $content_file or die $!;
173 0           my $body_str = '';
174 0           $body_str.= $_ while ;
175 0           close F;
176              
177 0           set_prop( body => $body_str );
178              
179 0 0         open F, "$content_file.hdr" or die $!;
180 0           my $headers_str = '';
181 0           $headers_str.= $_ while ;
182 0           close F;
183              
184 0           set_prop( headers => $headers_str );
185              
186 0 0         if (debug_mod12()){
187 0           my $debug_bytes = get_prop('debug_bytes');
188 0           my $bshort = substr( $body_str, 0, $debug_bytes );
189 0 0         if (length($bshort) < length($body_str)) {
190 0           note("body:\n$bshort ... ( output truncated to $debug_bytes bytes )");
191             } else{
192 0           note("body:\n$body_str");
193             }
194             }
195              
196              
197 0           set_prop( response_done => 1 );
198              
199             }
200              
201             sub header {
202              
203            
204 0     0     my $project = get_prop('project');
205 0           my $swat_module = get_prop('swat_module');
206 0           my $hostname = get_prop('hostname');
207 0           my $resource = get_prop('resource');
208 0           my $http_method = get_prop('http_method');
209 0           my $curl_cmd = get_prop('curl_cmd');
210 0           my $debug = get_prop('debug');
211 0           my $try_num = get_prop('try_num');
212 0           my $ignore_http_err = get_prop('ignore_http_err');
213            
214 0           ok(1, "project: $project");
215 0           ok(1, "hostname: $hostname");
216 0           ok(1, "resource: $resource");
217 0           ok(1, "http method: $http_method");
218 0           ok(1,"swat module: $swat_module");
219 0           ok(1, "debug: $debug");
220 0           ok(1, "try num: $try_num");
221 0           ok(1, "ignore http errors: $ignore_http_err");
222            
223             }
224              
225             sub generate_asserts {
226              
227              
228 0     0     my $check_file = shift;
229              
230 0 0         header() if debug_mod2();
231              
232 0           dsl()->{debug_mod} = get_prop('debug');
233              
234 0           dsl()->{match_l} = get_prop('match_l');
235              
236 0 0         return if http_method() eq 'META';
237              
238 0           make_http_request();
239              
240 0           dsl()->{output} = headers().body();
241              
242 0           run_response_processor();
243              
244 0           eval {
245 0           dsl()->validate($check_file);
246             };
247              
248 0           my $err = $@;
249              
250 0           for my $r ( @{dsl()->results}){
  0            
251 0 0         note($r->{message}) if $r->{type} eq 'debug';
252 0 0         ok($r->{status}, $r->{message}) if $r->{type} eq 'check_expression';
253              
254             }
255              
256 0 0         confess "parser error: $err" if $err;
257              
258             }
259              
260             sub tapout {
261              
262 0     0     my $line = shift;
263 0           my $color = shift;
264              
265 0 0         if ($ENV{'swat_disable_color'}){
266 0           $line;
267             }else{
268 0           colored($color,$line);
269             }
270             }
271              
272             sub print_meta {
273              
274 0     0     note('@'.http_method());
275 0 0         open META, resource_dir()."/meta.txt" or die $!;
276 0           while (my $i = ){
277 0           chomp $i;
278 0           note( tapout( "\t $i", ['yellow'] ));
279             }
280 0           close META;
281            
282             }
283              
284             sub output_mod {
285              
286 0     0     return $ENV{output_mod};
287             }
288              
289             1;
290              
291              
292             __END__