├── .gitignore ├── .travis.yml ├── LICENSE.md ├── README.md ├── app.coffee ├── app.js ├── package.json ├── server.coffee ├── server.js └── test ├── mocha.opts ├── support └── http.js ├── test.coffee └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | npm-debug.log 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | services: 3 | - redis-server 4 | node_js: 5 | - "4" 6 | - "6" 7 | - "7" 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013, 2014, 2015 Patrick Liess http://www.tcs.de 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # REST rsmq 2 | 3 | [![Build Status](https://secure.travis-ci.org/smrchy/rest-rsmq.png?branch=master)](http://travis-ci.org/smrchy/rest-rsmq) 4 | 5 | 6 | A REST interface for [rsmq](https://github.com/smrchy/rsmq) the really simple message queue based on Redis. 7 | 8 | This REST interface makes it easy to use [rsmq](https://github.com/smrchy/rsmq) with other application platforms like php, .net etc. Simply call the RESTful methods to send and receive messages. 9 | 10 | **Note:** There is no security built in - so use this only in a trusted enviroment, just like memcached. 11 | 12 | 13 | ## Installation 14 | 15 | * Make sure Node 0.8.10+ is installed 16 | * Clone this repository 17 | * Run `npm install` to install the dependencies 18 | * For the test make sure Redis runs locally and run `npm test` 19 | * *Optional:* Modify the server port in server.js 20 | * Start the server: `node server.js` 21 | 22 | 23 | ## Methods 24 | 25 | ### changeMessageVisibility 26 | 27 | **PUT /message/qname/id** 28 | 29 | Change the visibility of the message with id `id` from queue `qname`. 30 | 31 | Example: 32 | 33 | ``` 34 | PUT /message/myqueue/dhxekddac921a9422ec10e5439b55aa62e4dd49142?vt=5 35 | ``` 36 | 37 | Response: 38 | 39 | ``` 40 | {"result": 1} 41 | ``` 42 | 43 | 44 | ### createQueue 45 | 46 | **POST /queues/qname** 47 | 48 | Create a new queue `qname` 49 | 50 | Parameters: 51 | 52 | * `qname` (String): The Queue name. Maximum 80 characters; alphanumeric characters, hyphens (-), and underscores (_) are allowed. 53 | * `vt` (Number): *optional* *(Default: 30)* The length of time, in seconds, that a message received from a queue will be invisible to other receiving components when they ask to receive messages. Allowed values: 0-9999999 54 | * `delay` (Number): *optional* *(Default: 0)* The time in seconds that the delivery of all new messages in the queue will be delayed. Allowed values: 0-9999999 55 | * `maxsize` (Number): *optional* *(Default: 65536)* The maximum message size in bytes. Allowed values: 1024-65536 56 | 57 | Example: 58 | 59 | ``` 60 | POST /queues/myqueue 61 | Content-Type: application/json 62 | 63 | { 64 | "vt": 30, 65 | "delay": 0, 66 | "maxsize": 2048 67 | } 68 | ``` 69 | 70 | Response: 71 | 72 | ``` 73 | {"result": 1} 74 | ``` 75 | 76 | ### deleteMessage 77 | 78 | **DELETE /messages/qname/id** 79 | 80 | Delete the message with id `id` from queue `qname`. 81 | 82 | Example: 83 | 84 | ``` 85 | DELETE /messages/myqueue/dhxekddac921a9422ec10e5439b55aa62e4dd49142 86 | ``` 87 | 88 | Response: 89 | 90 | ``` 91 | {"result": 1} 92 | ``` 93 | 94 | 95 | ### deleteQueue 96 | 97 | **DELETE /queues/qname** 98 | 99 | Delete the queue `qname` and all messages in that queue. 100 | 101 | Example: 102 | 103 | ``` 104 | DELETE /queues/myqueue 105 | ``` 106 | 107 | Response: 108 | 109 | ``` 110 | {"result": 1} 111 | ``` 112 | 113 | 114 | ### getQueueAttributes 115 | 116 | Get queue attributes, counter and stats 117 | 118 | Parameters: 119 | 120 | * `qname` (String): The Queue name. 121 | 122 | Example: 123 | 124 | ``` 125 | GET /queues/myqueue 126 | ``` 127 | 128 | Response: 129 | 130 | ``` 131 | { 132 |    "vt": 30, 133 |    "delay": 0, 134 |    "maxsize": 65536, 135 |    "totalrecv": 32, 136 |    "totalsent": 13, 137 |    "created": 1371645477, 138 |    "modified": 1371645477, 139 |    "msgs": 4, 140 |    "hiddenmsgs": 2 141 | } 142 | ``` 143 | 144 | 145 | 146 | ### listQueues 147 | 148 | **GET /queues** 149 | 150 | List all queues. 151 | 152 | Example: 153 | 154 | ``` 155 | GET /queues 156 | ``` 157 | 158 | Response: 159 | 160 | ``` 161 | {"queues": ["myqueue","someOtherQueue"]} 162 | ``` 163 | 164 | 165 | ### receiveMessage 166 | 167 | **GET /messages/qname** 168 | 169 | Receive the next message from the queue. 170 | 171 | Parameters: 172 | 173 | * `vt` (Number): *optional* *(Default: 30)* The length of time, in seconds, that a message received from a queue will be invisible to other receiving components when they ask to receive messages. Allowed values: 0-9999999 174 | 175 | Example: 176 | 177 | ``` 178 | GET /messages/myqueue?vt=30 179 | ``` 180 | 181 | Response: 182 | 183 | ``` 184 | { 185 | "id": "dhxeguaqv8cb2c78e9b5c97121d60f4caf54925c03", 186 | "message": "Hello World!", 187 | "rc": 1, 188 | "fr": 1370856862274, 189 | "sent": 1370855815832 190 | } 191 | ``` 192 | 193 | ### sendMessage 194 | 195 | **POST /messages/qname** 196 | 197 | Sends a new message. 198 | 199 | Example: 200 | 201 | ``` 202 | POST /messages/myqueue 203 | Content-Type: application/json 204 | 205 | { 206 | "message": "Hello World!", 207 | "delay": 0 208 | } 209 | ``` 210 | 211 | Response: 212 | 213 | ``` 214 | { 215 | "id": "dhxekddac921a9422ec10e5439b55aa62e4dd49142" 216 | } 217 | ``` 218 | 219 | 220 | 221 | ## The MIT License (MIT) 222 | 223 | Please see the LICENSE.md file. 224 | -------------------------------------------------------------------------------- /app.coffee: -------------------------------------------------------------------------------- 1 | ### 2 | Rest rsmq 3 | 4 | The MIT License (MIT) 5 | 6 | Copyright © 2013 Patrick Liess, http://www.tcs.de 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 11 | 12 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 13 | ### 14 | 15 | 16 | RSMQ = require "rsmq" 17 | rsmq = new RSMQ() 18 | 19 | express = require 'express' 20 | app = express() 21 | 22 | 23 | app.use (req, res, next) -> 24 | res.header('Content-Type', "application/json") 25 | res.removeHeader("X-Powered-By") 26 | next() 27 | return 28 | 29 | app.configure -> 30 | app.use( express.logger("dev") ) 31 | app.use(express.bodyParser()) 32 | return 33 | 34 | 35 | 36 | app.get '/queues', (req, res) -> 37 | rsmq.listQueues (err, resp) -> 38 | if err 39 | res.send(err, 500) 40 | return 41 | res.send({queues: resp}) 42 | return 43 | 44 | 45 | app.get '/queues/:qname', (req, res) -> 46 | rsmq.getQueueAttributes {qname: req.params.qname}, (err, resp) -> 47 | if err 48 | res.send(err, 500) 49 | return 50 | res.send(resp) 51 | return 52 | 53 | 54 | app.post '/queues/:qname', (req, res) -> 55 | params = req.body 56 | params.qname = req.params.qname 57 | rsmq.createQueue params, (err, resp) -> 58 | if err 59 | res.send(err, 500) 60 | return 61 | res.send({result:resp}) 62 | return 63 | return 64 | 65 | 66 | app.delete '/queues/:qname', (req, res) -> 67 | rsmq.deleteQueue {qname: req.params.qname}, (err, resp) -> 68 | if err 69 | res.send(err, 500) 70 | return 71 | res.send({result:resp}) 72 | return 73 | 74 | 75 | app.post '/messages/:qname', (req, res) -> 76 | params = req.body 77 | params.qname = req.params.qname 78 | rsmq.sendMessage params, (err, resp) -> 79 | if err 80 | res.send(err, 500) 81 | return 82 | res.send({id:resp}) 83 | return 84 | return 85 | 86 | 87 | app.get '/messages/:qname', (req, res) -> 88 | rsmq.receiveMessage {qname: req.params.qname, vt: req.param("vt")}, (err, resp) -> 89 | if err 90 | res.send(err, 500) 91 | return 92 | res.send(resp) 93 | return 94 | return 95 | 96 | app.put '/messages/:qname/:id', (req, res) -> 97 | rsmq.changeMessageVisibility {qname: req.params.qname, id: req.params.id, vt: req.param("vt")}, (err, resp) -> 98 | if err 99 | res.send(err, 500) 100 | return 101 | res.send({result:resp}) 102 | return 103 | return 104 | 105 | 106 | app.delete '/messages/:qname/:id', (req, res) -> 107 | rsmq.deleteMessage req.params, (err, resp) -> 108 | if err 109 | res.send(err, 500) 110 | return 111 | res.send({result:resp}) 112 | return 113 | return 114 | 115 | 116 | 117 | 118 | 119 | 120 | module.exports = app 121 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | // Generated by CoffeeScript 1.6.2 2 | /* 3 | Rest rsmq 4 | 5 | The MIT License (MIT) 6 | 7 | Copyright © 2013 Patrick Liess, http://www.tcs.de 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 14 | */ 15 | 16 | 17 | (function() { 18 | var RSMQ, app, express, rsmq; 19 | 20 | RSMQ = require("rsmq"); 21 | 22 | rsmq = new RSMQ(); 23 | 24 | express = require('express'); 25 | 26 | app = express(); 27 | 28 | app.use(function(req, res, next) { 29 | res.header('Content-Type', "application/json"); 30 | res.removeHeader("X-Powered-By"); 31 | next(); 32 | }); 33 | 34 | app.configure(function() { 35 | app.use(express.logger("dev")); 36 | app.use(express.bodyParser()); 37 | }); 38 | 39 | app.get('/queues', function(req, res) { 40 | return rsmq.listQueues(function(err, resp) { 41 | if (err) { 42 | res.send(err, 500); 43 | return; 44 | } 45 | res.send({ 46 | queues: resp 47 | }); 48 | }); 49 | }); 50 | 51 | app.get('/queues/:qname', function(req, res) { 52 | return rsmq.getQueueAttributes({ 53 | qname: req.params.qname 54 | }, function(err, resp) { 55 | if (err) { 56 | res.send(err, 500); 57 | return; 58 | } 59 | res.send(resp); 60 | }); 61 | }); 62 | 63 | app.post('/queues/:qname', function(req, res) { 64 | var params; 65 | 66 | params = req.body; 67 | params.qname = req.params.qname; 68 | rsmq.createQueue(params, function(err, resp) { 69 | if (err) { 70 | res.send(err, 500); 71 | return; 72 | } 73 | res.send({ 74 | result: resp 75 | }); 76 | }); 77 | }); 78 | 79 | app["delete"]('/queues/:qname', function(req, res) { 80 | return rsmq.deleteQueue({ 81 | qname: req.params.qname 82 | }, function(err, resp) { 83 | if (err) { 84 | res.send(err, 500); 85 | return; 86 | } 87 | res.send({ 88 | result: resp 89 | }); 90 | }); 91 | }); 92 | 93 | app.post('/messages/:qname', function(req, res) { 94 | var params; 95 | 96 | params = req.body; 97 | params.qname = req.params.qname; 98 | rsmq.sendMessage(params, function(err, resp) { 99 | if (err) { 100 | res.send(err, 500); 101 | return; 102 | } 103 | res.send({ 104 | id: resp 105 | }); 106 | }); 107 | }); 108 | 109 | app.get('/messages/:qname', function(req, res) { 110 | rsmq.receiveMessage({ 111 | qname: req.params.qname, 112 | vt: req.param("vt") 113 | }, function(err, resp) { 114 | if (err) { 115 | res.send(err, 500); 116 | return; 117 | } 118 | res.send(resp); 119 | }); 120 | }); 121 | 122 | app.put('/messages/:qname/:id', function(req, res) { 123 | rsmq.changeMessageVisibility({ 124 | qname: req.params.qname, 125 | id: req.params.id, 126 | vt: req.param("vt") 127 | }, function(err, resp) { 128 | if (err) { 129 | res.send(err, 500); 130 | return; 131 | } 132 | res.send({ 133 | result: resp 134 | }); 135 | }); 136 | }); 137 | 138 | app["delete"]('/messages/:qname/:id', function(req, res) { 139 | rsmq.deleteMessage(req.params, function(err, resp) { 140 | if (err) { 141 | res.send(err, 500); 142 | return; 143 | } 144 | res.send({ 145 | result: resp 146 | }); 147 | }); 148 | }); 149 | 150 | module.exports = app; 151 | 152 | }).call(this); 153 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rest-rsmq", 3 | "description": "A RESTful interface for rsmq", 4 | "version": "0.4.1", 5 | "author": "P. Liess ", 6 | "engines": { 7 | "node": "> 0.10.20" 8 | }, 9 | "scripts": { 10 | "test": "mocha ./test/test.js" 11 | }, 12 | "dependencies": { 13 | "express": "<4.0", 14 | "rsmq": "0.7.2" 15 | }, 16 | "devDependencies": { 17 | "underscore": "*", 18 | "mocha": "*", 19 | "should": "*", 20 | "async": "*" 21 | }, 22 | "keywords": [ 23 | "rest", 24 | "restful", 25 | "rsmq", 26 | "queue", 27 | "messagequeue", 28 | "sqs", 29 | "aws", 30 | "redis" 31 | ], 32 | "repository" : { 33 | "type": "git", 34 | "url": "http://github.com/smrchy/rest-rsmq.git" 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /server.coffee: -------------------------------------------------------------------------------- 1 | PORT = 8101 2 | 3 | app = require "./app" 4 | 5 | server = app.listen(PORT) 6 | console.log "Listening on port #{PORT}" -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | // Generated by CoffeeScript 1.6.2 2 | (function() { 3 | var PORT, app, server; 4 | 5 | PORT = 8101; 6 | 7 | app = require("./app"); 8 | 9 | server = app.listen(PORT); 10 | 11 | console.log("Listening on port " + PORT); 12 | 13 | }).call(this); 14 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --bail 2 | --slow 10 3 | --reporter spec -------------------------------------------------------------------------------- /test/support/http.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | var addr = {port:8101,address:"localhost"}; 4 | 5 | /** 6 | * Modified version of TJ's http support file from the Express repo: 7 | * https://github.com/visionmedia/express/blob/master/test/support/http.js 8 | * 9 | * Module dependencies. 10 | */ 11 | 12 | var EventEmitter = require('events').EventEmitter 13 | , should = require('should') 14 | , methods = ['get','post','put','delete','head'] 15 | , http = require('http') 16 | , server; 17 | 18 | 19 | exports.createServer = function(app,fn){ 20 | if(server){ return fn(); } 21 | 22 | server = app; 23 | server.listen(addr.port, function(){ 24 | fn(); 25 | }); 26 | 27 | } 28 | 29 | exports.request = function() { 30 | return new Request(); 31 | } 32 | 33 | function Request() { 34 | var self = this; 35 | this.data = []; 36 | this.header = {}; 37 | } 38 | 39 | /** 40 | * Inherit from `EventEmitter.prototype`. 41 | */ 42 | 43 | Request.prototype.__proto__ = EventEmitter.prototype; 44 | 45 | methods.forEach(function(method){ 46 | Request.prototype[method] = function(path){ 47 | return this.request(method, path); 48 | }; 49 | }); 50 | 51 | Request.prototype.set = function(field, val){ 52 | this.header[field] = val; 53 | return this; 54 | }; 55 | 56 | Request.prototype.write = function(data){ 57 | this.data.push(data); 58 | return this; 59 | }; 60 | 61 | Request.prototype.request = function(method, path){ 62 | this.method = method; 63 | this.path = path; 64 | return this; 65 | }; 66 | 67 | Request.prototype.expect = function(body, fn){ 68 | this.end(function(res){ 69 | if ('number' == typeof body) { 70 | res.statusCode.should.equal(body); 71 | } else if (body instanceof RegExp) { 72 | res.body.should.match(body); 73 | } else { 74 | res.body.should.equal(body); 75 | } 76 | fn(); 77 | }); 78 | }; 79 | 80 | Request.prototype.end = function(fn){ 81 | 82 | var req = http.request({ 83 | method: this.method 84 | , port: addr.port 85 | , host: addr.address 86 | , path: this.path 87 | , headers: this.header 88 | }); 89 | 90 | this.data.forEach(function(chunk){ 91 | req.write(chunk); 92 | }); 93 | 94 | req.on('response', function(res){ 95 | var buf = ''; 96 | res.setEncoding('utf8'); 97 | res.on('data', function(chunk){ buf += chunk }); 98 | res.on('end', function(){ 99 | res.body = buf; 100 | fn(res); 101 | }); 102 | }); 103 | 104 | req.end(); 105 | 106 | return this; 107 | }; -------------------------------------------------------------------------------- /test/test.coffee: -------------------------------------------------------------------------------- 1 | _ = require "underscore" 2 | should = require "should" 3 | async = require "async" 4 | app = require "../app" 5 | http = require "../test/support/http" 6 | 7 | # 8 | describe 'REST-rsmq Test', -> 9 | 10 | before (done) -> 11 | http.createServer(app,done) 12 | return 13 | 14 | after (done) -> 15 | done() 16 | return 17 | 18 | q1 = "mytestQueue" 19 | m1 = null 20 | m2 = null 21 | 22 | it 'POST /queues/mytestQueue should return 200 and create the queue', (done) -> 23 | http.request().post('/queues/' + q1) 24 | .set('Content-Type','application/json') 25 | .write(JSON.stringify({ vt: 20, maxsize: 2048 })) 26 | .end (resp) -> 27 | resp.statusCode.should.equal(200) 28 | body = JSON.parse(resp.body) 29 | body.result.should.equal(1) 30 | done() 31 | return 32 | return 33 | 34 | 35 | it 'POST /messages/mytestQueue should return 200 and send message 1', (done) -> 36 | http.request().post('/messages/' + q1) 37 | .set('Content-Type','application/json') 38 | .write(JSON.stringify({ message: "Hello World!"})) 39 | .end (resp) -> 40 | resp.statusCode.should.equal(200) 41 | body = JSON.parse(resp.body) 42 | body.id.length.should.equal(32) 43 | m1 = body.id 44 | done() 45 | return 46 | return 47 | 48 | it 'POST /messages/mytestQueue should return 200 and send message 2', (done) -> 49 | http.request().post('/messages/' + q1) 50 | .set('Content-Type','application/json') 51 | .write(JSON.stringify({ message: "Foo", delay: 20})) 52 | .end (resp) -> 53 | resp.statusCode.should.equal(200) 54 | body = JSON.parse(resp.body) 55 | body.id.length.should.equal(32) 56 | m2 = body.id 57 | done() 58 | return 59 | return 60 | 61 | 62 | it 'GET /messages/mytestQueue should return message 1', (done) -> 63 | http.request().get('/messages/' + q1).end (resp) -> 64 | resp.statusCode.should.equal(200) 65 | body = JSON.parse(resp.body) 66 | body.id.should.equal(m1) 67 | done() 68 | return 69 | return 70 | 71 | it 'GET /messages/mytestQueue should not return a message', (done) -> 72 | http.request().get('/messages/' + q1).end (resp) -> 73 | resp.statusCode.should.equal(200) 74 | body = JSON.parse(resp.body) 75 | should.not.exist(body.id) 76 | done() 77 | return 78 | return 79 | 80 | it 'PUT /messages/mytestQueue/message2 to set vt to 0', (done) -> 81 | http.request().put('/messages/' + q1 + '/' + m2 + '?vt=0').end (resp) -> 82 | resp.statusCode.should.equal(200) 83 | body = JSON.parse(resp.body) 84 | body.result.should.equal (1) 85 | done() 86 | return 87 | return 88 | 89 | 90 | it 'GET /messages/mytestQueue should return message 2', (done) -> 91 | http.request().get('/messages/' + q1).end (resp) -> 92 | resp.statusCode.should.equal(200) 93 | body = JSON.parse(resp.body) 94 | body.id.should.equal(m2) 95 | done() 96 | return 97 | return 98 | 99 | it 'GET /messages/mytestQueue should not return a message', (done) -> 100 | http.request().get('/messages/' + q1).end (resp) -> 101 | resp.statusCode.should.equal(200) 102 | body = JSON.parse(resp.body) 103 | should.not.exist(body.id) 104 | done() 105 | return 106 | return 107 | 108 | it 'DELETE /messages/mytestQueue/:message1 should delete message 1', (done) -> 109 | http.request().delete('/messages/' + q1 + '/' + m1).end (resp) -> 110 | resp.statusCode.should.equal(200) 111 | body = JSON.parse(resp.body) 112 | body.result.should.equal(1) 113 | done() 114 | return 115 | return 116 | 117 | it 'DELETE /messages/mytestQueue/:message1 should fail', (done) -> 118 | http.request().delete('/messages/' + q1 + '/' + m1).end (resp) -> 119 | resp.statusCode.should.equal(200) 120 | body = JSON.parse(resp.body) 121 | body.result.should.equal(0) 122 | done() 123 | return 124 | return 125 | 126 | it 'DELETE /messages/mytestQueue/:message2 should delete message 2', (done) -> 127 | http.request().delete('/messages/' + q1 + '/' + m2).end (resp) -> 128 | resp.statusCode.should.equal(200) 129 | body = JSON.parse(resp.body) 130 | body.result.should.equal(1) 131 | done() 132 | return 133 | return 134 | 135 | it 'GET /queues/mytestQueue should return our queue attributes', (done) -> 136 | http.request().get('/queues/' + q1).end (resp) -> 137 | resp.statusCode.should.equal(200) 138 | body = JSON.parse(resp.body) 139 | body.maxsize.should.equal(2048) 140 | body.totalsent.should.equal(2) 141 | done() 142 | return 143 | return 144 | 145 | it 'GET /queues should return our queue name', (done) -> 146 | http.request().get('/queues').end (resp) -> 147 | resp.statusCode.should.equal(200) 148 | body = JSON.parse(resp.body) 149 | body.queues.should.containEql(q1) 150 | done() 151 | return 152 | return 153 | 154 | 155 | it 'DELETE /queues/mytestQueue should return 200 ', (done) -> 156 | http.request().delete('/queues/' + q1).expect(200,done) 157 | return 158 | 159 | 160 | 161 | 162 | return -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | // Generated by CoffeeScript 1.6.3 2 | (function() { 3 | var app, async, http, should, _; 4 | 5 | _ = require("underscore"); 6 | 7 | should = require("should"); 8 | 9 | async = require("async"); 10 | 11 | app = require("../app"); 12 | 13 | http = require("../test/support/http"); 14 | 15 | describe('REST-rsmq Test', function() { 16 | var m1, m2, q1; 17 | before(function(done) { 18 | http.createServer(app, done); 19 | }); 20 | after(function(done) { 21 | done(); 22 | }); 23 | q1 = "mytestQueue"; 24 | m1 = null; 25 | m2 = null; 26 | it('POST /queues/mytestQueue should return 200 and create the queue', function(done) { 27 | http.request().post('/queues/' + q1).set('Content-Type', 'application/json').write(JSON.stringify({ 28 | vt: 20, 29 | maxsize: 2048 30 | })).end(function(resp) { 31 | var body; 32 | resp.statusCode.should.equal(200); 33 | body = JSON.parse(resp.body); 34 | body.result.should.equal(1); 35 | return done(); 36 | }); 37 | return; 38 | }); 39 | it('POST /messages/mytestQueue should return 200 and send message 1', function(done) { 40 | http.request().post('/messages/' + q1).set('Content-Type', 'application/json').write(JSON.stringify({ 41 | message: "Hello World!" 42 | })).end(function(resp) { 43 | var body; 44 | resp.statusCode.should.equal(200); 45 | body = JSON.parse(resp.body); 46 | body.id.length.should.equal(32); 47 | m1 = body.id; 48 | return done(); 49 | }); 50 | return; 51 | }); 52 | it('POST /messages/mytestQueue should return 200 and send message 2', function(done) { 53 | http.request().post('/messages/' + q1).set('Content-Type', 'application/json').write(JSON.stringify({ 54 | message: "Foo", 55 | delay: 20 56 | })).end(function(resp) { 57 | var body; 58 | resp.statusCode.should.equal(200); 59 | body = JSON.parse(resp.body); 60 | body.id.length.should.equal(32); 61 | m2 = body.id; 62 | return done(); 63 | }); 64 | return; 65 | }); 66 | it('GET /messages/mytestQueue should return message 1', function(done) { 67 | http.request().get('/messages/' + q1).end(function(resp) { 68 | var body; 69 | resp.statusCode.should.equal(200); 70 | body = JSON.parse(resp.body); 71 | body.id.should.equal(m1); 72 | done(); 73 | }); 74 | }); 75 | it('GET /messages/mytestQueue should not return a message', function(done) { 76 | http.request().get('/messages/' + q1).end(function(resp) { 77 | var body; 78 | resp.statusCode.should.equal(200); 79 | body = JSON.parse(resp.body); 80 | should.not.exist(body.id); 81 | done(); 82 | }); 83 | }); 84 | it('PUT /messages/mytestQueue/message2 to set vt to 0', function(done) { 85 | http.request().put('/messages/' + q1 + '/' + m2 + '?vt=0').end(function(resp) { 86 | var body; 87 | resp.statusCode.should.equal(200); 88 | body = JSON.parse(resp.body); 89 | body.result.should.equal(1.); 90 | done(); 91 | }); 92 | }); 93 | it('GET /messages/mytestQueue should return message 2', function(done) { 94 | http.request().get('/messages/' + q1).end(function(resp) { 95 | var body; 96 | resp.statusCode.should.equal(200); 97 | body = JSON.parse(resp.body); 98 | body.id.should.equal(m2); 99 | done(); 100 | }); 101 | }); 102 | it('GET /messages/mytestQueue should not return a message', function(done) { 103 | http.request().get('/messages/' + q1).end(function(resp) { 104 | var body; 105 | resp.statusCode.should.equal(200); 106 | body = JSON.parse(resp.body); 107 | should.not.exist(body.id); 108 | done(); 109 | }); 110 | }); 111 | it('DELETE /messages/mytestQueue/:message1 should delete message 1', function(done) { 112 | http.request()["delete"]('/messages/' + q1 + '/' + m1).end(function(resp) { 113 | var body; 114 | resp.statusCode.should.equal(200); 115 | body = JSON.parse(resp.body); 116 | body.result.should.equal(1); 117 | done(); 118 | }); 119 | }); 120 | it('DELETE /messages/mytestQueue/:message1 should fail', function(done) { 121 | http.request()["delete"]('/messages/' + q1 + '/' + m1).end(function(resp) { 122 | var body; 123 | resp.statusCode.should.equal(200); 124 | body = JSON.parse(resp.body); 125 | body.result.should.equal(0); 126 | done(); 127 | }); 128 | }); 129 | it('DELETE /messages/mytestQueue/:message2 should delete message 2', function(done) { 130 | http.request()["delete"]('/messages/' + q1 + '/' + m2).end(function(resp) { 131 | var body; 132 | resp.statusCode.should.equal(200); 133 | body = JSON.parse(resp.body); 134 | body.result.should.equal(1); 135 | done(); 136 | }); 137 | }); 138 | it('GET /queues/mytestQueue should return our queue attributes', function(done) { 139 | http.request().get('/queues/' + q1).end(function(resp) { 140 | var body; 141 | resp.statusCode.should.equal(200); 142 | body = JSON.parse(resp.body); 143 | body.maxsize.should.equal(2048); 144 | body.totalsent.should.equal(2); 145 | done(); 146 | }); 147 | }); 148 | it('GET /queues should return our queue name', function(done) { 149 | http.request().get('/queues').end(function(resp) { 150 | var body; 151 | resp.statusCode.should.equal(200); 152 | body = JSON.parse(resp.body); 153 | body.queues.should.containEql(q1); 154 | done(); 155 | }); 156 | }); 157 | it('DELETE /queues/mytestQueue should return 200 ', function(done) { 158 | http.request()["delete"]('/queues/' + q1).expect(200, done); 159 | }); 160 | }); 161 | 162 | }).call(this); 163 | --------------------------------------------------------------------------------