-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstep_implementation.js
96 lines (84 loc) · 2.54 KB
/
step_implementation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* globals gauge*/
"use strict";
var request = require("request"),
assert = require("assert");
var SERVER = process.env.API_BASE;
/**
* Step implementation to send messages
*/
gauge.step("Send message <msg>", function(msg, done) {
// Send a HTTP request to the server
request({ method: "POST", baseUrl: SERVER, uri: "/messages", json: { msg: msg } }, function (err, res, body) {
try {
// If request had error, throw it
if (err) {
throw err;
}
// If the response received was HTTP Status 200, then proceed
if (res.statusCode !== 200) {
throw new Error("HTTP Status " + res.statusCode + ". " + body);
}
// Assert on the content body of the API response
assert.equal("Ok", body);
done();
} catch (err) {
done(err);
}
});
});
/**
* Step implementation to retrieve messages
*/
gauge.step("Retrieve messages and validate <msgs>", function(msgs, done) {
var msglist = msgs.rows.map(function (row) {
return row.cells[0];
});
// Send a HTTP request to the server
request({ baseUrl: SERVER, uri: "/messages", json: true }, function (err, res, body) {
try {
// If request had error, throw it
if (err) {
throw err;
}
// If the response received was HTTP Status 200, then proceed
if (res.statusCode !== 200) {
throw new Error("HTTP Status " + res.statusCode + ". " + body);
}
gauge.message("Response received: " + body);
assert.equal(msglist.length, body.length, "Match number of messages retrieved");
if (msglist) {
// Assert on the content body of the API response
body.forEach(function (item, i) {
var parsedmsg = JSON.parse(item).msg;
assert.equal(parsedmsg, msglist[i], "Match message " + parsedmsg);
});
}
done();
} catch (err) {
done(err);
}
});
});
/**
* Step implementation to clear messages buffer
*/
gauge.step("Clear messages", function(done) {
// Send a HTTP request to the server
request({ method: "DELETE", baseUrl: SERVER, uri: "/messages", json: true }, function (err, res, body) {
try {
// If request had error, throw it
if (err) {
throw err;
}
// If the response received was HTTP Status 200, then proceed
if (res.statusCode !== 200) {
throw new Error("HTTP Status " + res.statusCode + ". " + body);
}
// Assert on the content body of the API response
assert.equal("Ok", body);
done();
} catch (err) {
done(err);
}
});
});