Test::...::REST(3pm)
π NAME π
okay so this is Test::HTTP::MockServer::REST β aka the REST Helper for Test::HTTP::MockServer, and honestly? it's giving "tiny module, huge slay" π
(also hi, you're reading Test::...::REST(3pm), the perl manpage, in case the terminal didn't already tell you π«Ά)
β¨ SYNOPSIS β¨
bestie, here's the whole vibe in code form:
my $rest = Test::HTTP::MockServer::REST->new(
'methoda_GET' => qr{^GET /foo/([a-z0-9]+)/bar$},
'methoda_POST' => qr{^POST /foo/([a-z0-9]+)/bar$},
);
# use an object, where methoda_GET and methoda_POST will be called
my $requestprocessor = $rest->wrap_object(MyMockServer->new());
# alternatively, just use a hash
my $requestprocessor = $rest->wrap_hash({ methoda_GET => sub { ... } })
literally just regexes in, coderef out. iconic behavior π
π¦ DESCRIPTION π¦
So this whole thing is a helper class that you use alongside Test::HTTP::MockServer to make mocking a REST service, like, actually easy?? no cap.
the way it works: you hand over the identifier of the operation, plus a regex to match the request against, and the module handles the rest. that's it. that's the whole lore π
π METHODS π
newβ creates a shiny new helper β¨ it takes a hash as input, where the key is the identifier of the operation and the value is a regular expression. that regex gets applied against the string"$method $path"β where$methodis the all-caps http method name and$pathis the path sent in the request. iykyk.wrap_objectβ returns a coderef you can use as the request processor, and it dispatches the calls as methods in the given object. cute right? BUT β οΈ if the object doesn't implement the method you listed, it will fail with error500. so make sure your object actually has the methods, bestie, fr fr.wrap_hashβ the simplified version of the above, dispatching based on a simple hash instead. lowkey the chill one of the family π
π CALLING THE WRAPPED CODE π
here's exactly what gets sent to the code your wrapper calls β no surprises, we're thorough:
$self(wrap_object version only) β if the code was wrapped withwrap_object, the object-oriented calling convention will be followed, so$selfis the first argument. BUT for thewrap_hashcase, the invocation happens as a simple invocation of the code reference, so there's no$self. semantics, we love her π$requestβ theHTTP::Requestobject.$responseβ theHTTP::Responseobject.$capturesβ an array reference with the items captured by the regex. the regex girlies ate π$dataβ decoded data submitted in the request. for now this is only available if the input content type is'application/json'. just json. only json. know that.
so if data is returned by the code, and the request had an appropriate Accept header, that data will be encoded in the given content type β and for now only JSON is supported π§
β οΈ lowkey important: if the code returns any data but there's no Accept header, or the accepted type is not supported, it will cause a failure. no vibes pass. π
πΈ COPYRIGHT πΈ
Copyright 2016 Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
okay that's the whole page, you're all caught up bestie π«Ά this was perl v5.42.0, dated 2021-04-16, Test::...::REST(3pm) β thanks for coming to my ted talk, periodt πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Test-HTTP-MockServer 0.0.1-bp160.1.10 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms