locked_user sundialsvc4 has asked for the wisdom of the Perl Monks concerning the following question:

An app has to talk to a web-service (the usual REST and JSON) that doesn’t provide for a sandbox.   The actual HTTP exchange does not need to be tested, merely that a set of “canned” known-good responses are in fact parsed correctly.   (For a URL-string that looks like this, the response should be (one-of-these).)   Without creating two programs that now have to be debugged, how might you go about doing that in the context of a .t test?

Replies are listed 'Best First'.
Re: How would you mock calls to a (third-party) web-service?
by Corion (Patriarch) on May 28, 2013 at 18:20 UTC

    LWP::UserAgent can conveniently read file:// URLs, so at least the REST part is easily mocked out by accessing file:// URLs instead of setting up a HTTP server, at least as long as you can make the URLs look something like a file system.

    Otherwise, I would use something like Test::HTTP::LocalServer, which echoes you the URL back, and mock/retrieve the canned content separately.

Re: How would you mock calls to a (third-party) web-service?
by locked_user sundialsvc4 (Abbot) on May 28, 2013 at 20:25 UTC

    Meditating on Test::LWP::UserAgent and mod:://Test::Mock::LWP, trending toward the former.   Humbly petitioning for the wisdom of the Wise, that ye may bequeath upon this penitent the benefit of thy Hard Knocks ...   ;-)