« lib/Test2/Harness/UI/Controller/Coverage.pm
↑ Summary
lib/Test2/Harness/UI/Schema/Overlay/Run.pm »
| 1 | package Test2::Harness::UI::Response; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '0.000150'; | ||
| 6 | |||
| 7 | use Carp qw/croak/; | ||
| 8 | use Time::HiRes qw/sleep time/; | ||
| 9 | use Test2::Harness::Util::JSON qw/encode_json/; | ||
| 10 | |||
| 11 | use parent 'Plack::Response'; | ||
| 12 | |||
| 13 | use Importer Importer => 'import'; | ||
| 14 | our %EXPORT_ANON = ( | ||
| 15 | resp => sub { __PACKAGE__->new(@_) }, | ||
| 16 | error => sub { __PACKAGE__->error(@_) }, | ||
| 17 | ); | ||
| 18 | |||
| 19 | my %DEFAULT_ERRORS = ( | ||
| 20 | 204 => 'No Content', | ||
| 21 | 205 => 'Reset Content', | ||
| 22 | 400 => 'Bad Request', | ||
| 23 | 401 => 'Unauthorized', | ||
| 24 | 402 => 'Payment Required', | ||
| 25 | 403 => 'Forbidden', | ||
| 26 | 404 => 'Not Found', | ||
| 27 | 405 => 'Method Not Allowed', | ||
| 28 | 406 => 'Not Acceptable', | ||
| 29 | 407 => 'Proxy Authentication Required', | ||
| 30 | 408 => 'Request Timeout', | ||
| 31 | 409 => 'Conflict', | ||
| 32 | 410 => 'Gone', | ||
| 33 | 411 => 'Length Required', | ||
| 34 | 412 => 'Precondition Failed', | ||
| 35 | 413 => 'Payload Too Large', | ||
| 36 | 414 => 'URI Too Long', | ||
| 37 | 415 => 'Unsupported Media Type', | ||
| 38 | 416 => 'Range Not Satisfiable', | ||
| 39 | 417 => 'Expectation Failed', | ||
| 40 | 418 => 'I\'m a teapot', | ||
| 41 | 421 => 'Misdirected Request', | ||
| 42 | 422 => 'Unprocessable Entity', | ||
| 43 | 423 => 'Locked', | ||
| 44 | 424 => 'Failed Dependency', | ||
| 45 | 426 => 'Upgrade Required', | ||
| 46 | 428 => 'Precondition Required', | ||
| 47 | 429 => 'Too Many Requests', | ||
| 48 | 431 => 'Request Header Fields Too Large', | ||
| 49 | 451 => 'Unavailable For Legal Reasons', | ||
| 50 | 500 => 'Internal Server Error', | ||
| 51 | 501 => 'Not Implemented', | ||
| 52 | 502 => 'Bad Gateway', | ||
| 53 | 503 => 'Service Unavailable', | ||
| 54 | 504 => 'Gateway Timeout', | ||
| 55 | 505 => 'HTTP Version Not Supported', | ||
| 56 | 506 => 'Variant Also Negotiates', | ||
| 57 | 507 => 'Insufficient Storage', | ||
| 58 | 508 => 'Loop Detected', | ||
| 59 | 510 => 'Not Extended', | ||
| 60 | 511 => 'Network Authentication Required', | ||
| 61 | ); | ||
| 62 | |||
| 63 | BEGIN { | ||
| 64 | for my $accessor (qw/no_wrap is_error errors messages title css js raw_body/) { | ||
| 65 | no strict 'refs'; | ||
| 66 | *{"$accessor"} = sub { $_[0]->{$accessor} = $_[1] if @_ > 1; $_[0]->{$accessor} }; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | sub stream { | ||
| 71 | my $self = shift; | ||
| 72 | if (@_) { | ||
| 73 | my %params = @_; | ||
| 74 | |||
| 75 | my $env = $params{env} or croak "'env' is a required parameter"; | ||
| 76 | |||
| 77 | my ($done, $fetch); | ||
| 78 | if(my $rs = $params{resultset}) { | ||
| 79 | my $json = Test2::Harness::Util::JSON::JSON()->new->utf8(0)->convert_blessed(1)->allow_nonref(1); | ||
| 80 | my $go = 1; | ||
| 81 | $done = sub { !$go }; | ||
| 82 | $fetch = sub { | ||
| 83 | $go = $rs->next() or return; | ||
| 84 | my $data = $go; | ||
| 85 | if(my $meth = $params{data_method}) { | ||
| 86 | $data = $go->$meth(); | ||
| 87 | } | ||
| 88 | my $out = $json->encode($data) . "\n"; | ||
| 89 | return $out; | ||
| 90 | }; | ||
| 91 | } | ||
| 92 | else { | ||
| 93 | $done = $params{done} or croak "'done' is a required parameter"; | ||
| 94 | $fetch = $params{fetch} or croak "'fetch' is a required parameter"; | ||
| 95 | } | ||
| 96 | |||
| 97 | my $wait = $params{wait} || 0.2; | ||
| 98 | my $cleanup = $params{cleanup}; | ||
| 99 | |||
| 100 | my $ct = $params{content_type} || $params{'content-type'} || $params{'Content-Type'} or croak "'content_type' is a required attribute"; | ||
| 101 | my $cache = $params{cache} // 1; | ||
| 102 | |||
| 103 | my @headers = @{$params{headers} // []}; | ||
| 104 | push @headers => ('Content-Type' => $ct); | ||
| 105 | push @headers => ('Cache-Control' => 'no-store') unless $cache; | ||
| 106 | |||
| 107 | my $last_write = time; | ||
| 108 | $self->{stream} = sub { | ||
| 109 | my $responder = shift; | ||
| 110 | my $writer = $responder->([200, \@headers]); | ||
| 111 | |||
| 112 | my $end = 0; | ||
| 113 | while (!$end) { | ||
| 114 | $end = $done->(); | ||
| 115 | |||
| 116 | my $seen = 0; | ||
| 117 | for my $item ($fetch->()) { | ||
| 118 | $writer->write($item); | ||
| 119 | last unless $env->{'psgix.io'}->connected; | ||
| 120 | $last_write = time; | ||
| 121 | $seen++; | ||
| 122 | } | ||
| 123 | |||
| 124 | unless ($seen || $end) { | ||
| 125 | if (time - $last_write > 1) { | ||
| 126 | $writer->write("\n"); | ||
| 127 | last unless $env->{'psgix.io'}->connected; | ||
| 128 | } | ||
| 129 | sleep $wait; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | $cleanup->() if $cleanup; | ||
| 134 | $writer->close; | ||
| 135 | }; | ||
| 136 | } | ||
| 137 | |||
| 138 | return $self->{stream}; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub error { | ||
| 142 | my $class = shift; | ||
| 143 | my ($code, $msg) = @_; | ||
| 144 | |||
| 145 | $msg ||= $DEFAULT_ERRORS{$code} || 'Error'; | ||
| 146 | |||
| 147 | my $self = $class->new($code); | ||
| 148 | $self->add_error($msg); | ||
| 149 | $self->is_error(1); | ||
| 150 | $self->title($msg ? "error: $code - $msg" : "error: $code"); | ||
| 151 | $self->body($msg); | ||
| 152 | |||
| 153 | return $self; | ||
| 154 | } | ||
| 155 | |||
| 156 | *add_message = \&add_msg; | ||
| 157 | sub add_msg { | ||
| 158 | my $self = shift; | ||
| 159 | |||
| 160 | push @{$self->{messages} ||= []} => @_; | ||
| 161 | |||
| 162 | return; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub add_error { | ||
| 166 | my $self = shift; | ||
| 167 | |||
| 168 | push @{$self->{errors} ||= []} => @_; | ||
| 169 | |||
| 170 | return; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub as_json { | ||
| 174 | my $self = shift; | ||
| 175 | my (%inject) = @_; | ||
| 176 | $self->content_type('application/json'); | ||
| 177 | |||
| 178 | my $data = { | ||
| 179 | %inject, | ||
| 180 | messages => $self->{messages}, | ||
| 181 | errors => $self->{errors}, | ||
| 182 | }; | ||
| 183 | |||
| 184 | $self->raw_body($data); | ||
| 185 | } | ||
| 186 | |||
| 187 | sub add_css { | ||
| 188 | my $self = shift; | ||
| 189 | |||
| 190 | push @{$self->{css} ||= []} => @_; | ||
| 191 | |||
| 192 | return; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub add_js { | ||
| 196 | my $self = shift; | ||
| 197 | |||
| 198 | push @{$self->{js} ||= []} => @_; | ||
| 199 | |||
| 200 | return; | ||
| 201 | } | ||
| 202 | |||
| 203 | 1; | ||
| 204 | |||
| 205 | __END__ |