2018-03-28 22:04:59 +00:00
|
|
|
import {Before, When, Then, TableDefinition} from "cucumber";
|
2017-07-26 21:45:26 +00:00
|
|
|
import seleniumWebdriver = require("selenium-webdriver");
|
|
|
|
import Assert = require("assert");
|
2017-10-22 15:42:05 +00:00
|
|
|
import Request = require("request-promise");
|
|
|
|
import Bluebird = require("bluebird");
|
|
|
|
|
2018-03-28 22:04:59 +00:00
|
|
|
Before(function () {
|
|
|
|
this.jar = Request.jar();
|
|
|
|
});
|
2017-10-22 15:42:05 +00:00
|
|
|
|
2018-03-28 22:04:59 +00:00
|
|
|
Then("I get an error {int}", function (code: number) {
|
|
|
|
return this.getErrorPage(code);
|
|
|
|
});
|
2017-10-22 15:42:05 +00:00
|
|
|
|
2018-03-28 22:04:59 +00:00
|
|
|
When("I request {string} with method {string}",
|
|
|
|
function (url: string, method: string) {
|
|
|
|
const that = this;
|
|
|
|
});
|
2017-10-22 15:42:05 +00:00
|
|
|
|
2018-03-28 22:04:59 +00:00
|
|
|
function requestAndExpectStatusCode(ctx: any, url: string, method: string,
|
|
|
|
expectedStatusCode: number) {
|
|
|
|
return Request(url, {
|
|
|
|
method: method,
|
|
|
|
jar: ctx.jar
|
|
|
|
})
|
|
|
|
.then(function (body: string) {
|
|
|
|
return Bluebird.resolve(parseInt(body.match(/Error ([0-9]{3})/)[1]));
|
|
|
|
}, function (response: any) {
|
|
|
|
return Bluebird.resolve(response.statusCode)
|
2017-10-22 15:42:05 +00:00
|
|
|
})
|
2018-03-28 22:04:59 +00:00
|
|
|
.then(function (statusCode: number) {
|
|
|
|
try {
|
|
|
|
Assert.equal(statusCode, expectedStatusCode);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
console.log(url);
|
|
|
|
console.log("%s (actual) != %s (expected)", statusCode,
|
|
|
|
expectedStatusCode);
|
|
|
|
throw e;
|
2017-10-22 15:42:05 +00:00
|
|
|
}
|
|
|
|
})
|
2018-03-28 22:04:59 +00:00
|
|
|
}
|
2017-10-22 15:42:05 +00:00
|
|
|
|
2018-03-28 22:04:59 +00:00
|
|
|
Then("I get the following status code when requesting:",
|
|
|
|
function (dataTable: TableDefinition) {
|
|
|
|
const promises: Bluebird<void>[] = [];
|
2017-10-22 15:42:05 +00:00
|
|
|
for (let i = 0; i < dataTable.rows().length; i++) {
|
2018-03-28 22:04:59 +00:00
|
|
|
const url: string = (dataTable.hashes() as any)[i].url;
|
|
|
|
const method: string = (dataTable.hashes() as any)[i].method;
|
|
|
|
const code: number = (dataTable.hashes() as any)[i].code;
|
|
|
|
promises.push(requestAndExpectStatusCode(this, url, method, code));
|
2017-10-22 15:42:05 +00:00
|
|
|
}
|
2018-03-28 22:04:59 +00:00
|
|
|
return Bluebird.all(promises);
|
2017-10-22 15:42:05 +00:00
|
|
|
})
|
2018-03-28 22:04:59 +00:00
|
|
|
|
|
|
|
When("I post {string} with body:", function (url: string,
|
|
|
|
dataTable: TableDefinition) {
|
|
|
|
const body = {};
|
|
|
|
for (let i = 0; i < dataTable.rows().length; i++) {
|
|
|
|
const key = (dataTable.hashes() as any)[i].key;
|
|
|
|
const value = (dataTable.hashes() as any)[i].value;
|
|
|
|
body[key] = value;
|
|
|
|
}
|
|
|
|
return Request.post(url, {
|
|
|
|
body: body,
|
|
|
|
jar: this.jar,
|
|
|
|
json: true
|
|
|
|
});
|
2017-07-26 21:45:26 +00:00
|
|
|
});
|