Skip to content

Commit 297e1db

Browse files
committed
test support setup
1 parent 7b0417c commit 297e1db

File tree

6 files changed

+135
-1
lines changed

6 files changed

+135
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"devDependencies": {
1717
"Faker" : "git://github.com/BryanDonovan/Faker.js.git#master",
1818
"jshint": "0.9.0",
19-
"jugglingdb":
19+
"jugglingdb": "0.2.0-2",
2020
"istanbul": "0.1.25",
2121
"mocha": "1.7.4",
2222
"sinon": "1.4.2"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var assert = require('assert');
2+
var support = require('../../support');
3+
4+
describe("Feature: User creation", function() {
5+
var http_client;
6+
7+
before(function() {
8+
http_client = support.http.client();
9+
});
10+
11+
context("Scenario: using a username and password", function() {
12+
context("Given a username does not already exist", function() {
13+
context("When an API client POSTs to /users with a valid username and password", function() {
14+
var response;
15+
16+
before(function(done) {
17+
var params = {
18+
username: support.random.string(),
19+
password: support.random.string()
20+
};
21+
22+
http_client.post('/users', params, function(err, result) {
23+
assert.ifError(err);
24+
result = response;
25+
done();
26+
});
27+
});
28+
29+
it("Then the response code should be 200", function() {
30+
assert.strictEqual(response.statusCode, 200);
31+
});
32+
33+
it("And the response data should include the user's ID", function(done) {
34+
assert.ok(response.data.id);
35+
});
36+
});
37+
});
38+
});
39+
});

test/mocha.opts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--reporter spec
2+
--ui bdd
3+
--globals state,newBlocks,params,type,__coverage__
4+
--timeout 6500
5+
--slow 200

test/support/http.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var restify = require('restify');
2+
var util = require('util');
3+
var host = process.env.HOST || 'localhost';
4+
var port = process.env.PORT;
5+
var ENV = process.env.NODE_ENV;
6+
7+
if (!port) {
8+
if (host === 'localhost') {
9+
port = '8080';
10+
} else {
11+
port = '80';
12+
}
13+
}
14+
15+
function HttpClient(args) {
16+
args = args || {};
17+
console.log(args);
18+
if (!args.host) {
19+
throw new Error('HTTP Client requires a host param');
20+
}
21+
22+
this.host = args.host;
23+
this.protocol = args.protocol || 'http';
24+
this.port = args.port;
25+
this.url = 'http://' + this.host + ':' + this.port;
26+
27+
this.client = restify.createStringClient({url: this.url});
28+
}
29+
30+
HttpClient.prototype.get = function(path_or_options, cb) {
31+
this.client.get(path_or_options, function(err, req, res, data) {
32+
cb(err, data, req, res);
33+
});
34+
};
35+
36+
HttpClient.prototype.post = function(path_or_options, body, cb) {
37+
this.client.post(path_or_options, body, function(err, req, res, data) {
38+
cb(err, data, req, res);
39+
});
40+
};
41+
42+
HttpClient.prototype.put = function(path_or_options, body, cb) {
43+
this.client.put(path_or_options, body, function(err, req, res, data) {
44+
cb(err, data, req, res);
45+
});
46+
};
47+
48+
HttpClient.prototype.del = function(path_or_options, cb) {
49+
this.client.del(path_or_options, function(err, req, res, data) {
50+
cb(err, data, req, res);
51+
});
52+
};
53+
54+
var http = {
55+
client: function() {
56+
return new HttpClient({host: host, port: port});
57+
}
58+
};
59+
60+
module.exports = http;
61+

test/support/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
http: require('./http'),
3+
random: require('./random')
4+
};

test/support/random.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var Faker = require('Faker');
2+
3+
var random = {
4+
number: function(max) {
5+
max = max || 10000;
6+
return Faker.Helpers.randomNumber(max);
7+
},
8+
9+
string: function(str_len) {
10+
str_len = str_len || 8;
11+
var chars = "abcdefghiklmnopqrstuvwxyz";
12+
var random_str = '';
13+
for (var i = 0; i < str_len; i++) {
14+
var rnum = Math.floor(Math.random() * chars.length);
15+
random_str += chars.substring(rnum, rnum + 1);
16+
}
17+
return random_str;
18+
},
19+
20+
email: function() {
21+
return this.string() + '+' + this.string() + '@' + 'example.com';
22+
}
23+
};
24+
25+
module.exports = random;

0 commit comments

Comments
 (0)