<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>thoughts of a doug &#187; testing</title>
	<atom:link href="http://somethingdoug.com/thoughts/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://somethingdoug.com/thoughts</link>
	<description></description>
	<lastBuildDate>Fri, 21 Oct 2011 05:03:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Test::Override::UserAgent</title>
		<link>http://somethingdoug.com/thoughts/2010/01/31/test-override-useragent/</link>
		<comments>http://somethingdoug.com/thoughts/2010/01/31/test-override-useragent/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 02:15:01 +0000</pubDate>
		<dc:creator>Douglas Wilson</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[cpan]]></category>
		<category><![CDATA[Perl5]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://somethingdoug.com/thoughts/?p=153</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<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>
<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>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">package</span> t<span style="color: #339933;">::</span><span style="color: #006600;">CustomUA</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> <span style="color: #cc66cc;">5.008</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Test<span style="color: #339933;">::</span><span style="color: #006600;">Override</span><span style="color: #339933;">::</span><span style="color: #006600;">UserAgent</span> <span style="color: #b1b100;">for</span> <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'configuration'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Disable live requests (the default, anyhow)</span>
allow_live<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
override_request
  host <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'localhost'</span><span style="color: #339933;">,</span>
  path <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'/test'</span><span style="color: #339933;">,</span>
  <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">return</span> <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'content-type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'text/plain'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #ff0000;">'Test response'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span></pre></div></div>

<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>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!perl -T</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> <span style="color: #cc66cc;">5.008</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Test<span style="color: #339933;">::</span><span style="color: #006600;">More</span> tests <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> t<span style="color: #339933;">::</span><span style="color: #006600;">CustomUA</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> My<span style="color: #339933;">::</span><span style="color: #006600;">Module</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># Your awesome module to test</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$object</span> <span style="color: #339933;">=</span> new_ok <span style="color: #ff0000;">'My::Module'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Your module has a way to set the user agent it uses</span>
t<span style="color: #339933;">::</span><span style="color: #006600;">CustomUA</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">configuration</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">install_in_user_agent</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$object</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">user_agent</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Perhaps your module has no way to set the user agent</span>
<span style="color: #666666; font-style: italic;"># or there is something down stream that uses a user agent.</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$scope</span> <span style="color: #339933;">=</span> t<span style="color: #339933;">::</span><span style="color: #006600;">CustomUA</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">configuration</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">install_in_scope</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Do tests that make requests by LWP::UserAgent</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># End the scope override</span>
<span style="color: #000066;">undef</span> <span style="color: #0000ff;">$scope</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://somethingdoug.com/thoughts/2010/01/31/test-override-useragent/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

