Commit ed104af7 authored by John E. Vincent's avatar John E. Vincent

specs for ephemeral nodes

parent 5b7c9c25
require 'logger'
require 'base64'
class Noah::App
# Stubbing Ephemeral endpoints
get '/e/?' do
......@@ -7,9 +5,10 @@ class Noah::App
end
get '/e/*' do
logger = Logger.new(STDOUT)
params["splat"].size == 0 ? (halt 404) : (e=Noah::Ephemeral.find(:path => "/#{params["splat"][0]}").first)
return e.to_json
(halt 404) if e.nil?
content_type "application/octet-stream"
e.data.nil? ? "" : "#{e.data}"
end
put '/e/*/watch' do
......@@ -22,14 +21,21 @@ class Noah::App
put '/e/*' do
raise("Data too large") if request.body.size > 512
d = Base64.encode64(request.body.read) || nil
e = Noah::Ephemeral.new(:path => params[:splat][0], :data => d)
e.valid? ? (e.save; e.to_json) : (raise "#{format_errors(e)}")
d = request.body.read || nil
e = Noah::Ephemeral.new(:path => "/#{params[:splat][0]}", :data => d)
if e.valid?
e.save
action = e.is_new? ? "create" : "update"
r = {"action" => action, "result" => "success", "id" => e.id, "path" => e.path, "data" => e.data}
r.to_json
else
raise "#{format_errors(e)}"
end
end
delete '/e/*' do
p = params[:splat][0]
e = Noah::Ephemeral.find(:path => p).first
e = Noah::Ephemeral.find(:path => "/"+p).first
if e
e.delete
r = {"result" => "success", "id" => "#{e.id}", "action" => "delete", "path" => e.name}
......
require 'base64'
module Noah
class Ephemeral < Model #NYI
......@@ -19,8 +18,7 @@ module Noah
end
def to_hash
data.nil? ? d=nil : d=Base64.decode64(data)
h = {:path => path, :data => d, :created_at => created_at, :updated_at => :updated_at}
h = {:path => path, :data => data, :created_at => created_at, :updated_at => :updated_at}
super.merge(h)
end
protected
......
......@@ -37,6 +37,7 @@ Gem::Specification.new do |s|
s.add_development_dependency("warbler", ["= 1.2.1"])
else
s.add_dependency("yajl-ruby")
s.add_dependency("SystemTimer")
s.add_dependency("thin")
end
......
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Ephemeral API", :reset_redis => true do
before(:each) do
Ohm.redis.flushdb
Noah::Ephemeral.create(:path => "/foo/bar/baz", :data => "value1")
Noah::Ephemeral.create(:path => "/baz/bar")
end
after(:each) do
Ohm.redis.flushdb
end
describe "calling" do
describe "GET" do
it "all ephemerals should return 404" do
get '/e'
last_response.should_not be_ok
last_response.status.should == 404
response = last_response.should return_json
response['error_message'].should == 'Resource not found'
response['result'].should == 'failure'
end
it "named path with data should work" do
get '/e/foo/bar/baz'
last_response.should be_ok
last_response.body.should == 'value1'
end
it "named path without data should work" do
get '/e/baz/bar'
last_response.status.should == 200
last_response.body.should == ""
end
it "invalid path should not work" do
get '/e/ssss/dddd'
last_response.should_not be_ok
last_response.status.should == 404
response = last_response.should return_json
response['error_message'].should == 'Resource not found'
response['result'].should == 'failure'
end
end
describe "PUT" do
it "new ephemeral with data should work" do
put '/e/whiz/bang/', 'value3'
last_response.should be_ok
response = last_response.should return_json
response['result'].should == 'success'
response['id'].nil?.should == false
response['path'].should == '/whiz/bang/'
response['data'].should == 'value3'
end
it "new ephemeral without data should work" do
put '/e/bang/whiz'
last_response.should be_ok
response = last_response.should return_json
response['result'].should == 'success'
response['action'].should == 'create'
response['id'].nil?.should == false
response['path'].should == '/bang/whiz'
response['data'].should == nil
end
it "existing ephemeral with data should work"
it "existing ephemeral without data should work"
end
describe "DELETE" do
it "existing path should work" do
e = Noah::Ephemeral.new(:path => '/slart/i/bart/fast', :data => 'someddata')
e.save
delete "/e/slart/i/bart/fast"
last_response.should be_ok
response = last_response.should return_json
response['result'].should == 'success'
response['action'].should == 'delete'
response['id'].should == e.id
response['path'].should == e.name
end
it "invalid path should not work" do
delete '/e/fork/spoon/knife'
last_response.should_not be_ok
last_response.status.should == 404
response = last_response.should return_json
response['error_message'].should == 'Resource not found'
response['result'].should == 'failure'
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment