Skip to content

Commit f91e43e

Browse files
test: add Accept-Language header tests for Client
1 parent c2ee9b2 commit f91e43e

1 file changed

Lines changed: 74 additions & 0 deletions

File tree

src/networking/__tests__/index.spec.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ describe('client', () => {
4848
it('should fail with no domain', () => {
4949
expect(() => new Client()).toThrowErrorMatchingSnapshot();
5050
});
51+
52+
it('should store acceptLanguage option if provided', () => {
53+
const lang = 'fr-CA';
54+
const client = new Client({ baseUrl, acceptLanguage: lang });
55+
expect(client.acceptLanguage).toEqual(lang);
56+
});
5157
});
5258

5359
describe('requests', () => {
@@ -219,6 +225,74 @@ describe('client', () => {
219225
});
220226
});
221227

228+
describe('Accept-Language Header', () => {
229+
const path = '/test-endpoint';
230+
const mockResponse = { status: 200, body: { success: true } };
231+
const defaultClient = new Client({
232+
baseUrl,
233+
telemetry: {name: 'react-native-auth0', version: '1.0.0'},
234+
token: 'a.bearer.token',
235+
});
236+
beforeEach(fetchMock.restore);
237+
it('should NOT set Accept-Language header if option is not provided', async () => {
238+
fetchMock.getOnce(`https://${domain}${path}`, mockResponse);
239+
await defaultClient.get(path);
240+
241+
const requestOptions = fetchMock.lastOptions();
242+
const headers = requestOptions.headers;
243+
expect(headers.get('Accept-Language')).toBeNull();
244+
});
245+
246+
it('should set Accept-Language header if option IS provided', async () => {
247+
const lang = 'es-ES,es;q=0.9';
248+
const clientWithLang = new Client({
249+
baseUrl,
250+
telemetry: { name: 'react-native-auth0', version: '1.0.0' },
251+
token: 'a.bearer.token',
252+
acceptLanguage: lang, // Provide the option
253+
});
254+
255+
fetchMock.getOnce(`https://${domain}${path}`, mockResponse);
256+
await clientWithLang.get(path);
257+
258+
const requestOptions = fetchMock.lastOptions();
259+
const headers = requestOptions.headers;
260+
expect(headers.get('Accept-Language')).toEqual(lang);
261+
});
262+
263+
it('should set Accept-Language header for POST requests if option is provided', async () => {
264+
const lang = 'fr';
265+
const clientWithLang = new Client({
266+
baseUrl,
267+
acceptLanguage: lang,
268+
});
269+
const body = { data: 'test' };
270+
271+
fetchMock.postOnce(`https://${domain}${path}`, mockResponse);
272+
await clientWithLang.post(path, body);
273+
274+
const requestOptions = fetchMock.lastOptions();
275+
const headers = requestOptions.headers;
276+
expect(headers.get('Accept-Language')).toEqual(lang);
277+
});
278+
279+
it('should set Accept-Language header for PATCH requests if option is provided', async () => {
280+
const lang = 'de-DE';
281+
const clientWithLang = new Client({
282+
baseUrl,
283+
acceptLanguage: lang,
284+
});
285+
const body = { data: 'update' };
286+
287+
fetchMock.patchOnce(`https://${domain}${path}`, mockResponse);
288+
await clientWithLang.patch(path, body);
289+
290+
const requestOptions = fetchMock.lastOptions();
291+
const headers = requestOptions.headers;
292+
expect(headers.get('Accept-Language')).toEqual(lang);
293+
});
294+
});
295+
222296
describe('url', () => {
223297
const client = new Client({
224298
baseUrl,

0 commit comments

Comments
 (0)