The Perl and Raku Conference 2025: Greenville, South Carolina - June 27-29 Learn more

NAME

Net::API::CPAN::Mock - Meta CPAN API

SYNOPSIS

my $this = Net::API::CPAN::Mock->new(
pretty => 1,
openapi => '/path/to/cpan-openapi-specs.json',
) || die( Net::API::CPAN::Mock->error, "\n" );
$mock->bind || die( $mock->error );
$mock->start || die( $mock->error );
my $url = $mock->url_base;
# perform tests by connecting to the mock MetaCPAN API at $url
$mock->stop;

VERSION

v0.1.0

DESCRIPTION

This provides a mock MetaCPAN API server based on the original mock server idea in Test::HTTP::MockServer

It emulates all the known endpoints of the CPAN API, and endeavours to mimic its responses.

It provides sample data for all endpoints. Those data are based on real CPAN modules, but have been completely anonymised using fake names and fake e-mail addresses.

CONSTRUCTOR

new

This instantiates a new Net::API::CPAN::Mock object. It accepts the following options as an hash or hash reference.

It returns the new object upon success, or upon error, sets an error and returns undef in scalar context, or an empty list in list context.

  • checksum

    OpenAPI specifications file checksum for caching. This is set automatically by load_specs

  • endpoints

    OpenAPI resulting endpoints we derived from the specs

  • host

    The host to use when starting the mock CPAN API server.

  • openapi

    This is the filepath to the OpenAPI specifications file.

  • port

    The port to use when starting the mock CPAN API server.

  • pretty

    Boolean. If true, the JSON data returned by the mock server will be in a human-readable format.

  • schema

    The OpenAPI resulting schema we derived from the specs.

METHODS

bind

This creates a socket, and binds it to the host and port

If none provided, the host is set to 127.0.0.1and the port is randomly generated.

Once done, it sets the host and port and socket values to be accessible with their respective method.

It returns the current object upon success, or, upon error, sets an error and returns undef in scalar context, or an empty list in list context.

data

my $data = $self->data;

Read-only. This returns the test data as an hash reference. The test data are stored in $Net::API::CPAN::Mock::TEST_DATA

endpoints

Sets or gets the hash reference of endpoints derived from the specs analysed in load_specs

This returns an hash object, or undef if nothing was set yet.

host

When used as a mutator, i.e. setting a value, this sets the host to use when starting the server.

In accessor mode, this ensures the socket is bound to the specified host and port.

By default, the value used is 127.0.0.1

This returns the current host value.

json

Sets or gets the JSON object.

load_specs

This takes a file path to an OpenAPI specifications, and will load its enclosed endpoints, schema and all specifications that will be used to run the mock CPAN API server.

The result is cached, so it can be re-called safely. If the OpenAPI specifications have been changed, the next call to load_specs will have it re-load the file.

It sets the value for specs, sets the MD5 checksum for the OpenAPI specifications file, and the hash reference of endpoints.

It returns the current object upon success, or, upon error, sets an error and returns undef in scalar context, or an empty list in list context.

pid

Sets or gets the process ID for this mock CPAN API server.

Returns a regular number or undef if nothing was set yet.

port

When used as a mutator, i.e. setting a value, this sets the port to use when starting the server.

In accessor mode, this ensures the socket is bound to the specified host and port.

By default, the value used is a random integer.

This returns the current host value.

pretty

$mock->pretty(1);
my $bool = $mock->pretty;

Sets or gets the boolean value of whether the JSON data returned by the mock server will be in a human-readable format.

Defaults to false.

socket

Sets or gets the server socket value.

Returns an IO::Socket filehandle when set, or undef otherwise.

specs

The OpenAPI JSON specifications as perl hash reference. This is set upon loading by load_specs

start

Starts the mock CPAN API server, forks and returns the current object upon success, or, upon error, sets an error and returns undef in scalar context, or an empty list in list context.

The child process whose pid can be accessed with "pid" in pid goes on waiting for connections.

stop

Stop the mock CPAN API server, and returns the killed process pid

url_base

my $uri = $mock->url_base; # http://127.0.0.1:1234

Read-only. Based on the host and port set, this returns an URI object representing the base HTTP URI for all endpoints.

ENDPOINTS

The following are the endpoints supported by MetaCPAN REST API:

SEE ALSO

Net::API::CPAN, Net::API::CPAN::Activity, Net::API::CPAN::Author, Net::API::CPAN::Changes, Net::API::CPAN::Changes::Release, Net::API::CPAN::Contributor, Net::API::CPAN::Cover, Net::API::CPAN::Diff, Net::API::CPAN::Distribution, Net::API::CPAN::DownloadUrl, Net::API::CPAN::Exception, Net::API::CPAN::Favorite, Net::API::CPAN::File, Net::API::CPAN::Filter, Net::API::CPAN::Generic, Net::API::CPAN::List, Net::API::CPAN::Mirror, Net::API::CPAN::Mirrors, Net::API::CPAN::Module, Net::API::CPAN::Package, Net::API::CPAN::Permission, Net::API::CPAN::Rating, Net::API::CPAN::Release, Net::API::CPAN::Release::Recent, Net::API::CPAN::Release::Suggest, Net::API::CPAN::Scroll

CREDITS

Daniel Ruoso for the original Test::HTTP::MockServer

COPYRIGHT & LICENSE

Copyright(c) 2023 DEGUEST Pte. Ltd.

All rights reserved

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.