Test::Override::UserAgent::Manual::ConfigurationPackage - Creating configuration packages
A "configuration package" is a normal perl package that Test::Override::UserAgent has imported a special set of symbols into and makes it easy to reuse override definitions in different test files.
To create a configuration package, just import Test::Override::UserAgent specifying it is for configuration:
package My::Overrides; use Test::Override::UserAgent 0.001 for => 'configuration'; # ... configuration here 1;
If you would like to setup your configuration module to give a nice error message, you may use:
package My::Overrides::Nice; BEGIN { if (!eval 'use Test::Override::UserAgent 0.001; 1;') { die "Test::Override::UserAgent >= 0.001 must be installed: $@"; } # Import for configuration Test::Override::UserAgent->import(for => 'configuration'); } # ... configuration here 1;
When then module is imported for configuration, the following subroutines are imported into the package:
This is the same as the method allow_live_requests in Test::Override::UserAgent but acts on the configuration in the package. If no arguments are provided, then live requests are enabled.
allow_live_requests
# Allow live requests allow_live(1);
This is a special method that returns the instance of Test::Override::UserAgent for this package.
Added in version 0.003; be sure to require this version for this feature.
This is a very convenient function to set some defaults for an entire block of code. This function takes the exact same arguments as override_request except the subroutine reference at the end is a block that will be built with the default arguments to all calls of override_request within. Calls made to override_for within an override_for block will extend the current scope's defaults with those provided. The main scope begins with no defaults.
override_request
override_for
override_for host => 'localhost', sub { override_request path => '/', sub { # ... only for host localhost and path / }; # Make an easy REST URL override_for path => '/rest', sub { # GET override_request method => 'GET' , sub { ... }; # POST override_request method => 'POST' , sub { ... }; # DELETE override_request method => 'DELETE', sub { ... }; # PUT override_request method => 'PUT' , sub { ... }; }; };
This is the same as the method in Test::Override::UserAgent but acts on the configuration in the package.
override_request path => '/', sub { # ... };
A full example of a configuration file:
package My::Overrides; use Test::Override::UserAgent 0.001 for => 'configuration'; # No live requests for us allow_live(0); # The root directory is always forbidden for localhost override_request host => 'localhost', path => '/', sub { [403, ['Content-Type' => 'text/plain'], ['Forbidden!'] }; 1;
To use a configuration package simply use it in the test file. Then using Package::Name->configuration will return the instance object so the configuration may be installed in a user agent or into the scope.
Package::Name->configuration
#!/usr/bin/env perl -T use My::Overrides; # Install into the current scope my $scope = My::Overrides->configuration->install_in_scope; # ... tests exit 0;
To install Test::Override::UserAgent, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Override::UserAgent
CPAN shell
perl -MCPAN -e shell install Test::Override::UserAgent
For more information on module installation, please visit the detailed CPAN module installation guide.