{"id":153,"date":"2010-01-31T21:15:01","date_gmt":"2010-02-01T02:15:01","guid":{"rendered":"http:\/\/somethingdoug.com\/thoughts\/?p=153"},"modified":"2010-01-31T23:41:44","modified_gmt":"2010-02-01T04:41:44","slug":"test-override-useragent","status":"publish","type":"post","link":"https:\/\/somethingdoug.com\/thoughts\/2010\/01\/31\/test-override-useragent\/","title":{"rendered":"Test::Override::UserAgent"},"content":{"rendered":"<p>I have just uploaded a new module to the CPAN: <a href=\"http:\/\/search.cpan.org\/dist\/Test-Override-UserAgent\/\">Test::Override::UserAgent<\/a>. This is geared toward developers that use <a href=\"http:\/\/search.cpan.org\/perldoc?LWP::UserAgent\">LWP::UserAgent<\/a> in their modules and want to easily test their code without having a live Internet connection or starting up a test server to make requests against. Just about every single module I have on the CPAN uses LWP::UserAgent and makes requests against live servers (usually they are doing web scraping). Before I was using <a href=\"http:\/\/search.cpan.org\/perldoc?Test::MockObject\">Test::MockObject<\/a>, but that was tedious at best.<\/p>\n<p>The goal of Test::Override::UserAgent is to be able to write response overrides easily. The module allows for the author to create a class consisting of only definitions for overrides so the override definitions do not have to be repeated in each test file. An example override class would look like:<\/p>\n<pre lang=\"perl\">package t::CustomUA;\r\n\r\nuse 5.008;\r\nuse Test::Override::UserAgent for => 'configuration';\r\n\r\n# Disable live requests (the default, anyhow)\r\nallow_live(0);\r\n\r\noverride_request\r\n  host => 'localhost',\r\n  path => '\/test',\r\n  sub {\r\n    return [200, ['content-type' => 'text\/plain'], ['Test response']];\r\n  };\r\n\r\n1;<\/pre>\n<p>This will cause requests that have a host of <code>localhost<\/code> and a path of <code>\/test<\/code> to get a response back of <code>text\/plain<\/code> with the <code>Test response<\/code> body. This configuration can then be used in a test file:<\/p>\n<pre lang=\"perl\">#!perl -T\r\n\r\nuse 5.008;\r\nuse Test::More tests => 1;\r\nuse t::CustomUA;\r\n\r\nuse My::Module; # Your awesome module to test\r\n\r\nmy $object = new_ok 'My::Module';\r\n\r\n# Your module has a way to set the user agent it uses\r\nt::CustomUA->configuration->install_in_user_agent($object->user_agent);\r\n\r\n# Perhaps your module has no way to set the user agent\r\n# or there is something down stream that uses a user agent.\r\nmy $scope = t::CustomUA->configuration->install_in_scope;\r\n\r\n# Do tests that make requests by LWP::UserAgent\r\n\r\n# End the scope override\r\nundef $scope;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have just uploaded a new module to the CPAN: Test::Override::UserAgent. This is geared toward developers that use LWP::UserAgent in their modules and want to easily test their code without having a live Internet connection or starting up a test server to make requests against. Just about every single module I have on the CPAN [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[12,27,19,23],"class_list":["post-153","post","type-post","status-publish","format-standard","hentry","category-perl","tag-cpan","tag-perl","tag-perl5","tag-testing"],"_links":{"self":[{"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/posts\/153","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/comments?post=153"}],"version-history":[{"count":6,"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/posts\/153\/revisions"}],"predecessor-version":[{"id":159,"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/posts\/153\/revisions\/159"}],"wp:attachment":[{"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/media?parent=153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/categories?post=153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/somethingdoug.com\/thoughts\/wp-json\/wp\/v2\/tags?post=153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}