Created
September 5, 2012 15:37
-
-
Save oestrich/3638605 to your computer and use it in GitHub Desktop.
WebMachine Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'webmachine' | |
require 'webmachine/adapters/rack' | |
require 'json' | |
class Order | |
attr_accessor :id, :email, :date | |
DB = {} | |
def self.all | |
DB.values | |
end | |
def self.find(id) | |
DB[id] | |
end | |
def self.next_id | |
DB.keys.max.to_i + 1 | |
end | |
def self.delete_all | |
DB.clear | |
end | |
def to_json(options = {}) | |
%{{"email":"#@email", "date":"#@date", "id":#@id}} | |
end | |
def initialize(attrs = {}) | |
attrs.each do |attr, value| | |
send("#{attr}=", value) if respond_to?(attr) | |
end | |
end | |
def save(id = nil) | |
self.id = id || self.class.next_id | |
DB[self.id] = self | |
end | |
def destroy | |
DB.delete(id) | |
end | |
end | |
class JsonResource < Webmachine::Resource | |
def content_types_provided | |
[["application/json", :to_json]] | |
end | |
def content_types_accepted | |
[["application/json", :from_json]] | |
end | |
private | |
def params | |
JSON.parse(request.body.to_s) | |
end | |
end | |
class OrdersResource < JsonResource | |
def allowed_methods | |
["GET", "POST"] | |
end | |
def to_json | |
{ | |
:orders => Order.all | |
}.to_json | |
end | |
def create_path | |
@id = Order.next_id | |
"/orders/#@id" | |
end | |
def post_is_create? | |
true | |
end | |
private | |
def from_json | |
order = Order.new(params).save(@id) | |
end | |
end | |
class OrderResource < JsonResource | |
def allowed_methods | |
["GET", "DELETE", "PUT"] | |
end | |
def id | |
request.path_info[:id].to_i | |
end | |
def delete_resource | |
Order.find(id).destroy | |
true | |
end | |
def to_json | |
order = Order.find(id) | |
order.to_json | |
end | |
private | |
def from_json | |
order = Order.new(params) | |
order.save(id) | |
end | |
end | |
App = Webmachine::Application.new do |app| | |
app.routes do | |
add ["orders"], OrdersResource | |
add ["orders", :id], OrderResource | |
add ['trace', '*'], Webmachine::Trace::TraceResource | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "app" | |
Order.new(:email => "eric@example.com", :date => Date.parse("2012-09-04")).save | |
Order.new(:email => "eric+second@example.com", :date => Date.parse("2012-09-06")).save | |
App.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source :rubygems | |
gem 'webmachine' | |
gem 'rspec' | |
gem 'rspec_api_documentation' | |
gem 'rack-test' | |
gem 'json_spec' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GEM | |
remote: http://rubygems.org/ | |
specs: | |
activesupport (3.2.8) | |
i18n (~> 0.6) | |
multi_json (~> 1.0) | |
addressable (2.3.2) | |
crack (0.3.1) | |
diff-lcs (1.1.3) | |
i18n (0.6.1) | |
json (1.7.5) | |
json_spec (1.0.3) | |
multi_json (~> 1.0) | |
rspec (~> 2.0) | |
multi_json (1.3.6) | |
mustache (0.99.4) | |
rack (1.4.1) | |
rack-test (0.6.1) | |
rack (>= 1.0) | |
rspec (2.11.0) | |
rspec-core (~> 2.11.0) | |
rspec-expectations (~> 2.11.0) | |
rspec-mocks (~> 2.11.0) | |
rspec-core (2.11.1) | |
rspec-expectations (2.11.2) | |
diff-lcs (~> 1.1.3) | |
rspec-mocks (2.11.2) | |
rspec_api_documentation (0.8.0) | |
activesupport (>= 3.0.0) | |
i18n (>= 0.1.0) | |
json (>= 1.4.6) | |
mustache (>= 0.99.4) | |
rspec (>= 2.6.0) | |
webmock (>= 1.7.0) | |
webmachine (1.0.0) | |
i18n (>= 0.4.0) | |
multi_json | |
webmock (1.8.9) | |
addressable (>= 2.2.7) | |
crack (>= 0.1.7) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
json_spec | |
rack-test | |
rspec | |
rspec_api_documentation | |
webmachine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
require 'rspec_api_documentation/dsl' | |
resource "Orders" do | |
header "Accept", "application/json" | |
header "Content-Type", "application/json" | |
get "/orders" do | |
example "Getting all orders" do | |
do_request | |
response_body.should be_json_eql({ | |
:orders => [ | |
{ | |
:email => "eric@example.com", | |
:date => "2012-09-04" | |
}, | |
{ | |
:email => "eric+second@example.com", | |
:date => "2012-09-06" | |
} | |
] | |
}.to_json) | |
status.should == 200 | |
end | |
end | |
post "/orders" do | |
parameter :email, "Email address" | |
parameter :date, "Date of order" | |
let(:email) { "eric+frompost@example.com" } | |
let(:date ) { "2012-08-19" } | |
let(:raw_post) { params.to_json } | |
example "Creating an order" do | |
do_request | |
response_body.should == "" | |
response_headers["Location"].should =~ /\/orders\/3$/ | |
status.should == 201 | |
end | |
end | |
get "/orders/:id" do | |
let(:id) { Order.all.first.id } | |
example "Viewing a single order" do | |
do_request | |
response_body.should be_json_eql({ | |
:email => "eric@example.com", | |
:date => "2012-09-04" | |
}.to_json) | |
status.should == 200 | |
end | |
end | |
delete "/orders/:id" do | |
let(:id) { Order.all.first.id } | |
example "Deleting an order" do | |
do_request | |
response_body.should == "" | |
status.should == 204 | |
end | |
end | |
put "/orders/:id" do | |
parameter :email, "Email address" | |
parameter :date, "Date" | |
let(:order) { Order.all.first } | |
let(:id) { order.id } | |
let(:email) { order.email } | |
let(:date) { "2012-08-10" } | |
let(:raw_post) { params.to_json } | |
example "Updating an order" do | |
do_request | |
response_body.should == "" | |
status.should == 204 | |
client.get("/orders/#{id}") | |
response_body.should be_json_eql({ | |
:email => "eric@example.com", | |
:date => "2012-08-10" | |
}.to_json) | |
status.should == 200 | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './app' | |
require 'rspec_api_documentation' | |
require 'json_spec' | |
RspecApiDocumentation.configure do |config| | |
config.app = Webmachine::Adapters::Rack.new(App.configuration, App.dispatcher) | |
end | |
RSpec.configure do |config| | |
config.include JsonSpec::Helpers | |
config.before do | |
Order.delete_all | |
Order.new(:email => "eric@example.com", :date => Date.parse("2012-09-04")).save | |
Order.new(:email => "eric+second@example.com", :date => Date.parse("2012-09-06")).save | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use the rack adapter in the current version, the following bits are required:
in app
and in spec_helper
Then boot could be like:
Webmachine.application.run