|
| 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 | +}) |
0 commit comments