Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Mocking request headers with Test::WWW::Mechanize?

by Anonymous Monk
on Oct 25, 2021 at 14:02 UTC ( [id://11138015]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do I mock a request header when testing an app with Test::WWW::Mechanize?

My use case: I have a Catalyst app that, in one case, needs to grab a value from the hostname that's sending an API request. (Requests can come from something like forum_name.example.com and I need to extract "forum_name".)

How do I test this? When I use Test::WWW::Mechanize, all requests come from "localhost", but I need to try goodname.localhost to make sure that works, badname.localhost so I can reject an unconfigured name, etc.

Replies are listed 'Best First'.
Re: Mocking request headers with Test::WWW::Mechanize?
by Corion (Patriarch) on Oct 25, 2021 at 14:10 UTC

    Maybe you can set the Host header using the ->add_header( Host => 'my.example.com') method from WWW::Mechanize? This should add/remove headers sent with every request.

      Wow! Yes, that was easy enough. I assumed add_header was only for additional headers, not for modifying the "core" headers. But that did it exactly. Thank you!
Re: Mocking request headers with Test::WWW::Mechanize?
by perlfan (Vicar) on Oct 25, 2021 at 15:26 UTC
    If you're grabbing just HTML, I know you know HTTP::Tiny is nice lite'r option. I've never actually needed to use Mechanize - though always wanted to. I seem to have gone from just parsing HTML or JSON with LWP or HTTP::Tiny to using something like Playwright or Firefox::Marionette for interaction. Web::Scraper is super handy here. I know the "dream" is to have browserless browser objects, but frankly I don't know how this kind of thing can be sustainable - I think it's a miracle Mechanize still exists.
      Well, in this case, everything is already set up with an elaborate structure and thousands of Mechanize-based tests, so the issue was solely how to do this one thing. I'll leave the restructuring of the entire app to the guy who comes after me :-)
        Indeed - like I said, I've always wanted to use Machanize, so now I'm just showing my envy. Dealing with "real" browsers is about as satisfying as one can suspect.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11138015]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 23:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found