@appello/services
Version:
Services package with api / graphql
69 lines (68 loc) • 2.78 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const setAuthorizationHeader_1 = require("./setAuthorizationHeader");
describe('setAuthorizationHeader', () => {
it('should add Authorization header to empty headers object', () => {
const token = 'test-token-123';
const headers = {};
const result = (0, setAuthorizationHeader_1.setAuthorizationHeader)(token, headers);
expect(result).toEqual({
Authorization: 'Bearer test-token-123',
});
});
it('should add Authorization header to existing headers without overriding them', () => {
const token = 'my-access-token';
const headers = {
'Content-Type': 'application/json',
Accept: 'application/json',
};
const result = (0, setAuthorizationHeader_1.setAuthorizationHeader)(token, headers);
expect(result).toEqual({
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: 'Bearer my-access-token',
});
});
it('should override existing Authorization header', () => {
const token = 'new-token';
const headers = {
'Content-Type': 'application/json',
Authorization: 'Bearer old-token',
};
const result = (0, setAuthorizationHeader_1.setAuthorizationHeader)(token, headers);
expect(result).toEqual({
'Content-Type': 'application/json',
Authorization: 'Bearer new-token',
});
});
it('should not mutate the original headers object', () => {
const token = 'test-token';
const originalHeaders = {
'Content-Type': 'application/json',
};
const result = (0, setAuthorizationHeader_1.setAuthorizationHeader)(token, originalHeaders);
expect(originalHeaders).toEqual({
'Content-Type': 'application/json',
});
expect(result).not.toBe(originalHeaders);
});
it('should handle empty token string', () => {
const token = '';
const headers = {
'Content-Type': 'application/json',
};
const result = (0, setAuthorizationHeader_1.setAuthorizationHeader)(token, headers);
expect(result).toEqual({
'Content-Type': 'application/json',
Authorization: 'Bearer ',
});
});
it('should handle special characters in token', () => {
const token = 'token-with-special@#$%^&*()_+characters';
const headers = {};
const result = (0, setAuthorizationHeader_1.setAuthorizationHeader)(token, headers);
expect(result).toEqual({
Authorization: 'Bearer token-with-special@#$%^&*()_+characters',
});
});
});