1
0
Fork 0

More tests.

This commit is contained in:
Alan Plum 2015-01-20 13:53:04 +01:00
parent 28070d1d41
commit 5accae0af3
2 changed files with 148 additions and 7 deletions

View File

@ -466,6 +466,7 @@ actions.defineHttp({
callback : function (req, res) {
res.responseCode = actions.HTTP_OK;
res.contentType = "application/json; charset=utf-8";
req.rawRequestBody = require('internal').rawRequestBody(req);
res.body = JSON.stringify(req);
}
});

View File

@ -31,7 +31,7 @@ var jsunity = require('jsunity');
var expect = require('expect.js');
var arangodb = require('org/arangodb');
var request = require('org/arangodb/request');
var fs = require('fs');
var qs = require('qs');
// -----------------------------------------------------------------------------
// --SECTION-- request
@ -42,8 +42,10 @@ var fs = require('fs');
////////////////////////////////////////////////////////////////////////////////
function RequestSuite () {
buildUrl = function (append) {
return arango.getEndpoint().replace(/^tcp:/, 'http:').replace(/^ssl:/, 'https:') + '/_admin/echo' + append;
buildUrl = function (append, base) {
base = base === false ? '' : '/_admin/echo';
append = append || '';
return arango.getEndpoint().replace(/^tcp:/, 'http:').replace(/^ssl:/, 'https:') + base + append;
};
buildUrlBroken = function (append) {
@ -97,15 +99,12 @@ function RequestSuite () {
testPostWithDefaults: function () {
var path = '/lol';
var body = {hello: 'world'};
var res = request.post(buildUrl(path), {body: body, json: true});
var res = request.post(buildUrl(path));
expect(res).to.be.a(request.Response);
expect(res.body).to.be.a('string');
expect(Number(res.headers['content-length'])).to.equal(res.rawBody.length);
var obj = JSON.parse(res.body);
expect(obj.path).to.equal(path);
expect(obj).to.have.property('requestBody');
expect(JSON.parse(obj.requestBody)).to.eql(body);
},
////////////////////////////////////////////////////////////////////////////////
@ -140,6 +139,147 @@ function RequestSuite () {
expect(obj.path).to.equal(path);
expect(obj).to.have.property('requestBody');
expect(JSON.parse(obj.requestBody)).to.eql(body);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test http request headers
////////////////////////////////////////////////////////////////////////////////
testRequestHeaders: function () {
var path = '/lol';
var headers = {
'content-type': 'application/x-magic',
'content-disposition': 'x-chaotic; mood=cheerful',
'x-hovercraft': 'full-of-eels'
};
var res = request.post(buildUrl(path), {headers: headers});
expect(res).to.be.a(request.Response);
var obj = JSON.parse(res.body);
expect(obj.path).to.equal(path);
expect(obj).to.have.property('headers');
Object.keys(headers).forEach(function (name) {
expect(obj.headers[name]).to.equal(headers[name]);
});
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test 404
////////////////////////////////////////////////////////////////////////////////
testError404: function () {
var url = buildUrlBroken('/lol');
expect(function () {
request.get(url);
}).to.throwException(function (err) {
expect(err).to.have.property('message', 'Not Found');
expect(err).to.have.property('statusCode', 404);
expect(err).to.have.property('status', 404);
expect(err).to.have.property('request');
expect(err).to.have.property('response');
expect(err.response).to.be.a(request.Response);
expect(err.response).to.have.property('statusCode', 404);
expect(err.response).to.have.property('status', 404);
expect(err.request).to.have.property('url', url);
});
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test http auth
////////////////////////////////////////////////////////////////////////////////
testBasicAuth: function () {
var path = '/lol';
var auth = {
username: 'jcd',
password: 'bionicman'
};
var res = request.post(buildUrl(path), {auth: auth});
expect(res).to.be.a(request.Response);
var obj = JSON.parse(res.body);
expect(obj.path).to.equal(path);
expect(obj).to.have.property('headers');
expect(obj.headers).to.have.property('authorization');
expect(obj.headers.authorization).to.equal(
'Basic ' + new Buffer(auth.username + ':' + auth.password).toString('base64')
);
},
testBearerAuth: function () {
var path = '/lol';
var auth = {
bearer: 'full of bears'
};
var res = request.post(buildUrl(path), {auth: auth});
expect(res).to.be.a(request.Response);
var obj = JSON.parse(res.body);
expect(obj.path).to.equal(path);
expect(obj).to.have.property('headers');
expect(obj.headers).to.have.property('authorization');
expect(obj.headers.authorization).to.equal('Bearer ' + auth.bearer);
},
////////////////////////////////////////////////////////////////////////////////
/// @brief test http request bodies
////////////////////////////////////////////////////////////////////////////////
testJsonBody: function () {
var path = '/lol';
var reqBody = {
hello: 'world',
answer: 42,
hovercraft: ['full', 'of', 'eels']
};
var res = request.post(buildUrl(path), {body: reqBody, json: true});
expect(res).to.be.a(request.Response);
var obj = JSON.parse(res.body);
expect(obj.path).to.equal(path);
expect(obj).to.have.property('requestBody');
expect(JSON.parse(obj.requestBody)).to.eql(reqBody);
},
testFormBody: function () {
var path = '/lol';
var reqBody = {
hello: 'world',
answer: '42',
hovercraft: ['full', 'of', 'eels']
};
var res = request.post(buildUrl(path), {form: qs.stringify(reqBody)});
expect(res).to.be.a(request.Response);
var obj = JSON.parse(res.body);
expect(obj.path).to.equal(path);
expect(obj).to.have.property('requestBody');
expect(qs.parse(obj.requestBody)).to.eql(reqBody);
},
testStringBody: function () {
var path = '/lol';
var reqBody = 'hello world';
var res = request.post(buildUrl(path), {body: reqBody});
expect(res).to.be.a(request.Response);
var obj = JSON.parse(res.body);
expect(obj.path).to.equal(path);
expect(obj).to.have.property('requestBody');
expect(obj.requestBody).to.equal(reqBody);
},
testBufferBody: function () {
var path = '/lol';
var reqBody = new Buffer('hello world');
var headers = {'content-type': 'application/octet-stream'};
var res = request.post(buildUrl(path), {body: reqBody, headers: headers});
expect(res).to.be.a(request.Response);
var obj = JSON.parse(res.body);
expect(obj.path).to.equal(path);
expect(obj).to.have.property('requestBody');
expect(obj.rawRequestBody).to.eql(reqBody.toJSON());
},
testBufferResponse: function () {
var path = '/_admin/aardvark/favicon.ico';
var res = request.get(buildUrl(path, false), {encoding: null});
expect(res).to.be.a(request.Response);
expect(res.body).to.be.a(Buffer);
}
// -----------------------------------------------------------------------------