Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2015/03/31

Testing my API changes with Perl

I have an API. I like some things about it, but it was kinda clunky in many ways.

I rewrote it. I thought I had everything together, used the same SQL queries, but I wanted to be sure it kicked out the same output.

So, I wrote tests using Perl's Test::Most and cmp_deeply().

A few weeks ago, I knew Perl testing existed but I didn't know much about it. Now it's practical.
#!/usr/bin/env perl
use feature qw'say state' ;
use subs qw'uniq' ;
use Data::Dumper ;
use JSON ;
use LWP::UserAgent ;
use Test::Most ;
my $agent = LWP::UserAgent->new() ;
$agent->credentials(
# AS IF I'd GIVE YOU THIS
);
my $server = 'https://myserver.test/apis' ;
my @endpoints = qw{
first
second
third
fourt
} ;
my $comp ;
for my $endpoint ( @endpoints ) {
for my $api ( qw{ test_api newest_api } ) {
my $url = join '/' ,
$server ,
$api ,
$endpoint ;
my $response = $agent->get( $url ) ;
$comp->{ $endpoint }->{ $api } = decode_json( $response->content() ) ;
}
}
for my $endpoint ( sort keys %$comp ) {
my $data = $comp->{ $endpoint } ;
cmp_deeply(
$data->{ test_api } ,
$data->{ newest_api } ,
"-- $endpoint is the same"
) ;
}
done_testing() ;
view raw api_tests.pl hosted with ❤ by GitHub

No comments:

Post a Comment