File Coverage

blib/lib/Nexmo/SMS/MockLWP.pm
Criterion Covered Total %
statement 46 48 95.8
branch 2 4 50.0
condition n/a
subroutine 8 8 100.0
pod n/a
total 56 60 93.3


line stmt bran cond sub pod time code
1             package Nexmo::SMS::MockLWP;
2              
3             # ABSTRACT: Module for the Nexmo SMS API!
4              
5              
6 7     7   351902 use LWP::UserAgent;
  7         269661  
  7         202  
7 7     7   53 use HTTP::Response;
  7         23  
  7         119  
8 7     7   4053 use JSON::PP;
  7         80207  
  7         473  
9              
10 7     7   45 use strict;
  7         13  
  7         104  
11 7     7   30 use warnings;
  7         9  
  7         201  
12              
13 7     7   49 no warnings 'redefine';
  7         11  
  7         2036  
14              
15             our $VERSION = 0.01;
16              
17             *LWP::UserAgent::post = sub {
18 5     5   17 my ($object,$url,$params) = @_;
19            
20 5         17 my $json = do{ undef $/; };
  5         15  
  5         133  
21            
22 5         64 my $coder = JSON::PP->new->ascii->pretty->allow_nonref;
23 5         730 my $perl = $coder->decode( $json );
24 5         60864 my $from = $params->{from};
25            
26 5 50       29 if ( $url =~ /get-balance/ ) {
27 0         0 $from = 'get-balance';
28 0         0 $url = 'get-balance';
29             }
30            
31 5         15 my $subhash = $perl->{$url}->{$from};
32 5         40 my $response = $coder->encode( $subhash );
33            
34 5         3569 my $http_response = HTTP::Response->new( 200 );
35 5         401 $http_response->content( $response );
36            
37 5         217 return $http_response;
38             };
39              
40             *LWP::UserAgent::get = sub {
41 1     1   4 my ($object,$url,$params) = @_;
42            
43 1         2 my $json = do{ undef $/; };
  1         4  
  1         35  
44            
45 1         12 my $coder = JSON::PP->new->ascii->pretty->allow_nonref;
46 1         134 my $perl = $coder->decode( $json );
47 1         10962 my $from = $params->{from};
48            
49 1 50       5 if ( $url =~ /get-balance/ ) {
50 1         9 $from = 'get-balance';
51 1         2 $url = 'get-balance';
52             }
53            
54 1         3 my $subhash = $perl->{$url}->{$from};
55 1         5 my $response = $coder->encode( $subhash );
56            
57 1         158 my $http_response = HTTP::Response->new( 200 );
58 1         69 $http_response->content( $response );
59            
60 1         38 return $http_response;
61             };
62              
63             1;
64              
65             =pod
66              
67             =encoding UTF-8
68              
69             =head1 NAME
70              
71             Nexmo::SMS::MockLWP - Module for the Nexmo SMS API!
72              
73             =head1 VERSION
74              
75             version 0.10
76              
77             =head1 DESCRIPTION
78              
79             This module mocks POST requests. It exists only for the unit tests!
80              
81             =head1 AUTHOR
82              
83             Renee Baecker
84              
85             =head1 COPYRIGHT AND LICENSE
86              
87             This software is Copyright (c) 2016 by Renee Baecker.
88              
89             This is free software, licensed under:
90              
91             The Artistic License 2.0 (GPL Compatible)
92              
93             =cut
94              
95             __DATA__