tadam/Test-Mock-LWP-Dispatch — pull request #3 by antipasta (2013-05-28 02:40:30+00:00)
Title: Mocked requests do not contain default headers normally set by LWP::UserAgent

Hi,

I've been using your module quite a bit when testing code built around LWP::UserAgent. I usually test using the ->map($site, $coderef) method, and in the $coderef I test against the HTTP::Request object that is passed into it, to make sure that it has everything I expect it to.

Today I noticed that authorization headers were missing from the HTTP::Request objects that were being created when doing $lwp->get() when using Test::Mock::LWP::Dispatch.

It took a little digging into LWP::UserAgent, but I realized that 'default' headers are added to requests by prepare_request(), which is called by send_request() in LWP::UserAgent.

Since your module mocks simple_request, send_request() is never called, so prepare_request() is also never called.

The test case I've included better illustrates what I'm talking about. It fails against master currently.

I didn't want to break backwards compatibility by making drastic changes, so this pull request just adds a flag that calls prepare_request() against any HTTP::Request objects passed into your module's simple_request function. So when this flag is set to 1 in a test case, it fixes the issues I was running into.

Let me know if you have any about the change, or suggestions on how it can be improved upon so that it can be merged.

Joe Papperello