|
1 | 1 | import assert from 'node:assert/strict'; |
2 | 2 | import fs from 'node:fs'; |
3 | 3 | import { afterEach, beforeEach, describe, it } from 'node:test'; |
| 4 | +import zlib from 'node:zlib'; |
4 | 5 | import type { ContextActionsBlock } from '@slack/types'; |
5 | 6 | import axios, { type InternalAxiosRequestConfig } from 'axios'; |
6 | 7 | import nock, { type ReplyHeaders } from 'nock'; |
@@ -465,6 +466,44 @@ describe('WebClient', () => { |
465 | 466 | } |
466 | 467 | }); |
467 | 468 |
|
| 469 | + it('should set error.body to the raw string when HTTP error response is not valid JSON', async () => { |
| 470 | + const htmlBody = '<html><body><h1>502 Bad Gateway</h1></body></html>'; |
| 471 | + const scope = nock('https://slack.com').post(/api/).reply(502, htmlBody); |
| 472 | + const client = new WebClient(token, { retryConfig: { retries: 0 } }); |
| 473 | + try { |
| 474 | + await client.apiCall('method'); |
| 475 | + assert.fail('expected error to be thrown'); |
| 476 | + } catch (error) { |
| 477 | + const e = error as WebAPIHTTPError; |
| 478 | + assert.strictEqual(e.code, ErrorCode.HTTPError); |
| 479 | + assert.strictEqual(e.statusCode, 502); |
| 480 | + assert.strictEqual(e.body, htmlBody); |
| 481 | + } finally { |
| 482 | + scope.done(); |
| 483 | + } |
| 484 | + }); |
| 485 | + |
| 486 | + describe('admin.analytics.getFile GZIP response', () => { |
| 487 | + it('should decompress GZIP response and return file_data array', async () => { |
| 488 | + const fileData = [ |
| 489 | + { date: '2024-01-01', user_id: 'U123', messages_posted: 5 }, |
| 490 | + { date: '2024-01-01', user_id: 'U456', messages_posted: 10 }, |
| 491 | + ]; |
| 492 | + const ndjson = fileData.map((d) => JSON.stringify(d)).join('\n'); |
| 493 | + const gzipped = zlib.gzipSync(Buffer.from(ndjson)); |
| 494 | + const scope = nock('https://slack.com') |
| 495 | + .post('/api/admin.analytics.getFile') |
| 496 | + .reply(200, gzipped, { 'content-type': 'application/gzip' }); |
| 497 | + try { |
| 498 | + const client = new WebClient(token, { retryConfig: rapidRetryPolicy }); |
| 499 | + const result = await client.apiCall('admin.analytics.getFile', { type: 'member' }); |
| 500 | + assert.deepStrictEqual(result.file_data, fileData); |
| 501 | + } finally { |
| 502 | + scope.done(); |
| 503 | + } |
| 504 | + }); |
| 505 | + }); |
| 506 | + |
468 | 507 | it('should properly serialize simple API arguments', async () => { |
469 | 508 | const scope = nock('https://slack.com') |
470 | 509 | // NOTE: this could create false negatives if the serialization order changes (it shouldn't matter) |
|
0 commit comments