Skip to content

Commit

Permalink
Add more tests for request checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Sannis committed May 25, 2015
1 parent a7fd0fd commit 665561d
Showing 1 changed file with 80 additions and 24 deletions.
104 changes: 80 additions & 24 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* See license text in LICENSE file
*/

/*global describe, it*/
/*global describe, it, beforeEach, afterEach */

var assert = require("assert");
var http = require("http");
Expand Down Expand Up @@ -43,54 +43,110 @@ function testJinbaRequestResponse(jinbaRequest, onResponseEnd) {
request.end();
}

function assertErrorResponse(requestJson, expectedErrorSubstring, done) {
var JS = JinbaServer.createJinbaServer(30002, '127.0.0.1', true);
JS.listen(3000, '127.0.0.1');

function assertResponse(requestJson, onResponseJson) {
testJinbaRequestResponse(requestJson, function (responseText) {
var responseJson;
try {
responseJson = JSON.parse(responseText);
} catch (e) {
assert.fail("Cannot parse Jinba server response JSON");
JS.close(done);
return;
}

assert.ok(
"error" in responseJson,
"Response does not contains error description"
);
onResponseJson(responseText, responseJson);
});
}

assert.ok(
responseJson.error.indexOf(expectedErrorSubstring) !== -1,
"Response error description does not contains substring: " + expectedErrorSubstring
);
function assertResponseGlobalError(requestJson, expectedErrorSubstring) {
assertResponse(
requestJson,
function (responseText, responseJson) {
assert.ok(
"error" in responseJson,
"Response does not contains error description: " + responseText
);

assert.ok(
responseJson.error.indexOf(expectedErrorSubstring) !== -1,
"Response error description '" + responseJson.error + "' does not contains substring '" + expectedErrorSubstring + "'"
);
}
);
}

JS.close(done);
});
function assertResponseLocalError(requestJson, expectedErrorSubstring) {
assertResponse(
requestJson,
function (responseText, responseJson) {
assert.ok(
(responseJson instanceof Array) && (responseJson.length == 1),
"Response should be an array with one object inside: " + responseText
);

assert.ok(
"error" in responseJson[0],
"Response does not contains error description: " + responseText
);

assert.ok(
responseJson[0].error.indexOf(expectedErrorSubstring) !== -1,
"Response error description '" + responseJson[0].error + "' does not contains substring '" + expectedErrorSubstring + "'"
);
}
);
}

describe('JinbaServer', function () {
it('should export createJinbaServer() function', function () {
assert.ok(typeof JinbaServer.createJinbaServer === 'function');
});

it('should export createRequestListener() function', function () {
assert.ok(typeof JinbaServer.createRequestListener === 'function');
});

describe('RequestListener', function () {
it('should check for empty POST data', function (done) {
assertErrorResponse("", "Empty POST data", done);
var JS;

beforeEach(function () {
JS = JinbaServer.createJinbaServer(30002, '127.0.0.1', true);
JS.listen(3000, '127.0.0.1');
});

afterEach(function (done) {
JS.close(done);
});

it('should check for empty POST data', function () {
assertResponseGlobalError("", "Empty POST data");
});

it('should check for broken JSON', function () {
assertResponseGlobalError("[}", "Cannot parse incoming JSON");
});

it('should check for wring JSON', function () {
assertResponseGlobalError("{}", "Incoming JSON is not array");
});

it('should check for broken JSON', function (done) {
assertErrorResponse("[}", "Cannot parse incoming JSON", done);
it('should check for empty JSON', function () {
assertResponseGlobalError("[]", "Incoming JSON is empty");
});

it('should check for wring JSON', function (done) {
assertErrorResponse("{}", "Incoming JSON is not array", done);
it('should check for Jinba requests structure', function () {
assertResponseLocalError([{}], "request.name is not set");
});

it('should check for empty JSON', function (done) {
assertErrorResponse("[]", "Incoming JSON is empty", done);
it('should check for Jinba requests timer values', function () {
assertResponseLocalError(
[
{
"name": "/",
"value": 1000000000
}
],
"request.value is out of limits"
);
});
});
});

0 comments on commit 665561d

Please sign in to comment.