-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveOutputs.js
More file actions
68 lines (55 loc) · 2.66 KB
/
Copy pathsaveOutputs.js
File metadata and controls
68 lines (55 loc) · 2.66 KB
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
////////////////////////////////////
// File: saveOutputs.js
// Developer: Arthur Dibe
///////////////////////////////////
const fetch = require('node-fetch')
const fs = require('fs');
// function called from within testAPI function
const testRoute = async (testNumber, url)=>
{
const options = {
"method": "GET",
}
responseTEST = await fetch(url, options).then(resp => resp.json())
// result to append to file
const result = `[TEST ${testNumber}] - ${url}\n
RESPONSE: ${JSON.stringify(responseTEST, null, '\t')}\n\n`
// Save result in the file
fs.appendFile('output.txt', result, (err)=> {
if (err) throw err;
});
}
// Function called from server.js
module.exports = generateOutputs = async()=>
{
// RESET the file to start over
fs.writeFile('output.txt', "",(err)=> {
if(err) throw err;
})
// -----------> TEST 1 - 404 route not found
testRoute('1.1','http://localhost:3000/')
testRoute('1.2','http://localhost:3000/postings')
testRoute('1.3','http://localhost:3000/posts')
testRoute('1.4','http://localhost:3000/api')
testRoute('1.5','http://localhost:3000/posts/api')
// TEST 2 - PING
testRoute('2','http://localhost:3000/api/ping')
// TEST 3 - /api/posts
const tagsExist = "politics,tech,health,history,startups,science,design,culture"
const tagsNotExist = "book,study,duty,paint,tv,warp,way,food"
const sortOptions = ["id", "reads", "likes", "popularity"]
const directOptions = ["asc", "desc"]
// -----------> TEST 3.1 - no TAGS provided
testRoute('3.1.1',`http://localhost:3000/api/posts`)
testRoute('3.1.2',`http://localhost:3000/api/posts?tags`)
sortOptions.forEach(option => testRoute('3.1.3',`http://localhost:3000/api/posts?tags&sortBy=${option}`))
directOptions.forEach(option => testRoute('3.1.4',`http://localhost:3000/api/posts?tags&direction=${option}`))
// -----------> TEST 3.2 - TAGS NOT EXIST
testRoute('3.2.1',`http://localhost:3000/api/posts?tags=${tagsNotExist}`)
sortOptions.forEach(option => testRoute('3.2.2',`http://localhost:3000/api/posts?tags=${tagsNotExist}&sortBy=${option}`))
directOptions.forEach(option => testRoute('3.2.3',`http://localhost:3000/api/posts?tags=${tagsNotExist}&direction=${option}`))
// -----------> TEST 3.3 - TAGS EXIST
testRoute('3.3.1',`http://localhost:3000/api/posts?tags=${tagsExist}`)
sortOptions.forEach(sortOpt => testRoute('3.3.2',`http://localhost:3000/api/posts?tags=${tagsExist}&sortBy=${sortOpt}&direction=asc`))
sortOptions.forEach(sortOpt => testRoute('3.3.3',`http://localhost:3000/api/posts?tags=${tagsExist}&sortBy=${sortOpt}&direction=desc`))
}