Skip to content

Commit

Permalink
Fix a bug when ActiveRecord is loaded together with Broach.
Browse files Browse the repository at this point in the history
The JSON gem and ActiveRecord accept different arguments on the to_json
method. This breaks when loading both and calling JSON.dump or JSON.generate
but not the calling object.to_json for some reason.
  • Loading branch information
Manfred committed Dec 17, 2009
1 parent 4149643 commit 099a17f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
23 changes: 10 additions & 13 deletions Rakefile
@@ -1,26 +1,23 @@
require 'rake/rdoctask' require 'rake/rdoctask'


desc "Run all specs by default" TEST_CATEGORIES = [:remote, :interaction, :unit]

desc "Run all unit specs by default"
task :default => 'test:unit' task :default => 'test:unit'


namespace :test do namespace :test do
desc "Run all remote tests" TEST_CATEGORIES.each do |category|
task :remote do desc "Run all #{category} tests"
Dir[File.dirname(__FILE__) + '/test/remote/*_test.rb'].each do |file| task category do
load file Dir[File.dirname(__FILE__) + "/test/#{category}/*_test.rb"].each do |file|
end load file
end end

desc "Run all unit tests"
task :unit do
Dir[File.dirname(__FILE__) + '/test/unit/*_test.rb'].each do |file|
load file
end end
end end
end end


desc "Run all specs" desc "Run all specs"
task :test => ['test:unit', 'test:remote'] task :test => TEST_CATEGORIES.map { |category| "test:#{category}" }


namespace :gem do namespace :gem do
desc "Build the gem" desc "Build the gem"
Expand Down
6 changes: 3 additions & 3 deletions lib/broach/session.rb
Expand Up @@ -39,7 +39,7 @@ def credentials
def get(path) def get(path)
response = REST.get(url_for(path), headers_for(:get), credentials) response = REST.get(url_for(path), headers_for(:get), credentials)
if response.ok? if response.ok?
return JSON.parse(response.body) JSON.parse(response.body)
else else
handle_response(:get, path, response) handle_response(:get, path, response)
end end
Expand All @@ -48,9 +48,9 @@ def get(path)
# Posts a resource to a certain path on the server. When the POST is successful # Posts a resource to a certain path on the server. When the POST is successful
# the parsed body is returned, otherwise an exception is raised. # the parsed body is returned, otherwise an exception is raised.
def post(path, payload) def post(path, payload)
response = REST.post(url_for(path), JSON.dump(payload), headers_for(:post), credentials) response = REST.post(url_for(path), payload.to_json, headers_for(:post), credentials)
if response.created? if response.created?
return JSON.parse(response.body) JSON.parse(response.body)
else else
handle_response(:post, path, response) handle_response(:post, path, response)
end end
Expand Down
19 changes: 19 additions & 0 deletions test/interaction/active_support_test.rb
@@ -0,0 +1,19 @@
require File.expand_path('../../start', __FILE__)

require 'open3'
require 'rbconfig'
module InteractionSpecHelpers
def ruby(script)
ruby_bin = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
stdin, stdout, stderr = Open3.popen3("#{ruby_bin} #{script}")
stderr.read
end
end

describe "Broach, when ActiveSupport is loaded" do
extend InteractionSpecHelpers

it "should say hi in a room" do
ruby(File.expand_path('../scripts/say_hi.rb', __FILE__)).should == ''
end
end
9 changes: 9 additions & 0 deletions test/interaction/scripts/say_hi.rb
@@ -0,0 +1,9 @@
require 'rubygems' rescue nil
require 'active_support'
require 'rest'

$:.unshift File.expand_path('../../../../lib', __FILE__)
require 'broach'

Broach.settings = YAML.load_file(File.expand_path('../../../../settings.yml', __FILE__))
Broach.speak(Broach.session.room, "Hi!")

0 comments on commit 099a17f

Please sign in to comment.