-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.js
148 lines (133 loc) · 3.83 KB
/
index.test.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import test from 'ava'
import got from 'got'
import * as app from './index.js'
const HTTP_PORT = 3001
const STATS_BASE_URL = `http://localhost:${HTTP_PORT}`
const SITE_BASE_URL = `http://localhost:${HTTP_PORT}`
let server, memory
test.beforeEach(async () => {
await new Promise((resolve) => setTimeout(resolve, 200))
const result = await app.start({ HTTP_PORT, STATS_BASE_URL, SITE_BASE_URL, DATA_PATH: 'data/test.ljson' }, [])
server = result.server
memory = result.memory
})
test.afterEach(() => server.close())
test.serial('starts server (port 3001)', async t => {
t.is(server.address().port, HTTP_PORT)
const response = await got(`http://localhost:${HTTP_PORT}`)
t.is(response.statusCode, 200)
t.is(response.headers['content-type'], 'text/html; charset=UTF-8')
})
test.serial('blocks bot', async t => {
const response = await got.post(`http://localhost:${HTTP_PORT}/p`, {
headers: {
'User-Agent': 'HeadlessChrome User Agent'
},
body: JSON.stringify({
r: undefined,
p: 'http://foo.bar/baz',
w: 1280
})
})
t.is(response.statusCode, 200)
t.deepEqual(memory, [])
})
test.serial('tracks pageview', async t => {
const response = await got.post(`http://localhost:${HTTP_PORT}/p`, {
headers: {
'User-Agent': 'foo-user-agent'
},
body: JSON.stringify({
r: undefined,
p: 'http://foo.bar/baz',
w: 1280
})
})
t.is(response.statusCode, 200)
t.is(memory.length, 1)
t.is(memory[0].w, 1280)
t.is(memory[0].p, 'http://foo.bar/baz')
t.truthy(memory[0].d)
})
test.serial('tracks heartbeat', async t => {
const response = await got.post(`http://localhost:${HTTP_PORT}/p`, {
headers: {
'User-Agent': 'foo-user-agent'
},
body: JSON.stringify({
t: 'heartbeat',
r: undefined,
p: 'http://foo.bar/baz',
w: 1280
})
})
t.is(response.statusCode, 200)
})
test.serial('tracks event', async t => {
const response = await got.post(`http://localhost:${HTTP_PORT}/e`, {
headers: {
'User-Agent': 'foo-user-agent'
},
body: JSON.stringify({
t: 'event',
r: undefined,
p: 'http://foo.bar/baz',
w: 1280,
e: 'foo'
})
})
t.is(response.statusCode, 200)
t.is(memory.length, 1)
t.is(memory[0].w, 1280)
t.is(memory[0].p, 'http://foo.bar/baz')
t.truthy(memory[0].d)
t.is(memory[0].e, 'foo')
})
test.serial('returns pageviews last h', async t => {
await got.post(`http://localhost:${HTTP_PORT}/p`, {
headers: {
'User-Agent': 'foo-user-agent'
},
body: JSON.stringify({
r: undefined,
p: 'http://foo.bar/baz',
w: 1280
})
})
const response = await got(`http://localhost:${HTTP_PORT}/api/today`)
t.is(response.statusCode, 200)
const body = JSON.parse(response.body)
t.is(body.data.length, 1)
t.is(body.pages.length, 1)
t.is(body.referrers.length, 1)
t.is(body.chartData.length, 0)
t.is(Object.keys(body.live).length, 1)
})
test.serial('returns pageviews for url', async t => {
const response = await got(`http://localhost:${HTTP_PORT}/api/pageviews/baz`)
t.is(response.statusCode, 200)
const body = JSON.parse(response.body)
t.is(body, 0)
})
test.serial('returns live visitors', async t => {
await got.post(`http://localhost:${HTTP_PORT}/p`, {
headers: {
'User-Agent': 'foo-user-agent'
},
body: JSON.stringify({
r: undefined,
p: 'http://foo.bar/baz',
w: 1280
})
})
const response = await got(`http://localhost:${HTTP_PORT}/live`)
t.is(response.statusCode, 200)
const body = JSON.parse(response.body)
t.is(Object.keys(body).length, 1)
})
test.serial('returns tracker script', async t => {
const response = await got(`http://localhost:${HTTP_PORT}/client.js`)
t.is(response.statusCode, 200)
t.is(response.headers['content-type'], 'text/javascript')
t.true(response.body.includes(STATS_BASE_URL))
})