Skip to content

Commit

Permalink
start new way to do integration test with rails app proxying the restbuy
Browse files Browse the repository at this point in the history
xml/json
  • Loading branch information
cairesvs committed Dec 29, 2010
1 parent 3dccaef commit 7c4ec1e
Show file tree
Hide file tree
Showing 79 changed files with 22,439 additions and 160 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.swp
.rvmrc
24 changes: 12 additions & 12 deletions client/lib/restfulie.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ var Restfulie = {};
};

// Default accepts should add all known media types
for (var format in Converters.mediaTypes)
for (var format in Converters.mediaTypes){
if ("register getMediaType".indexOf(format)==-1)
this.headers["Accept"] += (this.headers["Accept"]=='' ? '':', ') +format;

}

// configure accept
this.accepts = function(accept){
Expand Down Expand Up @@ -131,7 +131,7 @@ var Restfulie = {};
return addResponseXHR(resource,xhr);
},
"200" : function(xhr,entryPoint){
var resource = {};
var resource = {};
return addResponseXHR(resource, xhr);
},
"201" : function(xhr,entryPoint){
Expand All @@ -142,17 +142,17 @@ var Restfulie = {};
}

function getResponseHeadersFrom(xhr) {
headers = {};
headerLinks = {};
responseHeaders = xhr.getAllResponseHeaders().split("\n");
var headers = {};
var headerLinks = {};
var responseHeaders = xhr.getAllResponseHeaders().split("\n");
for(headerName in responseHeaders){
headerData = responseHeaders[headerName].split(':');
header = headerData[0];
value = headerData[1];
var headerData = responseHeaders[headerName].split(':');
var header = headerData[0];
var value = headerData[1];
if (header == 'Link'){
linkData = value.split(";");
url = linkData[0].trim().replace(/<|>/g, '');
rel = linkData[1].split('=')[1].replace(/"+/g, '').trim();
var linkData = value.split(";");
var url = linkData[0].trim().replace(/<|>/g, '');
var rel = linkData[1].split('=')[1].replace(/"+/g, '').trim();
headerLinks[rel] = Restfulie.at(url).accepts(xhr.getResponseHeader("Content-Type").split(";")[0]);
}else{
if (value){
Expand Down
152 changes: 13 additions & 139 deletions client/spec/unit/spec.js
Original file line number Diff line number Diff line change
@@ -1,140 +1,14 @@

describe 'Restfulie for javascript'
describe 'core'
it 'should unmarshall when response was successful'
r = Restfulie.at("http://localhost:3000/items").accepts('application/xml').get();
r.items.item[0].price.should.equal 10
r.response.body.should.not.be_null
r.response.code.should.equal 200
end

it 'should work with 4** error codes'
r = Restfulie.at("http://localhost:3000/notfound").get();
r.response.code.should.equal 404
end

it 'should follow 201 responses'
r = Restfulie.at("http://localhost:3000/items/1").get();
r.item.price = 20;
result = Restfulie.at("http://localhost:3000/items").post(r);
result.item.id.should.not.equal r.item.id
end

it 'should allow media type registration through a registry'
type = {
marshal : function(object){},
unmarshal : function(request){}
};
Restfulie.media_types.register("application/amf",type);
Restfulie.media_types['application/amf'].should.equal type
end

it 'should support xml retrieval'
r = Restfulie.at("http://localhost:3000/items").accepts("application/xml").get();
r.items.item[0].price.should.equal 10
r.response.body.should.not.be_null
r.response.code.should.equal 200
end

it 'should support xml marshalling'
r = Restfulie.at("http://localhost:3000/items/1").accepts("application/xml").get();
r.item.price.should.equal 10
r.response.body.should.not.be_null
r.response.code.should.equal 200
r.item.price = 500
rn = Restfulie.at("http://localhost:3000/items").as("application/xml").post(r);
rn.response.body.should.not.be_null
rn.response.code.should.equal 200
rn.item.price.should.equal 500
end

it 'should post the content if its a string'
rn = Restfulie.at("http://localhost:3000/items").as("application/xml").post("<item><name>Calpis</name><price>500</price></item>");
rn.response.body.should.not.be_null
rn.response.code.should.equal 200
rn.item.price.should.equal 500
end

it 'should add all known media types as default accepts'
entryPoint = Restfulie.at("http://localhost:3000/items");
entryPoint.headers['Accept'].indexOf("application/xml").should.not.equal -1
entryPoint.headers['Accept'].indexOf("application/json").should.not.equal -1
end

it 'should allow the representation to follow those links'
resource = Restfulie.at("http://localhost:3000/items").accepts("application/xml").get();
itemForList = resource.items.item[0];
itemForLink = itemForList.links["self"].get();
itemForLink.item.id.should.equal itemForList.id
end

it 'Support link following for json'
resource = Restfulie.at("http://localhost:3000/items").accepts("application/json").get();
itemForList = resource.items[0];
itemForLink = itemForList.links["self"].get();
itemForLink.item.id.should.equal itemForList.id
end

it 'should follow header links'
json_one = '{"item":{"price":10.0,"name":"Calpis","created_at":"2010-04-20T14:19:25Z","updated_at":"2010-04-20T14:19:25Z","id":1}}';
json_two = '{"item":{"price":10.0,"name":"Calpis","created_at":"2010-04-20T14:19:25Z","updated_at":"2010-04-20T14:19:25Z","id":2}}';

mock_request().and_return(json_one, 'application/json', 200,{Link: ' </items/two>; rel="two"'});
h_one = Restfulie.at("http://restfulie.js/items/one").accepts("application/json").get();

mock_request().and_return(json_two, 'application/json', 200, {Link: ' </items/one>; rel="one"'});
h_two = h_one.response.headers.links['two'].get();

h_two.item.id.should.equal 2
end

end

describe 'Rest from scratch examples'
describe 'Part 1'
it 'should get items with default response format'
h = Restfulie.at('http://localhost:3000/items').get();

h.items.length.should.be_greater_than 0
h.items[0].name.should.equal "Calpis"
h.items[0].price.should.equal 10
end

it 'should get items when I request specific format, in this case json'
h = Restfulie.at('http://localhost:3000/items').accepts("application/json").get();

h.items.length.should.be_greater_than 0
h.items[0].name.should.equal "Calpis"
h.items[0].price.should.equal 10

h.response.code.should.equal 200
h.response.headers['Content-Type'].indexOf('application/json').should.equal 0
end

it 'should create an item using a xml representation of an item'
xml = {
item: {
name:'calpis_xml',
price:3
}
};
h = Restfulie.at("http://localhost:3000/items").as("application/xml").post(xml);
h.item.price.should.equal 3
h.item.name.should.equal 'calpis_xml'
end

it 'should create an item using a json representation of an item'
json = {
item: {
name:'calpis_json',
price:3
}
};
h = Restfulie.at("http://localhost:3000/items").as("application/json").post(json);
h.item.price.should.equal 3
h.item.name.should.equal 'calpis_json'
end
end
end
end
function search (what) {
var description = Restfulie.at("http://localhost:3000/products/opensearch.xml").accepts('application/opensearchdescription+xml').get().resource();
}

JSpec.describe('Restfulie for Javascript', function(){
describe('when searching', function(){
it('should be able to search', function(){
var description = Restfulie.at("http://localhost:3000/products/opensearch.xml").accepts('application/opensearchdescription+xml').get().resource();
//var items = description.use("application/atom+xml").search({searchTerms : "20", startPage : 1});
//items.resource.entries.size.should.equal 2
});
});
});

4 changes: 4 additions & 0 deletions proxyfier/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.bundle
db/*.sqlite3
log/*.log
tmp/**/*
33 changes: 33 additions & 0 deletions proxyfier/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
source 'http://rubygems.org'

gem 'rails', '3.0.0'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'restfulie', "= 1.0.0.beta1"
gem 'debuggie'
gem 'respondie'

# Use unicorn as the web server
gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug'

# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3'

# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
# group :development, :test do
# gem 'webrat'
# end
90 changes: 90 additions & 0 deletions proxyfier/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionmailer (3.0.0)
actionpack (= 3.0.0)
mail (~> 2.2.5)
actionpack (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.4.1)
rack (~> 1.2.1)
rack-mount (~> 0.6.12)
rack-test (~> 0.5.4)
tzinfo (~> 0.3.23)
activemodel (3.0.0)
activesupport (= 3.0.0)
builder (~> 2.1.2)
i18n (~> 0.4.1)
activerecord (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
arel (~> 1.0.0)
tzinfo (~> 0.3.23)
activeresource (3.0.0)
activemodel (= 3.0.0)
activesupport (= 3.0.0)
activesupport (3.0.0)
arel (1.0.1)
activesupport (~> 3.0.0)
builder (2.1.2)
debuggie (0.9.0)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.4.1)
json_pure (1.4.6)
kgio (2.1.1)
mail (2.2.5)
activesupport (>= 2.3.6)
mime-types
treetop (>= 1.4.5)
mime-types (1.16)
nokogiri (1.4.4)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack (>= 1.0.0)
rack-test (0.5.6)
rack (>= 1.0)
rails (3.0.0)
actionmailer (= 3.0.0)
actionpack (= 3.0.0)
activerecord (= 3.0.0)
activeresource (= 3.0.0)
activesupport (= 3.0.0)
bundler (~> 1.0.0)
railties (= 3.0.0)
railties (3.0.0)
actionpack (= 3.0.0)
activesupport (= 3.0.0)
rake (>= 0.8.4)
thor (~> 0.14.0)
rake (0.8.7)
respondie (0.9.0)
restfulie (1.0.0.beta1)
actionpack (>= 2.3.2)
activesupport (>= 2.3.2)
json_pure (>= 1.2.4)
nokogiri (>= 1.4.2)
sqlite3-ruby (1.3.1)
thor (0.14.0)
treetop (1.4.8)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
unicorn (3.2.1)
kgio (~> 2.1)
rack

PLATFORMS
ruby

DEPENDENCIES
debuggie
rails (= 3.0.0)
respondie
restfulie (= 1.0.0.beta1)
sqlite3-ruby
unicorn
Loading

0 comments on commit 7c4ec1e

Please sign in to comment.