Skip to content

Commit 21b0839

Browse files
test: add coverage for untested WebClient and IncomingWebhook behavior (#2581)
1 parent 9fa2921 commit 21b0839

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

packages/web-api/src/WebClient.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from 'node:assert/strict';
22
import fs from 'node:fs';
33
import { afterEach, beforeEach, describe, it } from 'node:test';
4+
import zlib from 'node:zlib';
45
import type { ContextActionsBlock } from '@slack/types';
56
import axios, { type InternalAxiosRequestConfig } from 'axios';
67
import nock, { type ReplyHeaders } from 'nock';
@@ -465,6 +466,44 @@ describe('WebClient', () => {
465466
}
466467
});
467468

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+
468507
it('should properly serialize simple API arguments', async () => {
469508
const scope = nock('https://slack.com')
470509
// NOTE: this could create false negatives if the serialization order changes (it shouldn't matter)

packages/webhook/src/IncomingWebhook.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,25 @@ describe('IncomingWebhook', () => {
116116
}
117117
});
118118
});
119+
120+
describe('User-Agent header', () => {
121+
it('should send the User-Agent header with every request', async () => {
122+
const scope = nock('https://hooks.slack.com', {
123+
reqheaders: {
124+
'User-Agent': (value) => {
125+
return /@slack:webhook/.test(value);
126+
},
127+
},
128+
})
129+
.post(/services/)
130+
.reply(200, 'ok');
131+
try {
132+
const webhook = new IncomingWebhook(url);
133+
await webhook.send('Hello');
134+
} finally {
135+
scope.done();
136+
}
137+
});
138+
});
119139
});
120140
});

0 commit comments

Comments
 (0)