Skip to content

Commit

Permalink
cleaned test and changed module to document
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaLanziani committed Dec 13, 2011
1 parent f3a1b5e commit bdc0300
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 173 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
node_modules
test/repo
*.sock
test.js
*~
Empty file added .monitor
Empty file.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

REPODIR = test/repo
SRC = lib/rest-connect.js

test-server:
$(REPODIR):
mkdir -p $(REPODIR)

test-server: $(REPODIR)
@node test/server
91 changes: 46 additions & 45 deletions lib/rext-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ var rext_connect = module.exports = {};
*/
rext_connect.version = '0.0.2';


/**
* Send back an error message to the client.
* @param {Response} res http.ServerResponse Object
* @param {String} message Text to send
*/
function sendError(res, message) {
res.writeHead(400, {
'Content-Length': message.length,
'Content-Type': 'text/plain'
})
res.end(message);
}

/**
* Initialize the module.
*
Expand All @@ -31,120 +45,107 @@ rext_connect.routes = function (options) {
, pre = '/' + prefix + '/' + version
, rext = options.rext || {}
, paths = {
destroy: pre + '/resources/:module/:version/destroy'
, list: pre + '/resources/:module?'
, retrieve: pre + '/resources/:module/:version'
, create: pre + '/resources/:module/:version/create'
, update: pre + '/resources/:module/:version/update'
destroy: pre + '/resources/:document/:version/destroy'
, list: pre + '/resources/:document?'
, retrieve: pre + '/resources/:document/:version'
, create: pre + '/resources/:document/:version/create'
, update: pre + '/resources/:document/:version/update'
}
;

return function (app) {

app.get(paths.list, function (req, res, next) {
var options = {
name: req.params.module
};

rext.list(options, function (err, reply) {
var name = req.params.document;

rext.list(name, function (err, reply) {
if (err) {
res.send(err);
sendError(res, err.message);
return;
}

var data = JSON.stringify(reply);
res.writeHead(200, {
'Content-Length': reply.length,
'Content-Type': 'text/plain'
'Content-Length': data.length,
'Content-Type': 'application/json'
});

res.end(reply);
res.end(data);
});
});

app.get(paths.retrieve, function (req, res, next) {
var options = {
name: req.params.module,
name: req.params.document,
version: req.params.version
};

rext.retrieve(options, function (err, reply) {
if (err) {
res.send(err);
res.end(err.message);
return;
};

res.writeHead(200, {
'Content-Length': reply.length,
'Content-Type': 'text/plain'
});

res.end(reply);

//put the line below in the server
//callback(null, fs.createReadStream(filePath));
//reply.pipe(res);
res.end(reply.toString('utf8'));
});
});


app.get(paths.destroy, function (req, res, next) {
var options = {
name: req.params.module,
name: req.params.document,
version: req.params.version
};

rext.destroy(options, function (err, reply) {
if (err) {
res.send(err);
sendError(res, err.message);
return;
}

res.writeHead(200, {
'Content-Length': reply.length,
'Content-Type': 'text/plain'
});

res.end(reply);

res.end();
});
});

app.post(paths.create, function (req, res, next) {
var options = {
name: req.params.module,
name: req.params.document,
version: req.params.version,
data: req.body.data
};

rext.create(options, function (err, reply) {
if (err) {
res.send(err);
sendError(res, err.message);
return;
}

res.writeHead(200, {
'Content-Length': reply.length,
'Content-Type': 'text/plain'
});

res.end(reply);
res.end();
});
});

app.post(paths.update, function (req, res, next) {

var options = {
name: req.params.module,
name: req.params.document,
version: req.params.version,
data: req.body.data
};

rext.update(options, function (err, reply) {
if (err) {
res.send(err);
sendError(res, err.message);
return;
}

res.writeHead(200, {
'Content-Length': reply.length,
'Content-Type': 'text/plain'
});

res.end(reply);
res.end();
});
});
};
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
, "author": [""]
, "dependencies": {}
, "devDependencies": {
"connect": "1.8.2"
"connect": "1.8.2"
, "rext": ">= 0.0.5"
}
, "main": "./index.js"
, "engines": { "node": ">= 0.4.0 < 0.7.0" }
Expand Down
62 changes: 9 additions & 53 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,35 @@
<body id="index" onload="">
<div>
<h1>Default</h1>
<p>app.use(connect.router(rext_connect.routes({"rext":echo})))</p>
<p>app.use(connect.router(rext_connect.routes({"rext":rext})))</p>
<div style="padding-left:20px">
<p>
- <a href="/rext/1/resources">List modules</a><br>
should return <em>{}</em>
- <a href="/rext/1/resources">List document</a><br>
</p>
<p>
- <a href="/rext/1/resources/module">List module versions</a><br>
should return <em>{"name": "module"}</em>
- <a href="/rext/1/resources/document">List document versions</a>
</p>
<p>
- <a href="/rext/1/resources/module/1.0.1">Get version 1.0.1 of module</a><br>
should return <em>{"name": "module", "version": "1.0.1"}</em>
- <a href="/rext/1/resources/document/1.0.1">Get version 1.0.1 of document</a>
</p>
<p>
- <a href="/rext/1/resources/module/1.0.1/destroy">Destroy version x.y.z of module</a><br>
should return <em>{"name": "module", "version": "1.0.1"}</em>
- <a href="/rext/1/resources/document/1.0.1/destroy">Destroy version 1.0.1 of document</a>
</p>
<p>
<form action="/rext/1/resources/module/1.0.1/create" method="post">
- Create version 1.0.1 of module
<form action="/rext/1/resources/document/1.0.1/create" method="post">
- Create version 1.0.1 of document
<input type="text" name="data" value="testo di prova"/>
<input type="submit" value="Try it" id="create"/>
</form>
should return <em>{"name": "module", "version": "1.0.1", "data": "testo di prova"}</em>
</p>
<p>
<form action="/rext/1/resources/module/1.0.1/update" method="post">
- Update version 1.0.1 of module
<form action="/rext/1/resources/document/1.0.1/update" method="post">
- Update version 1.0.1 of document
<input type="text" name="data" value="new testo di prova"/>
<input type="submit" value="Try it"/>
</form>
should return <em>{"name": "module", "version": "1.0.1", "data": "new testo di prova"}</em>
</p>
</div>
</div>
<div>
<h1>Myapp</h1>
<p>app.use(connect.router(rext_connect.routes({"prefix":"myapp","rext":rext})))</p>
<div style="padding-left:20px">
<p>
- <a href="/myapp/1/resources">List modules</a> <br>
should return <em>myappLIST {}</em>
</p>
<p>
- <a href="/myapp/1/resources/module">List module versions</a> <br>
should return <em>myappLIST {"name": "module"}</em>
</p>
<p>
- <a href="/myapp/1/resources/module/1.0.1">Get version 1.0.1 of module</a> <br>
should return <em>myappRETRIEVE {"name": "module", "version": "1.0.1"}</em>
</p>
<p>
- <a href="/myapp/1/resources/module/1.0.1/destroy">Destroy version x.y.z of module</a>
should return <em>myappDESTROY {"name": "module", "version": "1.0.1"}</em>
</p>
<p>
<form action="/myapp/1/resources/module/1.0.1/create" method="post">
- Create version 1.0.1 of module
<input type="text" name="data" value="testo di prova"/>
<input type="submit" value="Try it" id="create"/>
</form>
should return <em>myappCREATE {"name": "module", "version": "1.0.1", "data": "testo di prova"}</em>
</p>
<p>
<form action="/myapp/1/resources/module/1.0.1/update" method="post">
- Update version 1.0.1 of module
<input type="text" name="data" value="new testo di prova"/>
<input type="submit" value="Try it"/>
</form>
should return <em>myappUPDATE {"name": "module", "version": "1.0.1", "data": "new testo di prova"}</em>
</p>
</div>
</div>
</body>
</html>
32 changes: 4 additions & 28 deletions test/server.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
var connect = require('../node_modules/connect/')
, rext_connect = require('../lib/rext-connect')
, Rext = require('../node_modules/rext')
, app = connect()
, port = 3000
, prefix = "myapp"
, prefix = "echo"
;

function echo(options, callback) {
callback(null, JSON.stringify(options));
};

function rext_function(name) {
return function (options, callback) {
callback(null, prefix + name + " " + JSON.stringify(options));
}
}

var rext = {
list: rext_function("LIST"),
create: rext_function("CREATE"),
update: rext_function("UPDATE"),
destroy: rext_function("DESTROY"),
retrieve: rext_function("RETRIEVE"),
};

var echo = {
list: echo,
create: echo,
update: echo,
destroy: echo,
retrieve: echo
};
var rext = new Rext("./test/repo/");

app.use(connect.bodyParser());
app.use(connect(connect.static(__dirname)));
app.use(connect.router(rext_connect.routes({"prefix": prefix,"rext": rext})));
app.use(connect.router(rext_connect.routes({"rext":echo}))).listen(port);
app.use(connect.router(rext_connect.routes({"rext":rext}))).listen(port);

console.log('Server running at http://127.0.0.1:' + port + '/');

Expand Down
43 changes: 0 additions & 43 deletions test/server.js~

This file was deleted.

0 comments on commit bdc0300

Please sign in to comment.