Skip to content

Commit

Permalink
Added pm.response.headers.all() & pm.response.headers.get() (#126)
Browse files Browse the repository at this point in the history
* Added pm.response.headers.all() & pm.response.headers.get()
  • Loading branch information
thim81 committed Apr 1, 2024
1 parent fff0ae7 commit 453c4a6
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/apideck-libraries/postman-to-k6/compare/v1.8.7...HEAD)

## [1.9.0] - 2024-04-01

- Support for pm.response.headers.all() & pm.response.headers.get() (thanks to the contribution of @aegrey)

## [1.8.10] - 2024-01-05

### Security
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ variables, ... to K6 scripts that can be executed by K6 to run performance tests
This project is a friendly fork of the original [grafana/postman-to-k6](https://github.com/grafana/postman-to-k6)
converter, actively maintained and open for new contributions.

Feel free to contribute or pass any suggestion to improve postman-to-k6.

## Content

- [Features](#features)
Expand Down Expand Up @@ -325,7 +327,6 @@ $ postman-to-k6 example/v2/echo.json -o k6-script.js
- `pm.response.to.not.have.status(reason)`
- Properties returning Postman classes:
- `pm.request.url` `pm.request.headers`
- `pm.response.headers`
- The Hawk authentication method.
- Deprecated `xmlToJson` method.
- Request IDs are changed. Postman doesn't provide them in the export, so we have to generate new ones.
Expand Down
36 changes: 32 additions & 4 deletions lib/shim/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ String.prototype.has = function(v) {
};

/*
* Functional manipulation of frozen dicts
* Functional manipulation of frozen dictionaries
*
* These objects are frozen for consistency with Postman readonly semantics.
* The constructor property is forced clear to keep it as clean as possible.
Expand Down Expand Up @@ -147,11 +147,13 @@ function requireEnvironment() {
throw new Error('Missing Postman environment');
}
}

function requireRequest() {
if (!state.request) {
throw new Error('May only be used in a request scope');
}
}

function requirePost() {
if (!state.post) {
throw new Error('May only be used in a postrequest script');
Expand Down Expand Up @@ -461,12 +463,19 @@ const pm = Object.freeze({

/* Response information */
response: Object.freeze({
headers: Object.freeze({
get(name) {
const header = store.response.headers.cased[name];
return header ? header : null;
},
all() {
const headers = store.response.headers.cased;
return headers ? headers : null;
},
}),
get code() {
return store.response.code;
},
get headers() {
throw new Error('pm.response.headers not supported');
},
json() {
parseBodyJson();
const body = store.response.body;
Expand Down Expand Up @@ -601,6 +610,7 @@ const pm = Object.freeze({
}
},
});

function exposePm() {
const exposed = {
environment: pm.environment,
Expand Down Expand Up @@ -917,6 +927,7 @@ const responseCode = Object.freeze({
function xml2Json(xml) {
throw new Error('To use xml2Json import ./libs/shim/xml2Json.js');
}

function xmlToJson(xml) {
throw new Error('Deprecated function xmlToJson not supported');
}
Expand All @@ -934,6 +945,7 @@ function parseBodyJson() {
body.json = false;
}
}

function deepEqual(x, y) {
// From https://stackoverflow.com/a/32922084/10086414
const ok = Object.keys;
Expand All @@ -944,6 +956,7 @@ function deepEqual(x, y) {
ok(x).every(key => deepEqual(x[key], y[key]))
: x === y;
}

function jsonPath(value, path) {
try {
return eval(`value.${path}`); /* eslint-disable-line no-eval */
Expand Down Expand Up @@ -976,6 +989,7 @@ function evaluate(text) {
}

/* Request execution */

/**
* @typedef {object} RequestParams
* @param {string} name - Request name.
Expand Down Expand Up @@ -1039,6 +1053,7 @@ function executeRequest({
exitRequest();
}
}

function namedRequest(name, index = 1) {
const requests = definition.request[name];
if (!requests) {
Expand All @@ -1050,6 +1065,7 @@ function namedRequest(name, index = 1) {
const config = requests[index - 1];
return executeRequest(config);
}

function makeRequestConfig(method, address, data, headers, options, tags) {
const config = {};
config.method = method || null;
Expand Down Expand Up @@ -1086,6 +1102,7 @@ function evaluateAddress(address) {
return evaluate(address);
}
}

function defaultProtocol(addressText) {
if (!postman[Extend].module.urijs) {
throw new Error(
Expand All @@ -1099,13 +1116,15 @@ function defaultProtocol(addressText) {
}
return address.toString();
}

function evaluateDataObject(data) {
for (const key of Object.keys(data)) {
if (typeof data[key] === 'string') {
data[key] = evaluate(data[key]);
}
}
}

function makeRequestArgs({ method, address, data, headers, options }) {
// Set K6 request params from setting
if (setting.options) {
Expand All @@ -1119,6 +1138,7 @@ function makeRequestArgs({ method, address, data, headers, options }) {
options.headers = requestHeaders;
return [method, address, data, options];
}

function enterRequest(name, id, method, address, data, headers) {
state.request = true;
store.request.id = id;
Expand All @@ -1141,6 +1161,7 @@ function enterRequest(name, id, method, address, data, headers) {
require,
});
}

function enterPost(response = {}) {
state.post = true;
Object.assign(store.response, {
Expand All @@ -1162,15 +1183,18 @@ function enterPost(response = {}) {
}
exposePm();
}

function enterTest() {
state.test = true;
exposePm();
}

function exitTest() {
state.test = false;
store.test = [];
exposePm();
}

function exitRequest() {
state.request = false;
state.post = false;
Expand Down Expand Up @@ -1207,6 +1231,7 @@ function exitRequest() {
require: standard.require,
});
}

function translateCookies(cookies) {
for (const key of Object.keys(cookies)) {
// Postman semantics have no duplicate cookie names. Duplicates discarded.
Expand All @@ -1216,6 +1241,7 @@ function translateCookies(cookies) {
store.response.cookies.simple[cookie.name] = cookie.value;
}
}

function translateCookie(cookie) {
return {
domain: cookie.domain,
Expand All @@ -1235,6 +1261,7 @@ function translateCookie(cookie) {
value: cookie.value,
};
}

function executePrerequest(outer, inner) {
const scripts = [...outer];
if (inner) {
Expand All @@ -1244,6 +1271,7 @@ function executePrerequest(outer, inner) {
script();
}
}

function executePostrequest(outer, inner, response) {
const scripts = [...outer];
if (inner) {
Expand Down
29 changes: 26 additions & 3 deletions test/com/shim/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,34 @@ test.serial('pm.response.code', t => {
});
});

test.serial('pm.response.headers', t => {
test.serial('pm.response.headers.get', t => {
http.request.returns({
headers: {
'Content-Type': 'application/json',
'Server': 'GitHub Copilot'
}
});
postman[Request]({
post() {
t.throws(() => {
pm.response.headers; /* eslint-disable-line no-unused-expressions */
t.deepEqual(pm.response.headers.get('Server'),
'GitHub Copilot'
);
}
});
});

test.serial('pm.response.headers.all', t => {
http.request.returns({
headers: {
'Content-Type': 'application/json',
'Server': 'GitHub Copilot'
}
});
postman[Request]({
post() {
t.deepEqual(pm.response.headers.all(), {
'Content-Type': 'application/json',
'Server': 'GitHub Copilot'
});
}
});
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2014,9 +2014,9 @@ camelcase@^6.2.0:
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==

caniuse-lite@^1.0.30001248:
version "1.0.30001374"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001374.tgz"
integrity sha512-mWvzatRx3w+j5wx/mpFN5v5twlPrabG8NqX2c6e45LCpymdoGqNvRkRutFUqpRTXKFQFNQJasvK0YT7suW6/Hw==
version "1.0.30001603"
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001603.tgz"
integrity sha512-iL2iSS0eDILMb9n5yKQoTBim9jMZ0Yrk8g0N9K7UzYyWnfIKzXBZD5ngpM37ZcL/cv0Mli8XtVMRYMQAfFpi5Q==

chai@^4.3.4:
version "4.3.6"
Expand Down

0 comments on commit 453c4a6

Please sign in to comment.