Skip to content

Commit a774cc5

Browse files
author
Martin Kuba
committed
tests: added tests for latest version of hapi
1 parent 22c9dcb commit a774cc5

File tree

9 files changed

+901
-0
lines changed

9 files changed

+901
-0
lines changed
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
2+
// hapi 10.x and higher works on Node 4 and higher
3+
var semver = require('semver')
4+
if (semver.satisfies(process.versions.node, '<4.0')) return
5+
6+
var path = require('path')
7+
var test = require('tap').test
8+
var request = require('request')
9+
var helper = require(path.join(__dirname, '..', '..', 'lib', 'agent_helper.js'))
10+
11+
12+
13+
test("Hapi capture params support", function (t) {
14+
t.plan(4)
15+
16+
t.test("simple case with no params", function (t) {
17+
var agent = helper.instrumentMockedAgent()
18+
var hapi = require('hapi')
19+
var server = new hapi.Server()
20+
21+
server.connection({
22+
host: 'localhost',
23+
port: 8089
24+
})
25+
26+
27+
// disabled by default
28+
agent.config.capture_params = true
29+
30+
agent.on('transactionFinished', function (transaction) {
31+
t.ok(transaction.trace, 'transaction has a trace.')
32+
if (transaction.trace.parameters.httpResponseMessage) {
33+
t.deepEqual(transaction.trace.parameters, {
34+
"request.headers.accept" : "application/json",
35+
"request.headers.host" : "localhost:8089",
36+
"request.method" : "GET",
37+
"response.status" : 200,
38+
"httpResponseCode": "200",
39+
"httpResponseMessage": "OK",
40+
}, 'parameters should only have request/response params')
41+
} else {
42+
t.deepEqual(transaction.trace.parameters, {
43+
"request.headers.accept" : "application/json",
44+
"request.headers.host" : "localhost:8089",
45+
"request.method" : "GET",
46+
"response.status" : 200,
47+
"httpResponseCode": "200",
48+
}, 'parameters should only have request/response params')
49+
}
50+
51+
helper.unloadAgent(agent)
52+
server.stop(function () {
53+
t.end()
54+
})
55+
})
56+
57+
server.route({
58+
method : 'GET',
59+
path : '/test/',
60+
handler : function (request, reply) {
61+
t.ok(agent.getTransaction(), "transaction is available")
62+
63+
reply({status : 'ok'})
64+
}
65+
})
66+
67+
server.start(function () {
68+
request.get('http://localhost:8089/test/',
69+
{json : true},
70+
function (error, res, body) {
71+
72+
t.equal(res.statusCode, 200, "nothing exploded")
73+
t.deepEqual(body, {status : 'ok'}, "got expected response")
74+
})
75+
})
76+
})
77+
78+
t.test("case with route params", function (t) {
79+
var agent = helper.instrumentMockedAgent()
80+
var hapi = require('hapi')
81+
var server = new hapi.Server()
82+
83+
server.connection({
84+
host: 'localhost',
85+
port: 8089
86+
})
87+
88+
89+
// disabled by default
90+
agent.config.capture_params = true
91+
92+
agent.on('transactionFinished', function (transaction) {
93+
t.ok(transaction.trace, 'transaction has a trace.')
94+
if (transaction.trace.parameters.httpResponseMessage) {
95+
t.deepEqual(transaction.trace.parameters, {
96+
"request.headers.accept" : "application/json",
97+
"request.headers.host" : "localhost:8089",
98+
"request.method" : "GET",
99+
"response.status" : 200,
100+
"httpResponseCode": "200",
101+
"httpResponseMessage": "OK",
102+
"id" : "1337",
103+
}, 'parameters should have id')
104+
} else {
105+
t.deepEqual(transaction.trace.parameters, {
106+
"request.headers.accept" : "application/json",
107+
"request.headers.host" : "localhost:8089",
108+
"request.method" : "GET",
109+
"response.status" : 200,
110+
"httpResponseCode": "200",
111+
"id" : "1337",
112+
}, 'parameters should have id')
113+
}
114+
115+
helper.unloadAgent(agent)
116+
server.stop(function () {
117+
t.end()
118+
})
119+
})
120+
121+
server.route({
122+
method : 'GET',
123+
path : '/test/{id}/',
124+
handler : function (request, reply) {
125+
t.ok(agent.getTransaction(), "transaction is available")
126+
127+
reply({status : 'ok'})
128+
}
129+
})
130+
131+
server.start(function () {
132+
request.get('http://localhost:8089/test/1337/',
133+
{json : true},
134+
function (error, res, body) {
135+
136+
t.equal(res.statusCode, 200, "nothing exploded")
137+
t.deepEqual(body, {status : 'ok'}, "got expected response")
138+
})
139+
})
140+
})
141+
142+
t.test("case with query params", function (t) {
143+
var agent = helper.instrumentMockedAgent()
144+
var hapi = require('hapi')
145+
var server = new hapi.Server()
146+
147+
server.connection({
148+
host: 'localhost',
149+
port: 8089
150+
})
151+
152+
153+
// disabled by default
154+
agent.config.capture_params = true
155+
156+
agent.on('transactionFinished', function (transaction) {
157+
t.ok(transaction.trace, 'transaction has a trace.')
158+
if (transaction.trace.parameters.httpResponseMessage) {
159+
t.deepEqual(transaction.trace.parameters, {
160+
"request.headers.accept" : "application/json",
161+
"request.headers.host" : "localhost:8089",
162+
"request.method" : "GET",
163+
"response.status" : 200,
164+
"httpResponseCode": "200",
165+
"httpResponseMessage": "OK",
166+
"name" : "hapi"
167+
}, 'parameters should have name')
168+
} else {
169+
t.deepEqual(transaction.trace.parameters, {
170+
"request.headers.accept" : "application/json",
171+
"request.headers.host" : "localhost:8089",
172+
"request.method" : "GET",
173+
"response.status" : 200,
174+
"httpResponseCode": "200",
175+
"name" : "hapi"
176+
}, 'parameters should have name')
177+
}
178+
179+
helper.unloadAgent(agent)
180+
server.stop(function () {
181+
t.end()
182+
})
183+
})
184+
185+
server.route({
186+
method : 'GET',
187+
path : '/test/',
188+
handler : function (request, reply) {
189+
t.ok(agent.getTransaction(), "transaction is available")
190+
191+
reply({status : 'ok'})
192+
}
193+
})
194+
195+
server.start(function () {
196+
request.get('http://localhost:8089/test/?name=hapi',
197+
{json : true},
198+
function (error, res, body) {
199+
200+
t.equal(res.statusCode, 200, "nothing exploded")
201+
t.deepEqual(body, {status : 'ok'}, "got expected response")
202+
})
203+
})
204+
})
205+
206+
t.test("case with both route and query params", function (t) {
207+
var agent = helper.instrumentMockedAgent()
208+
var hapi = require('hapi')
209+
var server = new hapi.Server()
210+
211+
server.connection({
212+
host: 'localhost',
213+
port: 8089
214+
})
215+
216+
217+
// disabled by default
218+
agent.config.capture_params = true
219+
220+
agent.on('transactionFinished', function (transaction) {
221+
t.ok(transaction.trace, 'transaction has a trace.')
222+
if (transaction.trace.parameters.httpResponseMessage) {
223+
t.deepEqual(transaction.trace.parameters, {
224+
"request.headers.accept" : "application/json",
225+
"request.headers.host" : "localhost:8089",
226+
"request.method" : "GET",
227+
"response.status" : 200,
228+
"httpResponseCode": "200",
229+
"httpResponseMessage": "OK",
230+
"id" : "1337",
231+
"name" : "hapi"
232+
}, 'parameters should have name and id')
233+
} else {
234+
t.deepEqual(transaction.trace.parameters, {
235+
"request.headers.accept" : "application/json",
236+
"request.headers.host" : "localhost:8089",
237+
"request.method" : "GET",
238+
"response.status" : 200,
239+
"httpResponseCode": "200",
240+
"id" : "1337",
241+
"name" : "hapi"
242+
}, 'parameters should have name and id')
243+
}
244+
245+
helper.unloadAgent(agent)
246+
server.stop(function () {
247+
t.end()
248+
})
249+
})
250+
251+
server.route({
252+
method : 'GET',
253+
path : '/test/{id}/',
254+
handler : function (request, reply) {
255+
t.ok(agent.getTransaction(), "transaction is available")
256+
257+
reply({status : 'ok'})
258+
}
259+
})
260+
261+
server.start(function () {
262+
request.get('http://localhost:8089/test/1337/?name=hapi',
263+
{json : true},
264+
function (error, res, body) {
265+
266+
t.equal(res.statusCode, 200, "nothing exploded")
267+
t.deepEqual(body, {status : 'ok'}, "got expected response")
268+
})
269+
})
270+
})
271+
})
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict'
2+
3+
// hapi 10.x and higher works on Node 4 and higher
4+
var semver = require('semver')
5+
if (semver.satisfies(process.versions.node, '<4.0')) return
6+
7+
var path = require('path')
8+
, test = require('tap').test
9+
, request = require('request')
10+
, helper = require(path.join(__dirname, '..', '..', 'lib', 'agent_helper.js'))
11+
, API = require(path.join(__dirname, '..', '..', '..', 'api.js'))
12+
13+
14+
test("ignoring a Hapi route", function (t) {
15+
t.plan(7)
16+
17+
var agent = helper.instrumentMockedAgent()
18+
, api = new API(agent)
19+
, hapi = require('hapi')
20+
, server = new hapi.Server()
21+
22+
server.connection({
23+
host: 'localhost',
24+
port: 8089
25+
})
26+
27+
28+
this.tearDown(function () {
29+
server.stop(function () {
30+
helper.unloadAgent(agent)
31+
})
32+
})
33+
34+
agent.on('transactionFinished', function (transaction) {
35+
t.equal(transaction.name, 'WebTransaction/Hapi/GET//order/{id}',
36+
"transaction has expected name even on error")
37+
t.ok(transaction.ignore, "transaction is ignored")
38+
39+
t.notOk(agent.traces.trace, "should have no transaction trace")
40+
41+
var metrics = agent.metrics.unscoped
42+
t.equal(Object.keys(metrics).length, 1,
43+
"only supportability metrics added to agent collection"
44+
)
45+
46+
var errors = agent.errors.errors
47+
t.equal(errors.length, 0, "no errors noticed")
48+
})
49+
50+
server.route({
51+
method : 'GET',
52+
path : '/order/{id}',
53+
handler : function (request, reply) {
54+
api.setIgnoreTransaction(true)
55+
56+
reply({status : 'cartcartcart'}).code(400)
57+
}
58+
})
59+
60+
server.start(function () {
61+
request.get('http://localhost:8089/order/31337',
62+
{json : true},
63+
function (error, res, body) {
64+
65+
t.equal(res.statusCode, 400, "got expected error")
66+
t.deepEqual(body, {status : 'cartcartcart'}, "got expected response")
67+
})
68+
})
69+
})

0 commit comments

Comments
 (0)