| 1 | package Test::HTTP::Scenario::Adapter::LWP; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp qw(croak carp); | ||
| 7 | use Scalar::Util qw(blessed); | ||
| 8 | |||
| 9 | #----------------------------------------------------------------------# | ||
| 10 | # Constructor | ||
| 11 | #----------------------------------------------------------------------# | ||
| 12 | # Entry: | ||
| 13 | # class name | ||
| 14 | # | ||
| 15 | # Exit: | ||
| 16 | # new adapter object with internal state initialised | ||
| 17 | # | ||
| 18 | # Side effects: | ||
| 19 | # none | ||
| 20 | # | ||
| 21 | # Notes: | ||
| 22 | # - scenario is attached later via set_scenario() | ||
| 23 | # - installed/uninstalled counters guard install/uninstall | ||
| 24 | # | ||
| 25 | sub new { | ||
| 26 | my $class = $_[0]; | ||
| 27 | |||
| 28 | return bless { | ||
| 29 | scenario => undef, # scenario object (set via set_scenario) | ||
| 30 | installed => 0, # install() guard counter | ||
| 31 | uninstalled => 0, # uninstall() guard counter | ||
| 32 | _orig_request => undef, # original LWP::UserAgent::request coderef | ||
| 33 | }, $class; | ||
| 34 | } | ||
| 35 | |||
| 36 | #----------------------------------------------------------------------# | ||
| 37 | # Scenario attachment | ||
| 38 | #----------------------------------------------------------------------# | ||
| 39 | # Entry: | ||
| 40 | # $scenario => Test::HTTP::Scenario object | ||
| 41 | # | ||
| 42 | # Exit: | ||
| 43 | # adapter now references scenario | ||
| 44 | # | ||
| 45 | # Side effects: | ||
| 46 | # none | ||
| 47 | # | ||
| 48 | # Notes: | ||
| 49 | # - no weaken() because scenario lifetime is controlled by tests | ||
| 50 | # | ||
| 51 | sub set_scenario { | ||
| 52 | my ($self, $scenario) = @_; | ||
| 53 | $self->{scenario} = $scenario; | ||
| 54 | |||
| 55 | return; | ||
| 56 | } | ||
| 57 | |||
| 58 | #----------------------------------------------------------------------# | ||
| 59 | # Install: monkey‑patch LWP::UserAgent::request | ||
| 60 | #----------------------------------------------------------------------# | ||
| 61 | # Entry: | ||
| 62 | # none | ||
| 63 | # | ||
| 64 | # Exit: | ||
| 65 | # request() overridden to call scenario->handle_request | ||
| 66 | # | ||
| 67 | # Side effects: | ||
| 68 | # - modifies LWP::UserAgent::request globally | ||
| 69 | # | ||
| 70 | # Notes: | ||
| 71 | # - recursion‑safe via localised glob | ||
| 72 | # - install() is idempotent per adapter instance | ||
| 73 | # | ||
| 74 | sub install { | ||
| 75 | my ($self) = @_; | ||
| 76 | return if $self->{installed}++; | ||
| 77 | |||
| 78 | no warnings 'redefine'; | ||
| 79 | |||
| 80 | # Capture original request method (glob slot) | ||
| 81 | $self->{_orig_request} ||= \&LWP::UserAgent::request; | ||
| 82 | |||
| 83 | my $adapter = $self; | ||
| 84 | my $orig = $self->{_orig_request}; | ||
| 85 | |||
| 86 | *LWP::UserAgent::request = sub { | ||
| 87 | my ($ua, $request, @rest) = @_; | ||
| 88 | |||
| 89 | my $scenario = $adapter->{scenario}; | ||
| 90 | unless ($scenario) { | ||
| 91 | carp "STRAY LWP REQUEST DURING GLOBAL DESTRUCTION"; | ||
| 92 | return $orig->($ua, $request, @rest); | ||
| 93 | } | ||
| 94 | |||
| 95 | # Prevent recursion: internal calls use original method | ||
| 96 | local *LWP::UserAgent::request = $orig; | ||
| 97 | |||
| 98 | return $scenario->handle_request( | ||
| 99 | $request, | ||
| 100 | sub { $orig->($ua, $request, @rest) }, | ||
| 101 | ); | ||
| 102 | }; | ||
| 103 | |||
| 104 | return; | ||
| 105 | } | ||
| 106 | |||
| 107 | #----------------------------------------------------------------------# | ||
| 108 | # Uninstall: restore original LWP::UserAgent::request | ||
| 109 | #----------------------------------------------------------------------# | ||
| 110 | # Entry: | ||
| 111 | # none | ||
| 112 | # | ||
| 113 | # Exit: | ||
| 114 | # original request() restored | ||
| 115 | # | ||
| 116 | # Side effects: | ||
| 117 | # - modifies LWP::UserAgent::request globally | ||
| 118 | # | ||
| 119 | # Notes: | ||
| 120 | # - uninstall() is idempotent per adapter instance | ||
| 121 | # | ||
| 122 | sub uninstall { | ||
| 123 | my ($self) = @_; | ||
| 124 | return if $self->{uninstalled}++; | ||
| 125 | |||
| 126 | no warnings 'redefine'; | ||
| 127 | |||
| 128 | if ($self->{_orig_request}) { | ||
| 129 | *LWP::UserAgent::request = $self->{_orig_request}; | ||
| 130 | } | ||
| 131 | |||
| 132 | return; | ||
| 133 | } | ||
| 134 | |||
| 135 | #----------------------------------------------------------------------# | ||
| 136 | # Request normalization | ||
| 137 | #----------------------------------------------------------------------# | ||
| 138 | # Entry: | ||
| 139 | # $req => HTTP::Request object | ||
| 140 | # | ||
| 141 | # Exit: | ||
| 142 | # hashref with method, uri, headers, body | ||
| 143 | # | ||
| 144 | # Side effects: | ||
| 145 | # none | ||
| 146 | # | ||
| 147 | # Notes: | ||
| 148 | # - stable, serializable structure for fixture storage | ||
| 149 | # | ||
| 150 | sub normalize_request { | ||
| 151 | my ($self, $req) = @_; | ||
| 152 | |||
| 153 | croak "normalize_request() expects an HTTP::Request" | ||
| 154 | unless blessed($req) && $req->isa('HTTP::Request'); | ||
| 155 | |||
| 156 | return { | ||
| 157 | method => $req->method, | ||
| 158 | uri => $req->uri->as_string, | ||
| 159 | headers => { $req->headers->flatten }, | ||
| 160 | body => $req->content, | ||
| 161 | }; | ||
| 162 | } | ||
| 163 | |||
| 164 | #----------------------------------------------------------------------# | ||
| 165 | # Response normalization | ||
| 166 | #----------------------------------------------------------------------# | ||
| 167 | # Entry: | ||
| 168 | # $res => HTTP::Response object | ||
| 169 | # | ||
| 170 | # Exit: | ||
| 171 | # hashref with status, reason, headers, body | ||
| 172 | # | ||
| 173 | # Side effects: | ||
| 174 | # none | ||
| 175 | # | ||
| 176 | # Notes: | ||
| 177 | # - decoded_content(charset => 'none') preserves raw bytes | ||
| 178 | # | ||
| 179 | sub normalize_response { | ||
| 180 | my ($self, $res) = @_; | ||
| 181 | |||
| 182 | croak "normalize_response() expects an HTTP::Response" | ||
| 183 | unless blessed($res) && $res->isa('HTTP::Response'); | ||
| 184 | |||
| 185 | return { | ||
| 186 | status => $res->code, | ||
| 187 | reason => $res->message, | ||
| 188 | headers => { $res->headers->flatten }, | ||
| 189 | body => $res->decoded_content(charset => 'none'), | ||
| 190 | }; | ||
| 191 | } | ||
| 192 | |||
| 193 | #----------------------------------------------------------------------# | ||
| 194 | # Build a real HTTP::Response from stored hash | ||
| 195 | #----------------------------------------------------------------------# | ||
| 196 | # Entry: | ||
| 197 | # $hash => normalized response hash | ||
| 198 | # | ||
| 199 | # Exit: | ||
| 200 | # HTTP::Response object | ||
| 201 | # | ||
| 202 | # Side effects: | ||
| 203 | # none | ||
| 204 | # | ||
| 205 | # Notes: | ||
| 206 | # - headers restored exactly as stored | ||
| 207 | # | ||
| 208 | sub build_response { | ||
| 209 | my ($self, $hash) = @_; | ||
| 210 | |||
| 211 | require HTTP::Response; | ||
| 212 | |||
| 213 | my $res = HTTP::Response->new( | ||
| 214 | $hash->{status} // 200, | ||
| 215 | $hash->{reason} // 'OK', | ||
| 216 | ); | ||
| 217 | |||
| 218 | if (my $h = $hash->{headers}) { | ||
| 219 | while (my ($k, $v) = each %$h) { | ||
| 220 | $res->header($k => $v); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | $res->content($hash->{body} // ''); | ||
| 225 | |||
| 226 | return $res; | ||
| 227 | } | ||
| 228 | |||
| 229 | 1; | ||
| 230 | |||
| 231 | __END__ |