Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed bugs implemented other tests, bodyDataReader refactored

  • Loading branch information...
commit cb5d581335d43bc9cd2988e2c7325cf4fe8ef6b1 1 parent 98e7bac
@euprogramador euprogramador authored
View
7 server/lib/converterManager.js
@@ -13,8 +13,15 @@ function ConverterManager(){
}
this.getConverter = function(format){
+
+ if (!format || format == "")
+ return null;
+
+ format = format.split(";")[0];
for (c in converters)
if (converters[c].format == format)
return converters[c];
+
+ return null;
}
}
View
20 server/lib/filters/bodyDataReader.js
@@ -1,20 +0,0 @@
-exports.create = function(){
- return new BodyDataReader();
-}
-
-function BodyDataReader(){
- this.execute = function(request,response,chain){
- request.body = "";
- if ("POST,PUT".indexOf(request.method) != -1) {
- request.setEncoding('utf8');
- request.on('data',function(data){
- request.body = data;
- chain.doChain(request,response);
- });
- } else {
- chain.doChain(request,response);
- }
-
- }
-}
-
View
14 server/lib/filters/contentNegotiation.js
@@ -8,10 +8,24 @@ function ContentNegotiation(converterManager){
if (request.data == null)
request.data = {};
+ if (request.headers['content-type'] == null){
+ response.statusCode = 500;
+ response.body = "content-type is not informed";
+ return;
+ }
+
converter = converterManager.getConverter(request.headers['content-type']);
+
+ if (converter == null) {
+ response.statusCode = 500;
+ response.body = "content-type is not supported";
+ return;
+ }
+
object = converter.toObject(request.body);
enrichContent(request.data,object);
+
chain.doChain(request,response);
if (response.headers == null)
View
3  server/lib/filters/processResponse.js
@@ -6,6 +6,9 @@ function ProcessResponse(){
this.execute = function(request,response,chain){
chain.doChain(request,response);
+ if (response.headers == null)
+ response.headers = {};
+
if (response.logicalName == "create" && request.method == "PUT") {
response.statusCode = 201;
response.headers['location'] = "end";
View
7 server/lib/filters/routeExecutor.js
@@ -7,6 +7,7 @@ function RouteExecutor(routeManager){
this.execute = function(request,response,chain){
route = routeManager.find(request.method,request.url);
+
if (route == null) {
response.data = "";
response.statusCode = 404;
@@ -19,9 +20,11 @@ function RouteExecutor(routeManager){
try {
eval(executer);
response.data = retorno;
- response.statusCode = 200;
+ response.statusCode = 200;
+ response.statusCode = retorno == null && request.method == "GET" ? 404 : 200;
} catch (e){
- console.log(e);
+ console.log(e.message);
+ console.log(e.stack);
response.data = "";
response.statusCode = 500;
}
View
19 server/lib/restfulie.js
@@ -2,7 +2,6 @@ var routeManager = require("./routeManager")
var http = require("http")
var filterManager = require("./filterManager")
var converterManager = require("./converterManager")
-var bodyDataReader = require("./filters/bodyDataReader")
var contentNegotiation = require("./filters/contentNegotiation")
var pathDataReader = require("./filters/pathDataReader")
var queryStringDataReader= require("./filters/queryStringDataReader")
@@ -22,8 +21,8 @@ function Restfulie(){
var server=null;
convertersManager.register("application/json",JSONConverter.create());
+
filtersManager.register(processResponse.create());
- filtersManager.register(bodyDataReader.create());
filtersManager.register(contentNegotiation.create(convertersManager));
filtersManager.register(queryStringDataReader.create());
filtersManager.register(pathDataReader.create(routesManager));
@@ -58,7 +57,7 @@ function Restfulie(){
this.registerRoute("POST","/"+baseuri,object_resource.create);
if (object_resource.update != null)
- this.registerRoute("PUT","/"+baseuri,object_resource.update);
+ this.registerRoute("PUT","/"+baseuri+"/{id}",object_resource.update);
}
@@ -77,7 +76,19 @@ function Restfulie(){
this.listen = function(port,ip){
server = http.createServer(function(request,response){
- filtersManager.execute(request,response);
+
+ if ("POST,PUT".indexOf(request.method) != -1){
+ request.setEncoding('utf8');
+ request.on('data',function(data){
+ request.body = data;
+ filtersManager.execute(request,response);
+ });
+ } else {
+ request.body = "";
+ filtersManager.execute(request,response);
+ }
+
+
});
server.listen(port,ip);
}
View
43 server/test/integration/AllIntegrationTest.js
@@ -0,0 +1,43 @@
+var restfulie = require("../../lib/restfulie")
+
+var clientes = [];
+function ClientesResource(){
+
+ this.index = function(){
+ console.log("clientes");
+ console.log(clientes);
+ return clientes;
+ }
+
+ this.show = function(id){
+ console.log("show"+id);
+ return clientes[id];
+ }
+
+ this.destroy = function(id){
+ console.log("destroy"+ id);
+ delete clientes[id];
+ }
+
+ this.create = function(cliente){
+ console.log("criar");
+ console.log(cliente);
+ clientes.push(cliente);
+ }
+
+ this.update = function(cliente,id){
+ console.log("update");
+ console.log(id);
+ console.log(cliente);
+ clientes[id] = cliente;
+ }
+
+}
+
+server = restfulie.createServer();
+
+server.registerResource(ClientesResource);
+server.listen(3000,"127.0.0.1");
+
+console.log("initialized.");
+
View
3  server/test/unit/converterManagerTest.js
@@ -32,5 +32,6 @@ module.exports = testCase({
converter = cm.getConverter('application/json');
assert.equal(converter,JSONConverter);
assert.done();
- }
+ },
+
});
View
2  server/test/unit/filters/allFiltersTest.js
@@ -1,14 +1,12 @@
var routeExecutorTest = require("./routeExecutorTest")
var pathDataReaderTest = require("./pathDataReaderTest")
var queryStringDataReaderTest = require("./queryStringDataReaderTest")
-var bodyDataReaderTest = require("./bodyDataReaderTest")
var contentNegotiationTest = require("./contentNegotiationTest")
var processResponseTest = require("./processResponseTest")
exports['routeExecutorTest'] = routeExecutorTest;
exports['pathDataReaderTest'] = pathDataReaderTest;
exports['queryStringDataReaderTest'] = queryStringDataReaderTest;
-exports['bodyDataReaderTest'] = bodyDataReaderTest;
exports['contentNegotiationTest'] = contentNegotiationTest;
exports['processResponse'] = processResponseTest;
View
38 server/test/unit/filters/bodyDataReaderTest.js
@@ -1,38 +0,0 @@
-var testCase = require('nodeunit').testCase;
-var bodyDataReader = require("../../../lib/filters/bodyDataReader")
-
-var dr;
-
-module.exports = testCase({
-
- setUp : function(callback){
- dr = bodyDataReader.create();
- callback();
- },
-
- 'should read data body sended at request': function (assert) {
- request = {
- method:"POST",
- url:"/services/1/carlos",
- on : function(event,callback){
- callback("data sended");
- },
- setEncoding : function(){}
- };
-
- invoked = false;
- chain = {
- doChain : function(request,response){
- invoked = true;
- }
- };
-
- dr.execute(request,response,chain);
-
- assert.ok(request.body !=null);
- assert.equal(request.body,'data sended');
- assert.ok(invoked);
- assert.done();
- },
-
-});
View
19 server/test/unit/filters/contentNegotiationTest.js
@@ -55,4 +55,23 @@ module.exports = testCase({
assert.done();
},
+
+ 'should return response is content-type not defined or not supported' : function(assert){
+ chain = {
+ doChain : function(request,response){
+ }
+ };
+ request = {
+ headers : {}
+ };
+ response ={};
+
+ converterManager.getConverter = function(format){
+ }
+
+ cn.execute(request,response,chain);
+ assert.equal(response.statusCode,500);
+ assert.equal(response.body,"content-type is not informed");
+ assert.done();
+ }
});
View
50 server/test/unit/filters/routeExecutorTest.js
@@ -71,6 +71,56 @@ module.exports = testCase({
assert.ok(chainInvoked)
assert.done();
+ },
+
+ 'should return 404 statusCode for logic returning is null of request method get' : function(assert){
+ request = {method:"GET"};
+ response = {};
+
+ rm.find = function(){
+ return {
+ uri:"/services/{client.id}",
+ method:"GET",
+ logic: function(){
+ }
+ };
+ }
+ chainInvoked = false;
+ chain = {doChain : function(){
+ }};
+
+ re.execute(request,response,chain);
+ assert.equal(response.statusCode,404);
+ assert.done();
+ },
+
+ 'should return 500 at logic of error' : function(assert){
+ request = {method:"GET"};
+ response = {};
+
+ rm.find = function(){
+ return {
+ uri:"/services/{client.id}",
+ method:"GET",
+ logic: function(){
+ throw new Error("error");
+ }
+ };
+ }
+ chainInvoked = false;
+ chain = {doChain : function(){
+ }};
+ _console = console;
+ console = {log : function(){}};
+
+ re.execute(request,response,chain);
+
+ console = _console;
+
+ assert.equal(response.statusCode,500);
+ assert.done();
}
+
+
});
Please sign in to comment.
Something went wrong with that request. Please try again.