Skip to content

Commit

Permalink
Initialise headers for each K6 request (#133)
Browse files Browse the repository at this point in the history
* Initialise headers for each K6 request

* Bump dependencies
  • Loading branch information
thim81 authored Jul 31, 2024
1 parent 2ce0417 commit 29a5bf9
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 1,182 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ 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)

- Fix initialization of headers for each K6 request (#98)

## [1.11.0] - 2024-07-12

- Handle mixing ESM and commonJS to be ready for k6 v0.53.0
Expand Down
7 changes: 6 additions & 1 deletion lib/shim/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ function makeRequestConfig(method, address, data, headers, options, tags) {
for (const key of Object.keys(headers)) {
headers[key] = evaluate(headers[key]);
}
config.headers = headers;
config.headers = Object.assign({}, headers);
} else {
config.headers = {};
}
Expand Down Expand Up @@ -1126,6 +1126,11 @@ function evaluateDataObject(data) {
}

function makeRequestArgs({ method, address, data, headers, options }) {
// Reinitialize setting.option.headers
if (setting.options && setting.options.headers) {
setting.options.headers = {};
}

// Set K6 request params from setting
if (setting.options) {
options = Object.assign(setting.options, options);
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"test-unit": "cross-env NODE_PATH=lib:test ava test/unit/**/*.js",
"test-jest": "jest --colors --verbose --reporters=default --collectCoverage --no-cache",
"prepare": "husky install",
"release": "yarn format && yarn lint && yarn bundle && np --branch main"
"release": "yarn format && yarn lint && yarn bundle && npx np --branch main"
},
"dependencies": {
"ajv": "^6.12.6",
Expand All @@ -62,16 +62,16 @@
"eslint-config-prettier": "^6.11.0",
"fs-extra": "^8.1.0",
"lodash": "^4.17.21",
"oauth-1.0a": "^2.2.5",
"oauth-1.0a": "^2.2.6",
"papaparse": "^5.3.1",
"pkginfo": "^0.4.1",
"postman-collection": "^4.3.0",
"postman-collection": "^4.4.1",
"postman-collection-transformer": "^4.1.8",
"prettier": "^1.19.1",
"spo-gpo": "^1.0.0",
"strip-json-comments": "^3.1.1",
"urijs": "^1.19.11",
"uuid": "^3.3.2",
"uuid": "^3.4.0",
"verror": "^1.10.0",
"xml2js": "^0.5.0"
},
Expand All @@ -85,7 +85,6 @@
"husky": "^7.0.4",
"jest": "^29.7.0",
"mock-require": "^3.0.3",
"np": "^7.6.2",
"npm-run-all": "^4.1.5",
"sinon": "^7.5.0",
"snazzy": "^8.0.0",
Expand Down
7 changes: 7 additions & 0 deletions test/int/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,10 @@ test('iterations', async t => {
});
t.snapshot(main);
});

test('multiple requests', async t => {
const [main] = await convertFile(
'test/material/2.1/multi-request-headers.json'
);
t.snapshot(main);
});
49 changes: 49 additions & 0 deletions test/int/snapshots/basic.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,52 @@ Generated by [AVA](https://ava.li).
});␊
}␊
`

## multiple requests

> Snapshot 1
`// Auto-generated by the postman-to-k6 converter␊
import "./libs/shim/core.js";␊
import { group } from "k6";␊
export let options = { maxRedirects: 4 };␊
const Request = Symbol.for("request");␊
postman[Symbol.for("initial")]({␊
options␊
});␊
export default function() {␊
group("Widgets", function() {␊
postman[Request]({␊
name: "Get Widget",␊
method: "GET",␊
address: "http://localhost:4010/widget",␊
headers: {␊
"X-Correlation-Id": "{{$randomUUID}}",␊
TimeStamp: "{{timestamp}}",␊
"Content-Signature": "{{signature_key}}"␊
},␊
auth(config, Var) {␊
config.headers.Authorization = "Bearer access_token";␊
}␊
});␊
});␊
group("Widget", function() {␊
postman[Request]({␊
name: "Create Widget",␊
method: "POST",␊
address: "http://localhost:4010/widget",␊
data:␊
'{\\n "isLogin": true,\\n "widget_id": "0",\\n "callFrom": "my-tax"\\n}',␊
headers: {␊
"X-Correlation-Id": "{{$randomUUID}}",␊
"Content-Signature": "{{signature_key}}"␊
}␊
});␊
});␊
}␊
`
Binary file modified test/int/snapshots/basic.js.snap
Binary file not shown.
155 changes: 155 additions & 0 deletions test/material/2.1/multi-request-headers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"info": {
"_postman_id": "5f0fab53-3ac7-4151-902d-b0c2855e8eb4",
"name": "98 - K6 Header",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "238711"
},
"item": [
{
"name": "Widgets",
"item": [
{
"name": "Get Widget",
"event": [
{
"listen": "test",
"script": {
"exec": [],
"type": "text/javascript",
"packages": {}
}
}
],
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "access_token",
"type": "string"
}
]
},
"method": "GET",
"header": [
{
"key": "X-Correlation-Id",
"value": "{{$randomUUID}}",
"type": "text"
},
{
"key": "TimeStamp",
"value": "{{timestamp}}",
"type": "text"
},
{
"key": "Content-Signature",
"value": "{{signature_key}}",
"type": "text"
}
],
"url": {
"raw": "http://localhost:4010/widget",
"host": [
"http://localhost:4010"
],
"path": [
"widget"
]
}
},
"response": []
}
],
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "12345678",
"type": "string"
}
]
},
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"packages": {},
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"packages": {},
"exec": [
""
]
}
}
]
},
{
"name": "Widget",
"item": [
{
"name": "Create Widget",
"event": [
{
"listen": "test",
"script": {
"exec": [],
"type": "text/javascript",
"packages": {}
}
}
],
"request": {
"auth": {
"type": "noauth"
},
"method": "POST",
"header": [
{
"key": "X-Correlation-Id",
"value": "{{$randomUUID}}",
"type": "text"
},
{
"key": "Content-Signature",
"value": "{{signature_key}}",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"isLogin\": true,\n \"widget_id\": \"0\",\n \"callFrom\": \"my-tax\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:4010/widget",
"host": [
"http://localhost:4010"
],
"path": [
"widget"
]
}
},
"response": []
}
]
}
]
}
Loading

0 comments on commit 29a5bf9

Please sign in to comment.