Test::...Server(3pm)
π¦ Test::...Server(3pm) π¦
okay bestie so this page lives in the Perl docs under User Contributed Perl Documentation, and the module is Test::HTTP::MockServer β yes, the header truncates it to Test::...Server(3pm), that's just how the man page renders it, don't panic π
β¨ NAME β¨
Test::HTTP::MockServer β implement a mock HTTP server for use in tests π
β¨ SYNOPSIS β¨
use Test::HTTP::MockServer;
my $server = Test::HTTP::MockServer->new();
my $url = $server->url_base();
# inject $url as the config for the remote http service.
my $handle_request_phase1 = sub {
my ($request, $response) = @_;
...
};
$server->start_mock_server($handle_request_phase1);
# run your tests against $handle_request_phase1
$server->stop_mock_server();
my $handle_request_phase2 = sub {
my ($request, $response) = @_;
...
};
$server->start_mock_server($handle_request_phase2);
# run your tests against $handle_request_phase2
$server->stop_mock_server();
π DESCRIPTION π
So here's the situation, bestie π sometimes you're writing a test and you literally cannot do dependency injection on the type of transport some API uses. Like, the code is gonna use actual HTTP, periodt. The only thing you get to control is the host and port it connects to. That's it. That's the whole vibe.
This class is your little superhero for exactly that π«Ά it gives you a simple way to mock the service being called. How? By binding to a random port on localhost and letting you inspect which port that ended up being π Using a random port is iconic because it means this whole thing works for tests running in parallel on the same host π₯
The socket gets bound and listened on the main test process, so the lifetime of the connection is defined by the lifetime of the test itself. Very main character energy β¨
And since the socket is already bound and listened to, the two control methods (start_mock_server and stop_mock_server) only fork for the accept call. Which means β and this is the good part β it's totally safe to call start and stop several times during one test in order to change the expectations of the mocked code π
That gives you the ability to easily configure the mock server's expectations across each step of your test case π On the other hand (balance, queen), it also means no state is shared between the code running in the mock server and the test code. Just so you know π
π§ METHOS π§
(not me β the original page really spells it like that, we're keeping it, iykyk)
new()β creates a new MockServer object β¨bind_mock_server()β finds a random available port, binds to it and listens on it. This is what lets you peek at what the mock url of the portal will be before the server forks to start. And if that random port is already in use? It just keeps trying until it finds one that works πͺhost()β returns the host the mock server is binding to. It'll callbind_mock_serverfor you if that hasn't been initialized yet. The current version always binds to127.0.0.1πport()β returns the port the mock server is bound and listening to. It'll callbind_mock_serverif that wasn't initialized yet πbase_url()β returns the url to be used as the base url for requests into the mock server. It'llbind_mock_serverif that wasn't initialized yet πΈstart_mock_server($request_processor)β this willbind_mock_serverif that wasn't initialized yet, thenforkto accept connections π
And to make it easier for state to propagate across different requests in your mock implementation, there will only be one connection at a time, and every request in that connection gets handled serially β
stop_mock_server()β kills the server running in the background, but it will not unbind the socket. Which means you can just callstart_mock_serveragain with a differentrequest_processorand the same url will be preserved π
π THE REQUEST PROCESSOR π
The request processor is that code reference you send as an argument to the start_mock_server call β it receives all the requests that come in via the socket π¬
Whenever a request is received, your code reference gets called with two arguments:
$requestβ anHTTP::Requestobject with the request as it was received π$responseβ anHTTP::Responseobject that will be sent back. It's initialized with"200 OK"and no content β¨
π heads up bestie: if your codedies while processing a request, a"500 Internal Server Error"response will be generated. dramatic but fair.
πΈ 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, no cap π
this is perl v5.42.0, dated 2021-04-16, and it's the Test::...Server(3pm) page β go forth and mock, queen ππ¦
π¦ 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