Skip to content

Commit

Permalink
Use sham_rack to test Sinatra app with Net::HTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
miyagawa authored and apotonick committed Mar 22, 2012
1 parent 68b9c58 commit ff2cff9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions roar.gemspec
Expand Up @@ -25,4 +25,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "test_xml"
s.add_development_dependency "minitest", ">= 2.8.1"
s.add_development_dependency "sinatra", "~> 1.2.6"
s.add_development_dependency "sham_rack", "~> 1.3.0"
end
7 changes: 3 additions & 4 deletions test/fake_server.rb
Expand Up @@ -36,13 +36,13 @@ class FakeServer < Sinatra::Base
end


require Dir.pwd + '/order_representers'
require './test/order_representers'
JSON::Order.class_eval do
def items_url
"http://localhost:9999/orders/1/items"
"http://roar.example.com/orders/1/items"
end
def order_url(order)
"http://localhost:9999/orders/#{order}"
"http://roar.example.com/orders/#{order}"
end
def represented
1
Expand Down Expand Up @@ -72,4 +72,3 @@ def represented

end

FakeServer.run! :host => 'localhost', :port => 9999
8 changes: 4 additions & 4 deletions test/http_verbs_feature_test.rb
Expand Up @@ -32,15 +32,15 @@ class Band
include Roar::Representer::Feature::HttpVerbs
attr_accessor :name, :label
end
@band = @Band.get("http://localhost:9999/bands/slayer", "application/json")
@band = @Band.get("http://roar.example.com/bands/slayer", "application/json")
assert_equal "Slayer", @band.name
assert_equal "Canadian Maple", @band.label
end
end

describe "#get" do
it "updates instance with incoming representation" do
@band.get("http://localhost:9999/bands/slayer", "application/json")
@band.get("http://roar.example.com/bands/slayer", "application/json")
assert_equal "Slayer", @band.name
assert_equal "Canadian Maple", @band.label
end
Expand All @@ -51,7 +51,7 @@ class Band
@band.name = "Strung Out"
assert_equal nil, @band.label

@band.post("http://localhost:9999/bands", "application/xml")
@band.post("http://roar.example.com/bands", "application/xml")
assert_equal "Strung Out", @band.name
assert_equal "n/a", @band.label
end
Expand All @@ -61,7 +61,7 @@ class Band
it "updates instance with incoming representation" do
@band.name = "Strung Out"
@band.label = "Fat Wreck"
@band.put("http://localhost:9999/bands/strungout", "application/xml")
@band.put("http://roar.example.com/bands/strungout", "application/xml")
assert_equal "Strung Out", @band.name
assert_equal "Fat Wreck", @band.label
end
Expand Down
7 changes: 7 additions & 0 deletions test/test_helper.rb
Expand Up @@ -33,3 +33,10 @@ class Order

require "test_xml/mini_test"
require "roar/representer/xml"

require 'sham_rack'
require './test/fake_server'

ShamRack.at('roar.example.com').rackup do
run FakeServer
end
10 changes: 5 additions & 5 deletions test/transport_test.rb
Expand Up @@ -8,24 +8,24 @@ class TransportTest < MiniTest::Spec
end

it "#get_uri returns response" do
assert_equal "<method>get</method>", @transport.get_uri("http://localhost:9999/method", "application/xml").body
assert_equal "<method>get</method>", @transport.get_uri("http://roar.example.com/method", "application/xml").body
end

it "#post_uri returns response" do
assert_equal "<method>post</method>", @transport.post_uri("http://localhost:9999/method", "booty", "application/xml").body
assert_equal "<method>post</method>", @transport.post_uri("http://roar.example.com/method", "booty", "application/xml").body
end

it "#put_uri returns response" do
assert_equal "<method>put</method>", @transport.put_uri("http://localhost:9999/method", "booty", "application/xml").body
assert_equal "<method>put</method>", @transport.put_uri("http://roar.example.com/method", "booty", "application/xml").body
end

it "#delete_uri returns response" do
assert_equal "<method>delete</method>", @transport.delete_uri("http://localhost:9999/method", "application/xml").body
assert_equal "<method>delete</method>", @transport.delete_uri("http://roar.example.com/method", "application/xml").body
end

# TODO: how to get PATCH into Sinatra?
#it "#patch_uri returns Restfulie response" do
# assert_equal "<method>patch</method>", @o.patch_uri("http://localhost:9999/method", "booty", "application/xml").body
# assert_equal "<method>patch</method>", @o.patch_uri("http://roar.example.com/method", "booty", "application/xml").body
#end
end
end

0 comments on commit ff2cff9

Please sign in to comment.