Implement Response URL

Implement feature to get response URL. Available for asserts, via script, or tests.
This commit is contained in:
Nikischin 2024-08-30 17:07:33 +02:00
parent c5c343c543
commit 812e56b78f
7 changed files with 54 additions and 8 deletions

View File

@ -30,11 +30,13 @@ if (!SERVER_RENDERED) {
'res.headers',
'res.body',
'res.responseTime',
'res.url',
'res.getStatus()',
'res.getHeader(name)',
'res.getHeaders()',
'res.getBody()',
'res.getResponseTime()',
'res.getUrl()',
'req',
'req.url',
'req.method',

View File

@ -262,6 +262,7 @@ const runSingleRequest = async function (
statusText: null,
headers: null,
data: null,
url: null,
responseTime: 0
},
error: err.message,
@ -389,6 +390,7 @@ const runSingleRequest = async function (
statusText: response.statusText,
headers: response.headers,
data: response.data,
url: response.request?.res?.responseUrl,
responseTime
},
error: null,
@ -413,6 +415,7 @@ const runSingleRequest = async function (
statusText: null,
headers: null,
data: null,
url: null,
responseTime: 0
},
error: err.message,

View File

@ -688,7 +688,8 @@ const registerNetworkIpc = (mainWindow) => {
data: response.data,
dataBuffer: dataBuffer.toString('base64'),
size: Buffer.byteLength(dataBuffer),
duration: responseTime ?? 0
duration: responseTime ?? 0,
url: response.request?.res?.responseUrl
};
} catch (error) {
deleteCancelToken(cancelTokenUid);
@ -764,7 +765,8 @@ const registerNetworkIpc = (mainWindow) => {
status: response.status,
statusText: response.statusText,
headers: response.headers,
data: response.data
data: response.data,
url: response.request?.res?.responseUrl
};
} catch (error) {
return Promise.reject(error);
@ -1026,7 +1028,8 @@ const registerNetworkIpc = (mainWindow) => {
dataBuffer: dataBuffer.toString('base64'),
size: Buffer.byteLength(dataBuffer),
data: response.data,
responseTime: response.headers.get('request-duration')
responseTime: response.headers.get('request-duration'),
url: response.request?.res?.responseUrl
},
...eventData
});

View File

@ -6,26 +6,31 @@ class BrunoResponse {
this.headers = res ? res.headers : null;
this.body = res ? res.data : null;
this.responseTime = res ? res.responseTime : null;
this.url = res ? res.request?.res?.responseUrl : null;
}
getStatus() {
return this.res ? this.res.status : null;
return this.res ? this.status : null;
}
getHeader(name) {
return this.res && this.res.headers ? this.res.headers[name] : null;
return this.res && this.headers ? this.headers[name] : null;
}
getHeaders() {
return this.res ? this.res.headers : null;
return this.res ? this.headers : null;
}
getBody() {
return this.res ? this.res.data : null;
return this.res ? this.data : null;
}
getResponseTime() {
return this.res ? this.res.responseTime : null;
return this.res ? this.responseTime : null;
}
getUrl() {
return this.res ? this.url : null;
}
}

View File

@ -7,16 +7,19 @@ const addBrunoResponseShimToContext = (vm, res) => {
const headers = marshallToVm(res?.headers, vm);
const body = marshallToVm(res?.body, vm);
const responseTime = marshallToVm(res?.responseTime, vm);
const url = marshallToVm(res?.url, vm);
vm.setProp(resObject, 'status', status);
vm.setProp(resObject, 'headers', headers);
vm.setProp(resObject, 'body', body);
vm.setProp(resObject, 'responseTime', responseTime);
vm.setProp(resObject, 'url', url);
status.dispose();
headers.dispose();
body.dispose();
responseTime.dispose();
url.dispose();
let getStatus = vm.newFunction('getStatus', function () {
return marshallToVm(res.getStatus(), vm);
@ -48,6 +51,12 @@ const addBrunoResponseShimToContext = (vm, res) => {
vm.setProp(resObject, 'getResponseTime', getResponseTime);
getResponseTime.dispose();
let getUrl = vm.newFunction('getUrl', function () {
return marshallToVm(res.getUrl(), vm);
});
vm.setProp(resObject, 'getUrl', getUrl);
getUrl.dispose();
vm.setProp(vm.global, 'res', resObject);
resObject.dispose();
};

View File

@ -115,6 +115,7 @@ const createResponseParser = (response = {}) => {
res.headers = response.headers;
res.body = response.data;
res.responseTime = response.responseTime;
res.url = response.request?.res?.responseUrl;
res.jq = (expr) => {
const output = jsonQuery(expr, { data: response.data });

View File

@ -0,0 +1,23 @@
meta {
name: getUrl
type: http
seq: 1
}
get {
url: {{host}}/ping
body: none
auth: none
}
assert {
res.status: eq 200
res.body: eq pong
}
tests {
test("res.getUrl()", function() {
const url = res.getUrl();
expect(url).to.equal("https://testbench-sanity.usebruno.com/ping");
});
}