-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathcanary.js
86 lines (80 loc) · 2.41 KB
/
canary.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
var synthetics = require('Synthetics');
const log = require('SyntheticsLogger');
const axios = require('axios');
const PAGE_URL = process.env['WEBPAGE_URL'];
const API_ENDPOINT = process.env['API_ENDPOINT'];
const loadPage = async function () {
let page = await synthetics.getPage();
const response = await page.goto(PAGE_URL, {waitUntil: 'domcontentloaded', timeout: 10000});
if (!response) {
throw "Failed to load page!";
}
await page.waitForTimeout(5000);
await synthetics.takeScreenshot('loaded', 'loaded');
let pageTitle = await page.title();
log.info('Page title: ' + pageTitle);
if (response.status() !== 200) {
throw "Failed to load page!";
}
};
const getAllQuestionsApi = async function () {
log.info("Getting all questions");
const response = await axios({
url: '/api/trivia/all',
method: 'get',
baseURL: API_ENDPOINT,
headers: {
'User-Agent': synthetics.getCanaryUserAgentString()
}
});
log.info("Response:");
log.info(response);
if (response.status != 200) {
throw "Failed to load questions!";
} else if (response.data.length != 4) {
throw "Wrong number of categories!";
}
};
const getQuestionApi = async function () {
log.info("Getting question 1");
const response = await axios({
url: '/api/trivia/question/1',
method: 'get',
baseURL: API_ENDPOINT,
headers: {
'User-Agent': synthetics.getCanaryUserAgentString()
}
});
log.info("Response:");
log.info(response);
if (response.status != 200 ||
response.data.question != "What year was the first AWS re:Invent held?") {
throw "Failed to load question!";
}
};
const answerQuestionApi = async function () {
log.info("Answering question 1");
const response = await axios({
url: '/api/trivia/question/1',
method: 'post',
baseURL: API_ENDPOINT,
headers: {
'User-Agent': synthetics.getCanaryUserAgentString()
},
data: {
answer: '2012'
}
});
log.info("Response:");
log.info(response);
if (response.status != 200 ||
!response.data.result) {
throw "Failed to answer question!";
}
};
exports.handler = async () => {
await loadPage();
await getAllQuestionsApi();
await getQuestionApi();
await answerQuestionApi();
};