Skip to content

Commit bec47ec

Browse files
daniel-jones-devJanEbbing
authored andcommitted
test: add test case for custom options
1 parent 094b641 commit bec47ec

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

Diff for: tests/core.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export const testTimeout = 60000;
201201

202202
// Base URL for mocking out HTTP client
203203
export const urlToMockRegexp =
204-
/(https?:\/\/api.*\.deepl\.com)|(deepl-mock:\d+)|(https?:\/\/localhost:\d+)/;
204+
/(https?:\/\/api.*\.deepl\.com)|(deepl-mock:\d+)|(https?:\/\/localhost:\d+)/;
205205

206206
module.exports = {
207207
exampleText,

Diff for: tests/translateDocument.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import * as deepl from 'deepl-node';
66

7+
import nock from 'nock';
8+
79
import fs from 'fs';
810
import path from 'path';
911
import util from 'util';
@@ -17,9 +19,11 @@ import {
1719
tempFiles,
1820
testTimeout,
1921
timeout,
22+
urlToMockRegexp,
2023
withMockServer,
2124
withRealServer,
2225
} from './core';
26+
import { DocumentTranslateOptions, QuotaExceededError } from 'deepl-node';
2327

2428
describe('translate document', () => {
2529
it(
@@ -261,4 +265,33 @@ describe('translate document', () => {
261265
const translator = makeTranslator();
262266
await expect(translator.getDocumentStatus(handle)).rejects.toThrow('Not found');
263267
});
268+
269+
describe('request parameter tests', () => {
270+
beforeAll(() => {
271+
nock.disableNetConnect();
272+
});
273+
274+
it('sends extra request parameters', async () => {
275+
nock(urlToMockRegexp)
276+
.post('/v2/document', function (body) {
277+
// Nock unfortunately does not support proper form data matching
278+
// See https://github.com/nock/nock/issues/887
279+
// And https://github.com/nock/nock/issues/191
280+
expect(body).toContain('form-data');
281+
expect(body).toContain('my-extra-parameter');
282+
expect(body).toContain('my-extra-value');
283+
return true;
284+
})
285+
.reply(456);
286+
const translator = makeTranslator();
287+
const dataBuf = Buffer.from('Example file contents', 'utf8');
288+
const options: DocumentTranslateOptions = {
289+
filename: 'example.txt',
290+
extraRequestParameters: { 'my-extra-parameter': 'my-extra-value' },
291+
};
292+
await expect(translator.uploadDocument(dataBuf, null, 'de', options)).rejects.toThrow(
293+
QuotaExceededError,
294+
);
295+
});
296+
});
264297
});

Diff for: tests/translateText.test.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44

55
import * as deepl from 'deepl-node';
66

7-
import { exampleText, makeTranslator, testTimeout, withMockServer, withRealServer } from './core';
7+
import nock from 'nock';
8+
9+
import {
10+
exampleText,
11+
makeTranslator,
12+
testTimeout,
13+
urlToMockRegexp,
14+
withMockServer,
15+
withRealServer,
16+
} from './core';
17+
import { QuotaExceededError, TranslateTextOptions } from 'deepl-node';
818

919
describe('translate text', () => {
1020
it('should translate a single text', async () => {
@@ -235,4 +245,26 @@ describe('translate text', () => {
235245
expect(result.text).toContain('<h1>Meine erste Überschrift</h1>');
236246
expect(result.text).toContain('<p translate="no">My first paragraph.</p>');
237247
});
248+
249+
describe('request parameter tests', () => {
250+
beforeAll(() => {
251+
nock.disableNetConnect();
252+
});
253+
254+
it('sends extra request parameters', async () => {
255+
nock(urlToMockRegexp)
256+
.post('/v2/translate', function (body) {
257+
expect(body['my-extra-parameter']).toBe('my-extra-value');
258+
return true;
259+
})
260+
.reply(456);
261+
const translator = makeTranslator();
262+
const options: TranslateTextOptions = {
263+
extraRequestParameters: { 'my-extra-parameter': 'my-extra-value' },
264+
};
265+
await expect(translator.translateText('test', null, 'de', options)).rejects.toThrow(
266+
QuotaExceededError,
267+
);
268+
});
269+
});
238270
});

0 commit comments

Comments
 (0)