Skip to content

Commit

Permalink
fixup! fix(mon-pix): create request manager service
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot committed Nov 8, 2024
1 parent 9165b88 commit 197bcf8
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions mon-pix/tests/unit/services/request-manager-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ module('Unit | Service | request-manager', function (hooks) {
// then
assert.strictEqual(result.response.status, 200);
assert.deepEqual(result.content, { foo: 'bar' });
assert.strictEqual(result.request.headers.get('Accept-Language'), 'fr');
assert.strictEqual(result.request.headers.get('X-App-Version'), 'development');
assert.strictEqual(result.request.headers.get('Accept'), 'application/json');
assert.strictEqual(result.request.headers.get('Content-Type'), 'application/json');

const [url, { headers }] = window.fetch.getCall(0).args;
assert.strictEqual(url, '/test');
assert.strictEqual(headers.get('Accept-Language'), 'fr');
assert.strictEqual(headers.get('X-App-Version'), 'development');
assert.strictEqual(headers.get('Accept'), 'application/json');
assert.strictEqual(headers.get('Content-Type'), 'application/json');
});

module('when user is authenticated', function () {
Expand All @@ -54,26 +57,12 @@ module('Unit | Service | request-manager', function (hooks) {
sinon.stub(sessionService, 'data').value({ authenticated: { access_token: 'baz' } });

// when
const result = await requestManagerService.request({ url: '/test', method: 'GET' });
await requestManagerService.request({ url: '/test', method: 'GET' });

// then
assert.strictEqual(result.request.headers.get('Authorization'), 'Bearer baz');
});
});

module('when a body object is set for POST', function () {
test('it is serialized as string', async function (assert) {
// given
window.fetch.resolves(responseMock({ status: 200, data: { foo: 'bar' } }));

// when
const result = await requestManagerService.request({ url: '/test', method: 'POST', body: { bar: 'baz' } });

// then
sinon.assert.calledWithMatch(window.fetch, '/test', { body: '{"bar":"baz"}' });
assert.strictEqual(result.request.body, '{"bar":"baz"}');

assert.ok(true);
const [url, { headers }] = window.fetch.getCall(0).args;
assert.strictEqual(url, '/test');
assert.strictEqual(headers.get('Authorization'), 'Bearer baz');
});
});

Expand All @@ -84,10 +73,12 @@ module('Unit | Service | request-manager', function (hooks) {
sinon.stub(currentDomainService, 'isFranceDomain').value(true);

// when
const result = await requestManagerService.request({ url: '/test', method: 'GET' });
await requestManagerService.request({ url: '/test', method: 'GET' });

// then
assert.strictEqual(result.request.headers.get('Accept-Language'), 'fr-fr');
const [url, { headers }] = window.fetch.getCall(0).args;
assert.strictEqual(url, '/test');
assert.strictEqual(headers.get('Accept-Language'), 'fr-fr');
});
});

Expand Down

0 comments on commit 197bcf8

Please sign in to comment.