<?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>Sean's Obsessions &#187; Ruby on Rails</title>
	<atom:link href="http://ertw.com/blog/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://ertw.com/blog</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 15 Jul 2010 12:57:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Load testing a Rails app with JMeter and the authenticity_token</title>
		<link>http://ertw.com/blog/2010/06/29/load-testing-a-rails-app-and-the-authenticity_token/</link>
		<comments>http://ertw.com/blog/2010/06/29/load-testing-a-rails-app-and-the-authenticity_token/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 02:50:42 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[jmeter]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[regexp]]></category>

		<guid isPermaLink="false">http://ertw.com/blog/?p=262</guid>
		<description><![CDATA[I have been slowly learning how to use JMeter to load test the Small Payroll application. One of the problems has been getting around the CSRF protection that Rails puts in with the authenticity_token parameter.
Each form has a hidden form element:
&#60;div style="margin:0;padding:0;display:inline">
&#60;input name="authenticity_token" type="hidden"
value="16iUP1J2tdSKyvHKgYR/I/og6K7NgPPmTHCZ+idQP4k=" />
&#60;/div>
The token is also encrypted in the session so that the [...]<p>a</p>
]]></description>
			<content:encoded><![CDATA[<p>I have been slowly learning how to use <a href="http://jakarta.apache.org/jmeter/">JMeter</a> to load test the <a href="http://smallpayroll.ca">Small Payroll</a> application. One of the problems has been getting around the CSRF protection that Rails puts in with the authenticity_token parameter.</p>
<p>Each form has a hidden form element:</p>
<pre><code>&lt;div style="margin:0;padding:0;display:inline">
&lt;input name="authenticity_token" type="hidden"
value="16iUP1J2tdSKyvHKgYR/I/og6K7NgPPmTHCZ+idQP4k=" />
&lt;/div></code></pre>
<p>The token is also encrypted in the session so that the response to the form has to match. This value changes every time the form is loaded and can&#8217;t be set to a known value, otherwise it would be easy to defeat the CSRF protection. This means that JMeter has to figure out the token and put it into the POST.</p>
<p>Googling around, I found some people that said they did it, and a lot of people who couldn&#8217;t get it to work, but no solid walkthroughs.</p>
<p>Here&#8217;s my solution:<br />
<a href="http://ertw.com/blog/wp-content/uploads/2010/06/jmeter-rails-authenticity_token.jpg"><img src="http://ertw.com/blog/wp-content/uploads/2010/06/jmeter-rails-authenticity_token.jpg" alt="" title="jmeter-rails-authenticity_token" width="689" height="651" class="alignnone size-full wp-image-265" /></a></p>
<p>There are three elements. The first is the HTTP request sampler that gets the login page. Under that is a regular expression extractor post processor that gets the authenticity token. The extractor uses a simple regexp to pull out the value parameter and saves it to the LOGIN_AUTH_TOKEN variable. The login is then done by making reference to the variable &#8211; ${LOGIN_AUTH_TOKEN}. <b>Make sure you have the Encode? button checked</b>, as the authenticity_token is not always base-64 friendly!</p>
<p>The final step, not pictured, is that you have an HTTP Cookie Manager in your thread group to take care of cookies. You probably already have one, though.</p>
<p>Sean</p>
<p>a</p>
]]></content:encoded>
			<wfw:commentRss>http://ertw.com/blog/2010/06/29/load-testing-a-rails-app-and-the-authenticity_token/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Shoulda, nested contexts, and should_change_by</title>
		<link>http://ertw.com/blog/2009/09/03/shoulda-nested-contexts-and-should_change_by/</link>
		<comments>http://ertw.com/blog/2009/09/03/shoulda-nested-contexts-and-should_change_by/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 01:24:32 +0000</pubDate>
		<dc:creator>sean</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://ertw.com/blog/?p=223</guid>
		<description><![CDATA[I love some of shoulda&#8217;s macros, partially because it forces me to think about my tests and put them in setup blocks, which ends up making things cleaner.
So I ran into a case where I do an action and use should_change to make sure that a certain number of rows were added to a table, [...]<p>a</p>
]]></description>
			<content:encoded><![CDATA[<p>I love some of <a href="http://thoughtbot.com/projects/shoulda">shoulda&#8217;s</a> macros, partially because it forces me to think about my tests and put them in setup blocks, which ends up making things cleaner.</p>
<p>So I ran into a case where I do an action and use should_change to make sure that a certain number of rows were added to a table, then I do it again and make sure they don&#8217;t change. So I used a nested set of contexts to handle this:</p>
<p><code>
<pre>
context "do it once" do
  setup do
    post :method, :foo => {...}
  end
  should_change("something", :by => 6) {Something.count}
  #... other tests ...
  context "do it a second time" do
     setup do
       post :method, :foo => {...}
     end
     # should it change by 6 or 0?
  end
end
</pre>
<p></code></p>
<p>I wasn&#8217;t sure if the should_change applied to both the setup blocks or only the second. Turns out it&#8217;s only the second, so it should not change in the second test. Here&#8217;s proof:</p>
<p><code>
<pre>
require 'test_helper'

class SomethingControllerTest < ActionController::TestCase

  context "outer" do
    setup do
      @user = Factory.create(:valid_user)
    end
    should_change("number of users", :by => 1) { User.count }
    context "inner" do
      setup do
      end
      should_change("number of users", :by => 0) { User.count }
    end
  end
end
</pre>
<p></code></p>
<p>Since I didn&#8217;t write my tests before I wrote the code I wasn&#8217;t sure if my code was bad or my test was. Now I know how the test could be structured and I could test properly.</p>
<p>a</p>
]]></content:encoded>
			<wfw:commentRss>http://ertw.com/blog/2009/09/03/shoulda-nested-contexts-and-should_change_by/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
