|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 | import {
|
| 17 | + Attributes, |
17 | 18 | StatusCode,
|
18 | 19 | ROOT_CONTEXT,
|
19 | 20 | SpanKind,
|
@@ -308,4 +309,145 @@ describe('Utility', () => {
|
308 | 309 | assert.deepEqual(attributes[HttpAttribute.HTTP_ROUTE], undefined);
|
309 | 310 | });
|
310 | 311 | });
|
| 312 | + // Verify the key in the given attributes is set to the given value, |
| 313 | + // and that no other HTTP Content Length attributes are set. |
| 314 | + function verifyValueInAttributes( |
| 315 | + attributes: Attributes, |
| 316 | + key: string | undefined, |
| 317 | + value: number |
| 318 | + ) { |
| 319 | + const httpAttributes = [ |
| 320 | + HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED, |
| 321 | + HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH, |
| 322 | + HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED, |
| 323 | + HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH, |
| 324 | + ]; |
| 325 | + |
| 326 | + for (const attr of httpAttributes) { |
| 327 | + if (attr === key) { |
| 328 | + assert.strictEqual(attributes[attr], value); |
| 329 | + } else { |
| 330 | + assert.strictEqual(attributes[attr], undefined); |
| 331 | + } |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + describe('setRequestContentLengthAttributes()', () => { |
| 336 | + it('should set request content-length uncompressed attribute with no content-encoding header', () => { |
| 337 | + const attributes: Attributes = {}; |
| 338 | + const request = {} as IncomingMessage; |
| 339 | + |
| 340 | + request.headers = { |
| 341 | + 'content-length': '1200', |
| 342 | + }; |
| 343 | + utils.setRequestContentLengthAttribute(request, attributes); |
| 344 | + |
| 345 | + verifyValueInAttributes( |
| 346 | + attributes, |
| 347 | + HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED, |
| 348 | + 1200 |
| 349 | + ); |
| 350 | + }); |
| 351 | + |
| 352 | + it('should set request content-length uncompressed attribute with "identity" content-encoding header', () => { |
| 353 | + const attributes: Attributes = {}; |
| 354 | + const request = {} as IncomingMessage; |
| 355 | + request.headers = { |
| 356 | + 'content-length': '1200', |
| 357 | + 'content-encoding': 'identity', |
| 358 | + }; |
| 359 | + utils.setRequestContentLengthAttribute(request, attributes); |
| 360 | + |
| 361 | + verifyValueInAttributes( |
| 362 | + attributes, |
| 363 | + HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED, |
| 364 | + 1200 |
| 365 | + ); |
| 366 | + }); |
| 367 | + |
| 368 | + it('should set request content-length compressed attribute with "gzip" content-encoding header', () => { |
| 369 | + const attributes: Attributes = {}; |
| 370 | + const request = {} as IncomingMessage; |
| 371 | + request.headers = { |
| 372 | + 'content-length': '1200', |
| 373 | + 'content-encoding': 'gzip', |
| 374 | + }; |
| 375 | + utils.setRequestContentLengthAttribute(request, attributes); |
| 376 | + |
| 377 | + verifyValueInAttributes( |
| 378 | + attributes, |
| 379 | + HttpAttribute.HTTP_REQUEST_CONTENT_LENGTH, |
| 380 | + 1200 |
| 381 | + ); |
| 382 | + }); |
| 383 | + }); |
| 384 | + |
| 385 | + describe('setResponseContentLengthAttributes()', () => { |
| 386 | + it('should set response content-length uncompressed attribute with no content-encoding header', () => { |
| 387 | + const attributes: Attributes = {}; |
| 388 | + |
| 389 | + const response = {} as IncomingMessage; |
| 390 | + |
| 391 | + response.headers = { |
| 392 | + 'content-length': '1200', |
| 393 | + }; |
| 394 | + utils.setResponseContentLengthAttribute(response, attributes); |
| 395 | + |
| 396 | + verifyValueInAttributes( |
| 397 | + attributes, |
| 398 | + HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED, |
| 399 | + 1200 |
| 400 | + ); |
| 401 | + }); |
| 402 | + |
| 403 | + it('should set response content-length uncompressed attribute with "identity" content-encoding header', () => { |
| 404 | + const attributes: Attributes = {}; |
| 405 | + |
| 406 | + const response = {} as IncomingMessage; |
| 407 | + |
| 408 | + response.headers = { |
| 409 | + 'content-length': '1200', |
| 410 | + 'content-encoding': 'identity', |
| 411 | + }; |
| 412 | + |
| 413 | + utils.setResponseContentLengthAttribute(response, attributes); |
| 414 | + |
| 415 | + verifyValueInAttributes( |
| 416 | + attributes, |
| 417 | + HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED, |
| 418 | + 1200 |
| 419 | + ); |
| 420 | + }); |
| 421 | + |
| 422 | + it('should set response content-length compressed attribute with "gzip" content-encoding header', () => { |
| 423 | + const attributes: Attributes = {}; |
| 424 | + |
| 425 | + const response = {} as IncomingMessage; |
| 426 | + |
| 427 | + response.headers = { |
| 428 | + 'content-length': '1200', |
| 429 | + 'content-encoding': 'gzip', |
| 430 | + }; |
| 431 | + |
| 432 | + utils.setResponseContentLengthAttribute(response, attributes); |
| 433 | + |
| 434 | + verifyValueInAttributes( |
| 435 | + attributes, |
| 436 | + HttpAttribute.HTTP_RESPONSE_CONTENT_LENGTH, |
| 437 | + 1200 |
| 438 | + ); |
| 439 | + }); |
| 440 | + |
| 441 | + it('should set no attributes with no content-length header', () => { |
| 442 | + const attributes: Attributes = {}; |
| 443 | + const message = {} as IncomingMessage; |
| 444 | + |
| 445 | + message.headers = { |
| 446 | + 'content-encoding': 'gzip', |
| 447 | + }; |
| 448 | + utils.setResponseContentLengthAttribute(message, attributes); |
| 449 | + |
| 450 | + verifyValueInAttributes(attributes, undefined, 1200); |
| 451 | + }); |
| 452 | + }); |
311 | 453 | });
|
0 commit comments