mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-21 15:33:11 +01:00
feat: bru cookie fns
This commit is contained in:
parent
4894ac2754
commit
b8b39037ad
@ -75,7 +75,9 @@ if (!SERVER_RENDERED) {
|
||||
'bru.getRequestVar(key)',
|
||||
'bru.sleep(ms)',
|
||||
'bru.getGlobalEnvVar(key)',
|
||||
'bru.setGlobalEnvVar(key, value)'
|
||||
'bru.setGlobalEnvVar(key, value)',
|
||||
'bru.setCookie(cookieString, url);',
|
||||
'bru.getCookie(url);'
|
||||
];
|
||||
CodeMirror.registerHelper('hint', 'brunoJS', (editor, options) => {
|
||||
const cursor = editor.getCursor();
|
||||
|
@ -319,14 +319,6 @@ const configureRequest = async (
|
||||
|
||||
request.timeout = preferencesUtil.getRequestTimeout();
|
||||
|
||||
// add cookies to request
|
||||
if (preferencesUtil.shouldSendCookies()) {
|
||||
const cookieString = getCookieStringForUrl(request.url);
|
||||
if (cookieString && typeof cookieString === 'string' && cookieString.length) {
|
||||
request.headers['cookie'] = cookieString;
|
||||
}
|
||||
}
|
||||
|
||||
// Add API key to the URL
|
||||
if (request.apiKeyAuthValueForQueryParams && request.apiKeyAuthValueForQueryParams.placement === 'queryparams') {
|
||||
const urlObj = new URL(request.url);
|
||||
@ -445,6 +437,14 @@ const registerNetworkIpc = (mainWindow) => {
|
||||
}
|
||||
}
|
||||
|
||||
// add cookies to request
|
||||
if (preferencesUtil.shouldSendCookies()) {
|
||||
const cookieString = getCookieStringForUrl(request.url);
|
||||
if (cookieString && typeof cookieString === 'string' && cookieString.length) {
|
||||
request.headers['cookie'] = cookieString;
|
||||
}
|
||||
}
|
||||
|
||||
return scriptResult;
|
||||
};
|
||||
|
||||
|
@ -7,6 +7,7 @@ const path = require('path');
|
||||
const crypto = require('node:crypto');
|
||||
const { getTreePathFromCollectionToItem } = require('../../utils/collection');
|
||||
const { buildFormUrlEncodedPayload } = require('../../utils/common');
|
||||
const { addCookieToJar, getCookiesForUrl } = require('../../utils/cookies');
|
||||
|
||||
const mergeFolderLevelHeaders = (request, requestTreePath) => {
|
||||
let folderHeaders = new Map();
|
||||
@ -465,6 +466,9 @@ const prepareRequest = (item, collection) => {
|
||||
axiosRequest.globalEnvironmentVariables = request.globalEnvironmentVariables;
|
||||
axiosRequest.assertions = request.assertions;
|
||||
|
||||
axiosRequest.setCookiesForUrl = (cookie, url) => addCookieToJar(cookie, url);
|
||||
axiosRequest.getCookiesForUrl = (cookie, url) => getCookiesForUrl(cookie, url);
|
||||
|
||||
return axiosRequest;
|
||||
};
|
||||
|
||||
|
@ -3,18 +3,30 @@ const each = require('lodash/each');
|
||||
|
||||
const cookieJar = new CookieJar();
|
||||
|
||||
const addCookieToJar = (setCookieHeader, requestUrl) => {
|
||||
const normalizeUrl = (url) => {
|
||||
try {
|
||||
return new URL(url)?.toString?.();
|
||||
}
|
||||
catch(error) {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
const addCookieToJar = (setCookieHeader, _url) => {
|
||||
const url = normalizeUrl(_url);
|
||||
const cookie = Cookie.parse(setCookieHeader, { loose: true });
|
||||
cookieJar.setCookieSync(cookie, requestUrl, {
|
||||
cookieJar.setCookieSync(cookie, url, {
|
||||
ignoreError: true // silently ignore things like parse errors and invalid domains
|
||||
});
|
||||
};
|
||||
|
||||
const getCookiesForUrl = (url) => {
|
||||
const getCookiesForUrl = (_url) => {
|
||||
const url = normalizeUrl(_url);
|
||||
return cookieJar.getCookiesSync(url);
|
||||
};
|
||||
|
||||
const getCookieStringForUrl = (url) => {
|
||||
const getCookieStringForUrl = (_url) => {
|
||||
const url = normalizeUrl(_url);
|
||||
const cookies = getCookiesForUrl(url);
|
||||
|
||||
if (!Array.isArray(cookies) || !cookies.length) {
|
||||
|
@ -4,7 +4,7 @@ const { interpolate } = require('@usebruno/common');
|
||||
const variableNameRegex = /^[\w-.]*$/;
|
||||
|
||||
class Bru {
|
||||
constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables) {
|
||||
constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, setCookiesForUrl, getCookiesForUrl) {
|
||||
this.envVariables = envVariables || {};
|
||||
this.runtimeVariables = runtimeVariables || {};
|
||||
this.processEnvVars = cloneDeep(processEnvVars || {});
|
||||
@ -13,6 +13,8 @@ class Bru {
|
||||
this.requestVariables = requestVariables || {};
|
||||
this.globalEnvironmentVariables = globalEnvironmentVariables || {};
|
||||
this.collectionPath = collectionPath;
|
||||
this.setCookiesForUrl = setCookiesForUrl;
|
||||
this.getCookiesForUrl = getCookiesForUrl;
|
||||
}
|
||||
|
||||
_interpolate = (str) => {
|
||||
@ -138,6 +140,26 @@ class Bru {
|
||||
sleep(ms) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
getCookie(url) {
|
||||
let interpolatedUrl = this._interpolate(url);
|
||||
try {
|
||||
return this.getCookiesForUrl?.(interpolatedUrl);
|
||||
}
|
||||
catch(error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
setCookie(cookie, url) {
|
||||
let interpolatedUrl = this._interpolate(url);
|
||||
try {
|
||||
this.setCookiesForUrl?.(cookie, interpolatedUrl);
|
||||
}
|
||||
catch(error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Bru;
|
||||
|
@ -51,7 +51,8 @@ class ScriptRuntime {
|
||||
const collectionVariables = request?.collectionVariables || {};
|
||||
const folderVariables = request?.folderVariables || {};
|
||||
const requestVariables = request?.requestVariables || {};
|
||||
const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables);
|
||||
const { setCookiesForUrl, getCookiesForUrl } = request;
|
||||
const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, setCookiesForUrl, getCookiesForUrl);
|
||||
const req = new BrunoRequest(request);
|
||||
const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
|
||||
const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);
|
||||
@ -171,7 +172,8 @@ class ScriptRuntime {
|
||||
const collectionVariables = request?.collectionVariables || {};
|
||||
const folderVariables = request?.folderVariables || {};
|
||||
const requestVariables = request?.requestVariables || {};
|
||||
const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables);
|
||||
const { setCookiesForUrl, getCookiesForUrl } = request;
|
||||
const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, setCookiesForUrl, getCookiesForUrl);
|
||||
const req = new BrunoRequest(request);
|
||||
const res = new BrunoResponse(response);
|
||||
const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
|
||||
|
@ -52,7 +52,8 @@ class TestRuntime {
|
||||
const collectionVariables = request?.collectionVariables || {};
|
||||
const folderVariables = request?.folderVariables || {};
|
||||
const requestVariables = request?.requestVariables || {};
|
||||
const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables);
|
||||
const { setCookiesForUrl, getCookiesForUrl } = request;
|
||||
const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, setCookiesForUrl, getCookiesForUrl);
|
||||
const req = new BrunoRequest(request);
|
||||
const res = new BrunoResponse(response);
|
||||
const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
|
||||
|
@ -128,6 +128,18 @@ const addBruShimToContext = (vm, bru) => {
|
||||
});
|
||||
sleep.consume((handle) => vm.setProp(bruObject, 'sleep', handle));
|
||||
|
||||
let getCookie = vm.newFunction('getCookie', function (url) {
|
||||
return marshallToVm(bru.getCookie(vm.dump(url)), vm);
|
||||
});
|
||||
vm.setProp(bruObject, 'getCookie', getCookie);
|
||||
getCookie.dispose();
|
||||
|
||||
let setCookie = vm.newFunction('setCookie', function (cookie, url) {
|
||||
return marshallToVm(bru.setCookie(vm.dump(cookie), vm.dump(url)), vm);
|
||||
});
|
||||
vm.setProp(bruObject, 'setCookie', setCookie);
|
||||
setCookie.dispose();
|
||||
|
||||
vm.setProp(vm.global, 'bru', bruObject);
|
||||
bruObject.dispose();
|
||||
};
|
||||
|
31
packages/bruno-tests/collection/scripting/api/bru/cookie.bru
Normal file
31
packages/bruno-tests/collection/scripting/api/bru/cookie.bru
Normal file
@ -0,0 +1,31 @@
|
||||
meta {
|
||||
name: cookie
|
||||
type: http
|
||||
seq: 7
|
||||
}
|
||||
|
||||
post {
|
||||
url: {{echo-host}}
|
||||
body: json
|
||||
auth: none
|
||||
}
|
||||
|
||||
body:json {
|
||||
{
|
||||
"hello": "bruno"
|
||||
}
|
||||
}
|
||||
|
||||
script:pre-request {
|
||||
bru.setCookie('foo=bar', req.getUrl());
|
||||
}
|
||||
|
||||
tests {
|
||||
test("should contain cookie set by bru script", function() {
|
||||
const url = req.getUrl();
|
||||
const cookies = bru.getCookie(url);
|
||||
const cookie = cookies?.at?.(0);
|
||||
expect(cookie?.key).to.equal("foo");
|
||||
expect(cookie?.value).to.equal("bar");
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user