Skip to content

Commit

Permalink
chore: updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
helloanoop authored Jan 17, 2025
1 parent 3c71554 commit d16a679
Showing 1 changed file with 121 additions and 105 deletions.
226 changes: 121 additions & 105 deletions packages/bruno-cli/tests/runner/prepare-request.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { describe, it, expect } = require('@jest/globals');

const { describe, it, expect, beforeEach } = require('@jest/globals');
const prepareRequest = require('../../src/runner/prepare-request');

describe('prepare-request: prepareRequest', () => {
Expand All @@ -25,124 +24,141 @@ describe('prepare-request: prepareRequest', () => {

describe('Properly maps inherited auth from collectionRoot', () => {
// Initialize Test Fixtures
let collectionRoot, request;
let collection, item;

beforeEach(() => {
// Reset test fixtures
collectionRoot = {
request: {
auth: {}
}
};

request = {
url: 'https://www.usebruno.com',
method: 'POST',
auth: {
mode: "inherit"
},
body: {
mode: 'json',
json: '{\n"test": {{someVar}} // comment\n}'
}
};
});

it('If collection auth is apikey in header', () => {
collectionRoot.request.auth = {
mode: "apikey",
apikey: {
key: "x-api-key",
value: "{{apiKey}}",
placement: "header"
}
};

const result = prepareRequest(request, collectionRoot);
expect(result.headers).toHaveProperty('x-api-key', '{{apiKey}}');
});


it('If collection auth is apikey in header and request has existing headers', () => {
collectionRoot.request.auth = {
mode: "apikey",
apikey: {
key: "x-api-key",
value: "{{apiKey}}",
placement: "header"
collection = {
name: 'Test Collection',
root: {
request: {
auth: {}
}
}
};

request['headers'] = [{ name: 'Content-Type', value: 'application/json', enabled: true }];

const result = prepareRequest(request, collectionRoot);
expect(result.headers).toHaveProperty('Content-Type', 'application/json');
expect(result.headers).toHaveProperty('x-api-key', '{{apiKey}}');
});

it('If collection auth is apikey in query parameters', () => {
collectionRoot.request.auth = {
mode: "apikey",
apikey: {
key: "apiKey",
value: "{{apiKey}}",
placement: "queryparams"
item = {
name: 'Test Request',
type: 'http-request',
request: {
method: 'GET',
headers: [],
params: [],
url: 'https://usebruno.com',
auth: {
mode: 'inherit'
},
script: {
req: 'console.log("Pre Request")',
res: 'console.log("Post Response")'
}
}
};

const expected = `${request.url}?${collectionRoot.request.auth.apikey.key}=${collectionRoot.request.auth.apikey.value}`;
const result = prepareRequest(request, collectionRoot);
expect(result.url).toEqual(expected);
});

it('If request does not have auth configured', () => {
delete request.auth;
let result;
expect(() => {
result = prepareRequest(request, collectionRoot);
}).not.toThrow();
expect(result).toBeDefined();
describe('API Key Authentication', () => {
it('If collection auth is apikey in header', () => {
collection.root.request.auth = {
mode: "apikey",
apikey: {
key: "x-api-key",
value: "{{apiKey}}",
placement: "header"
}
};

const result = prepareRequest(item, collection);
expect(result.headers).toHaveProperty('x-api-key', '{{apiKey}}');
});

it('If collection auth is apikey in header and request has existing headers', () => {
collection.root.request.auth = {
mode: "apikey",
apikey: {
key: "x-api-key",
value: "{{apiKey}}",
placement: "header"
}
};

item.request.headers.push({ name: 'Content-Type', value: 'application/json', enabled: true });
const result = prepareRequest(item, collection);
expect(result.headers).toHaveProperty('Content-Type', 'application/json');
expect(result.headers).toHaveProperty('x-api-key', '{{apiKey}}');
});

it('If collection auth is apikey in query parameters', () => {
collection.root.request.auth = {
mode: "apikey",
apikey: {
key: "x-api-key",
value: "{{apiKey}}",
placement: "queryparams"
}
};

const urlObj = new URL(item.request.url);
urlObj.searchParams.set(collection.root.request.auth.apikey.key, collection.root.request.auth.apikey.value);

const expected = urlObj.toString();
const result = prepareRequest(item, collection);
expect(result.url).toEqual(expected);
});
});

it('If collection auth is basic auth', () => {
collectionRoot.request.auth = {
mode: 'basic',
basic: {
username: 'testUser',
password: 'testPass123'
}
};

const result = prepareRequest(request, collectionRoot);
const expected = { username: 'testUser', password: 'testPass123' };
expect(result.auth).toEqual(expected);
describe('Basic Authentication', () => {
it('If collection auth is basic auth', () => {
collection.root.request.auth = {
mode: 'basic',
basic: {
username: 'testUser',
password: 'testPass123'
}
};

const result = prepareRequest(item, collection);
const expected = { username: 'testUser', password: 'testPass123' };
expect(result.auth).toEqual(expected);
});
});

it('If collection auth is bearer token', () => {
collectionRoot.request.auth = {
mode: 'bearer',
bearer: {
token: 'token'
}
};

const result = prepareRequest(request, collectionRoot);
expect(result.headers).toHaveProperty('Authorization', 'Bearer token');
describe('Bearer Token Authentication', () => {
it('If collection auth is bearer token', () => {
collection.root.request.auth = {
mode: 'bearer',
bearer: {
token: 'token'
}
};

const result = prepareRequest(item, collection);
expect(result.headers).toHaveProperty('Authorization', 'Bearer token');
});

it('If collection auth is bearer token and request has existing headers', () => {
collection.root.request.auth = {
mode: 'bearer',
bearer: {
token: 'token'
}
};

item.request.headers.push({ name: 'Content-Type', value: 'application/json', enabled: true });

const result = prepareRequest(item, collection);
expect(result.headers).toHaveProperty('Authorization', 'Bearer token');
expect(result.headers).toHaveProperty('Content-Type', 'application/json');
});
});

it('If collection auth is bearer token and request has existing headers', () => {
collectionRoot.request.auth = {
mode: 'bearer',
bearer: {
token: 'token'
}
};

request['headers'] = [{ name: 'Content-Type', value: 'application/json', enabled: true }];

const result = prepareRequest(request, collectionRoot);
expect(result.headers).toHaveProperty('Authorization', 'Bearer token');
expect(result.headers).toHaveProperty('Content-Type', 'application/json');
describe('No Authentication', () => {
it('If request does not have auth configured', () => {
delete item.request.auth;
let result;
expect(() => {
result = prepareRequest(item, collection);
}).not.toThrow();
expect(result).toBeDefined();
});
});
});
});

0 comments on commit d16a679

Please sign in to comment.