logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Net::Duo::Mock::Agent - Mock LWP::UserAgent for Net::Duo testing

Author

       Russ Allbery <rra@cpan.org>

Class Methods

       new(ARGS)
           Create  a  new  Net::Duo::Mock::Agent  object.   ARGS should be the same data structure passed to the
           Net::Duo-derived constructor (with the  obvious  exception  of  the  user_agent  argument,  which  is
           ignored).

Description

       This module provides the same interface as LWP::UserAgent, for the methods that Net::Duo calls, and
       verifies that the information passed in by Duo is correct.  It can also simulate responses to exercise
       response handling in Net::Duo.  To test Net::Duo, pass a Test::Mock::Duo::Agent object to the constructor
       of a Net::Duo-based class as the user_agent argument.

       All tests are reported by Test::More, and no effort is made to produce a predictable number of test
       results.  This means that any calling test program should probably not specify a plan and instead use
       done_testing().

       This module is primarily used by the Net::Duo test suite and can be ignored entirely when using Net::Duo
       normally.  It is provided as part of the Net::Duo module install, instead of kept only in the
       distribution source tree, because it may be useful for the test suites of other Perl modules or programs
       that use Net::Duo internally and want to test that integration without network access or a live Duo
       account to point to.

Instance Methods

       expect(ARGS)
           Expect  a  REST API call from Net::Duo.  This method can be called multiple times to build up a queue
           of expected requests.

           ARGS is used to specify both the expected request data and the response to return to the caller.  The
           same response is returned regardless of whether the request is correct.

           There are two ways to specify the response: a complete HTTP::Response object, or the JSON data of the
           response.  If only the JSON data is specified, the request will return a response with a status  code
           of  200  and a Duo success result ("stat" of "OK"), with the supplied JSON data as the "response" key
           in the JSON response data.  The content will have a Content-Type of "application/json".

           ARGS should be a reference to a hash with keys selected from the following:

           method
               The expected method of the request.

           uri The expected URI of the request.  This should just be the path,  not  the  hostname  or  protocol
               portions of the full URL, and should not include any GET parameters.

           content
               The  expected content of the request.  This is the parameters in the URL if the method is GET and
               the expected "application/x-www-form-urlencoded" content of the request  for  any  other  request
               type.   It  may  be  empty  or  not  specified  if  the request should not contain any additional
               parameters.

           response
               An HTTP::Response object to return to  the  client.   This  object  is  always  returned  without
               modification to any request, even if it doesn't match the expected request.

           response_data
               A  data  structure that will be converted to JSON and included as the value of the "response" key
               in the returned success response to the client.

           response_file
               A file containing JSON that will be included as the value of the "response" key in  the  returned
               success response to the client.

           next_offset
               Return  paging  metadata  in  the  response,  setting  the  "next_offset"  key to this value, the
               "prev_offset" key to 0, and the "total_objects" key to the value of the total_objects  parameter,
               which  must  be  specified  as  well.   Set this to "undef" to include pagination information but
               without a "next_offset" key.

           total_objects
               Value to return in the "total_objects" key in the paging metadata.

       request(REQUEST)
           This is the interface called internally by Net::Duo to make an API call.  The interface is  the  same
           as   the   request()   method   of   LWP::UserAgent:   REQUEST   is   an  HTTP::Request  object,  and
           Net::Duo::Mock::Agent  will  return  an  HTTP::Response  object.   Currently,  this   is   the   only
           LWP::UserAgent method implemented by this mock, since it's the only one that Net::Duo uses.

           When  request() is called, it checks the content of the request against whatever the mock was told to
           expect via the expect()  method.   The  results  of  that  comparison  are  reported  via  Test::More
           functions.   The expected call is then cleared.  This means that expect() must be called between each
           call to a Net::Duo method that would result in a REST API call request.

           If request() is called when no request was expected (via an expect() call), it throws an exception.

Name

       Net::Duo::Mock::Agent - Mock LWP::UserAgent for Net::Duo testing

Requirements

       Perl 5.14 or later and the modules HTTP::Request and HTTP::Response (part of HTTP::Message), JSON,
       Perl6::Slurp, and URI::Escape (part of URI), all of which are available from CPAN.

See Also

       Net::Duo

       This  module  is  part  of  the Net::Duo distribution.  The current version of Net::Duo is available from
       CPAN, or directly from its web site at <https://www.eyrie.org/~eagle/software/net-duo/>.

perl v5.36.0                                       2022-12-18                         Net::Duo::Mock::Agent(3pm)

Synopsis

           # Build the Net::Duo object and the mock.
           my %args = (key_file => 'admin.json');
           my $mock = Net::Duo::Mock::Agent->new(\%args);
           $args{user_agent} = $mock;
           my $duo = Net::Duo::Admin->new(\%args);

           # Indicate what to expect and then make the Net::Duo call.
           $mock->expect(
               {
                   method        => 'GET',
                   uri           => '/admin/v1/users',
                   response_file => 'response.json',
               }
           );
           my @users = $duo->users;

See Also