Dredd-Hooks
view release on metacpan or search on metacpan
lib/Dredd/Hooks/Methods.pm view on Meta::CPAN
package Dredd::Hooks::Methods;
=head1 NAME
Dredd::Hooks::Methods - Sugar module for each of writing Dredd Hookfiles
=head1 SUMMARY
#!/usr/bin/env perl
# hookfiles.pl
use strict;
use warnings;
use Dredd::Hooks::Methods;
before('/messages > GET' => sub {
my ($transaction) = @_;
$transaction->{headers}{RawData}{Auth} = 'Basic: hud87y2h8o7ysdiuhlku12h=='
});
=head1 DESCRIPTION
Dredd::Hooks::Methods provides useful functions for writing
Dredd hook files.
L<Dredd|https://dredd.readthedocs.org> is a testing framework
for testing API BluePrint formatted API definition files
against the implemenation that exposes that API. This is useful
to ensure that the API documentation doesn't get out of sync
with an new code changes.
L<Dredd::Hooks> provides and implementation of the
L<Dredd hooks handler socket interface|https://dredd.readthedocs.org/en/latest/hooks-new-language/>
and ensures that hooks from user defined hook files are run in
the correct order and with the correct information.
Dredd::Hooks::Methods provides functionallity that allow the
user to define the hooks files that get run and their
functionality.
=head1 Creating a hook file
Hookfiles are plain .pl perl files containing some callbacks
that are run for specific events.
The hookfile should have a .pl file extention (required by the
dredd funtion and are provided as an argument to the dredd code:
C<dredd apiary.apib http://localhost:5000 --language perl --hookfiles=./hooks/hooks_*.pl>
for each event listed in the Dredd documentation a method is
provided that takes a sub ref that will be run for that event,
This event will receive a transaction (HashRef) or an arrayref of
transactions that it should modify in place. The return value of this
function will be ignored:
beforeAll(sub {
my ($transactions) = @_;
});
beforeEach(sub {
my ($transaction) = @_;
});
See the each event below for the which events take which arguments.
If multiple callbacks are defined for the same event then these will
be run individually in the order defined. e.g:
beforeAll(sub {
... # Run First
});
beforeAll(sub {
... # Run Second
});
You can also supply multiple files (or a glob) to the dredd command
and these will all be coallated together. Allowing spefic hooks for
specific transactions or event types.
All events are run in a specific order:
beforeAll - Run before all transactions
beforeEach - Run before each transaction
before - Run before specific transactions
beforeEachValidation - Run before each Validation step
beforeValidation - Run before a specific validation step
after - Run after a specific transaction
afterEach - Run after each transaction
afterAll - Run after all transactions
=cut
use strict;
use warnings;
use Hash::Merge;
use Sub::Exporter -setup => {
exports => [qw/
before
beforeAll
beforeEach
beforeEachValidation
beforeValidation
after
afterAll
afterEach
get_hooks
merge_hook
/],
groups => {
default => [qw/
before
beforeAll
beforeEach
beforeEachValidation
beforeValidation
after
afterAll
afterEach
/],
handler => [qw/
get_hooks
merge_hook
/]
}
( run in 0.736 second using v1.01-cache-2.11-cpan-39bf76dae61 )