Skip to content

Commit

Permalink
make the FakeServer properly parse incoming documents so the HttpVerb…
Browse files Browse the repository at this point in the history
…sTest becomes a real test that actually finds bugs (thanks @nleguen for spotting!).
  • Loading branch information
apotonick committed Jun 22, 2012
1 parent 6bfe80d commit 127dba9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
35 changes: 30 additions & 5 deletions test/fake_server.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
require "bundler/setup"
require 'sinatra/base'
require "sinatra/base"
require "roar/representer/json"

class FakeServer < Sinatra::Base

set :raise_errors, false

module BandRepresenter
include Roar::Representer::JSON

property :name
property :label
end

class Band
attr_reader :name, :label

def name=(value)
@name = value.upcase
end

def label=(value)
@label = value.upcase
end
end

def consume_band
Band.new.extend(BandRepresenter).from_json(request.body.string)
end


get "/method" do
"<method>get</method>"
end
Expand All @@ -31,19 +55,20 @@ class FakeServer < Sinatra::Base

post "/bands" do
#if request.content_type =~ /xml/
body '{"label": "n/a", "name": "Strung Out", "links": [{"href":"http://search", "rel": "search"}, {"href":"http://band/strungout", "rel": "self"}]}'
body consume_band.to_json

status 201
end

put "/bands/strungout" do
# DISCUSS: as long as we don't agree on what to return in PUT/PATCH, let's return an updated document.
{:name => "Strung Out", :label => "Fat Wreck"}.to_json
body consume_band.to_json
#status 204
end

patch '/bands/strungout' do
# DISCUSS: as long as we don't agree on what to return in PUT/PATCH, let's return an updated document.
{:name => 'Strung Out', :label => 'Fat Mike'}.to_json
body consume_band.to_json
#status 204
end

Expand Down
17 changes: 6 additions & 11 deletions test/http_verbs_feature_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
require 'roar/representer/json'

class HttpVerbsTest < MiniTest::Spec
module BandRepresenter
include Roar::Representer::JSON

property :name
property :label
end
BandRepresenter = FakeServer::BandRepresenter

# keep this class clear of Roar modules.
class Band
Expand Down Expand Up @@ -80,8 +75,8 @@ class Band
assert_equal nil, @band.label

@band.post("http://roar.example.com/bands", "application/xml")
assert_equal "Strung Out", @band.name
assert_equal "n/a", @band.label
assert_equal "STRUNG OUT", @band.name
assert_equal nil, @band.label
end
end

Expand All @@ -90,16 +85,16 @@ class Band
@band.name = "Strung Out"
@band.label = "Fat Wreck"
@band.put("http://roar.example.com/bands/strungout", "application/xml")
assert_equal "Strung Out", @band.name
assert_equal "Fat Wreck", @band.label
assert_equal "STRUNG OUT", @band.name
assert_equal "FAT WRECK", @band.label
end
end

describe "#patch" do
it 'does something' do
@band.label = 'Fat Mike'
@band.patch("http://roar.example.com/bands/strungout", "application/xml")
assert_equal 'Fat Mike', @band.label
assert_equal 'FAT MIKE', @band.label
end
end

Expand Down

0 comments on commit 127dba9

Please sign in to comment.