Scroll to navigation

Test::HTTP::MockServer::REST(3pm) User Contributed Perl Documentation Test::HTTP::MockServer::REST(3pm)

NAME

Test::HTTP::MockServer::REST - REST Helper for Test::HTTP::MockServer

SYNOPSIS

  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 { ... } })

DESCRIPTION

This is a helper class to be used with Test::HTTP::MockServer to easily implement the mock of a REST service, you will provide the identifier to the operation and a regex to match the request against.

METHODS

Creates a new helper. It takes a hash as input, where the key is the identifier of the operation and the value is a regular expression to be applied against the string "$method $path" with $method being the all-caps http method name and $path being the path sent in the request.
Return a coderef to be used as the request processor that dispatches the calls as methods in the given object. If the object doesn't implement the method listed, it will fail with error 500.
A simplified version that dispatches based on a simple hash.

CALLING THE WRAPPED CODE

The following arguments are sent to the code called by the wrapper:

$self (wrap_object version only)
If the code was wrapped with wrap_object, the object-oriented calling convention will be followed, therefore $self will be the first argument. However, in the wrap_hash case, the invocation happens as a simple invocation of the code reference, so there is no $self.
$request
The HTTP::Request object.
$response
The HTTP::Response object.
$captures
An array reference with the items captured by the regex.
$data
Decoded data submitted in the request. For now this is only available if the input content type is 'application/json'.

If data is returned by the code, and the request had an appropriate Accept header, the data will be encoded in the given content type (for now only JSON is supported).

If the code returns any data but there is no Accept header or the accepted type is not supported, it will cause a failure.

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.

2021-04-16 perl v5.40.0