Test::...::REST(3pm)

πŸ“¦ version 0.0.1, in opensuse-leap 16.0

πŸ’– 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 $method is the all-caps http method name and $path is 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 error 500. 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 with wrap_object, the object-oriented calling convention will be followed, so $self is the first argument. BUT for the wrap_hash case, the invocation happens as a simple invocation of the code reference, so there's no $self. semantics, we love her πŸ’…
  • $request β€” the HTTP::Request object.
  • $response β€” the HTTP::Response object.
  • $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 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