Using Rack middleware to parse JSON

In attempting to AJAX-ize the site, I had the desire to handle JSON as if it were form post data. Queue a Rack middleware solution. rack-contrib contains a bunch of common middleware extensions, one being the horribly named PostBodyContentTypeParser. To get this working I added:

1
require 'rack/contrib'

with all of the rest of the required files.

Added:

1
use Rack::PostBodyContentTypeParser

to my application class

And went about over testing it like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def test_json_creates_params_hash
  params_hash={"user"=>{"username"=>"testuser","email"=>"test@test.com","password"=>"pass1","password_confirmation"=>"pass1"}}
  post '/test_json', params_hash
  assert !last_request.params.blank?
  assert_equal params_hash, last_request.params
  assert last_response.ok?
  json_string="{\"user\":{\"password_confirmation\":\"pass1\",\"username\":\"testuser\",\"password\":\"pass1\",\"email\":\"test@test.com\"}}"
  post '/test_json', JSON(json_string)
  assert !last_request.params.blank?
  assert_equal params_hash, last_request.params
  assert last_response.ok?
  post '/test_json', json_string, "CONTENT_TYPE"=>"application/json"
  assert_equal last_request.env["CONTENT_TYPE"], "application/json"
  assert !last_request.params.blank?
  assert_equal params_hash, last_request.params
  assert last_response.ok?
end
Posted: March 30th, 2010 | Author: | Filed under: Code | Tags: , , , , , , , | 1 Comment »

Battling autotest

Looking for a reason that autotest is skipping some of my test files….

UPDATE: Found the culprit. In my .autotest file I had some exclusions which were matching on the log in blog

1
2
3
4
5
Autotest.add_hook :initialize do |autotest|
  %w{.git .svn .hg .DS_Store db log tmp vendor ._* .sqlite3}.each do |exception| 
    autotest.add_exception(exception) 
  end
end

I think I’ll just comment the exception line out for now as I don’t think it’s necessary.

Posted: March 27th, 2010 | Author: | Filed under: Code | Tags: , , | No Comments »

Rack::Test and request / response objects

Rack::Test uses last_response and last_request objects instead of Rack’s typical request and response objects. This is probably normally fine, but when you are testing functionality that requires accessing the Rack’s normal objects, they aren’t there. I found (in the comments section of this post) that you can fix this by overriding them in your test_helper.rb:

1
2
3
4
5
6
7
8
9
10
11
12
module Test::Unit
  class TestCase
    include Rack::Test::Methods
    ...
 
    def request(*args)
      args.empty? ? last_request : rack_test_session.request(*args)
    end
 
    ...
  end
end
Posted: March 26th, 2010 | Author: | Filed under: Code | Tags: , , , , , , , , , | No Comments »

Including common routines in test_helper.rb

I should have done this earlier. To do a number of tests, I have to repeat the same actions like create a user, log a user in, etc. I was putting these actions at the top of each test file, but have wised up and moved them to the test_helper.rb file. Here’s how that looks now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
module Test::Unit
  class TestCase
    ...
 
    def create_user(user_hash={})
      post '/user/signup', {:user=>{:username=>"testuser",:email=>"test@test.com",:password=>"pass1",:password_confirmation=>"pass1"}.merge(user_hash)}
    end
 
    def login(user_hash={})
      post "/user/login", :user=>{:username_or_email=>'testuser',:password=>'pass1'}.merge(user_hash)
    end
 
  end
end
Posted: March 24th, 2010 | Author: | Filed under: Code | Tags: , , , , | 1 Comment »

Testing helpers in Sinatra

How do you test your helper methods in a Sinatra::Base app?

1
2
3
4
5
6
7
module FundAStache
  module Helpers    
    def logged_in?
      false # you shall not pass...
    end
  end
end

Include them in your tests…

1
2
3
4
class FundastacheUserTest < Test::Unit::TestCase
  include Rack::Test::Methods
  include FundAStache::Helpers
  ...
Posted: March 9th, 2010 | Author: | Filed under: Code | Tags: , , , , , | No Comments »