Skip to content

Commit

Permalink
Merge pull request #31 from loadimpact/fix/variable-resolution-entry-…
Browse files Browse the repository at this point in the history
…order

Resolve variables with correct entry order.
  • Loading branch information
legander committed May 20, 2020
2 parents c39aa2d + 2ec7ac9 commit 2d6a09b
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 74 deletions.
40 changes: 20 additions & 20 deletions example/headers.har
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"log": {
"entries": [
"log": {
"entries": [
{
"index": 0,
"request": {
"method": "GET",
"url": "http://test.loadimpact.com/path",
"httpVersion": "HTTP/1.1",
"headers": [
{
"index": 0,
"request": {
"method": "GET",
"url": "http://test.loadimpact.com/path",
"httpVersion": "HTTP/1.1",
"headers": [
{
"name": "Accept",
"value": "text/csv"
},
{
"name": "Authorization",
"value": "Bearer abc123"
}
]
}
"name": "Accept",
"value": "text/csv"
},
{
"name": "Authorization",
"value": "Bearer abc123"
}
]
}
]
}
}
]
}
}
48 changes: 48 additions & 0 deletions example/scope-vars.har
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"log": {
"options": {},
"pages": [
{
"title": "Zz",
"id": "9a2956c0-0b04-499f-82cc-bf62bf1ca0f4",
"type": "group"
}
],
"entries": [
{
"comment": "",
"checks": [],
"variables": [
{
"type": 0,
"name": "test",
"expression": "$.asd"
}
],
"request": {
"url": "http://test.k6.io",
"method": "GET",
"queryString": [],
"headers": []
},
"pageref": "9a2956c0-0b04-499f-82cc-bf62bf1ca0f4"
},
{
"comment": "",
"checks": [],
"variables": [],
"request": {
"url": "http://test.k6.io/login",
"method": "POST",
"queryString": [],
"headers": [
{
"name": "Authorization",
"value": "${test}"
}
]
}
}
]
}
}
132 changes: 84 additions & 48 deletions example/scopes.har
Original file line number Diff line number Diff line change
@@ -1,50 +1,86 @@
{
"log": {
"pages": [
{
"index": 0,
"id": "group_0",
"title": "Group 0"
},
{
"index": 1,
"id": "group_1",
"title": "Group 1"
}
],
"entries": [
{
"index": 0,
"pageref": "group_0",
"request": {
"method": "POST",
"url": "http://test.loadimpact.com/login"
}
},
{
"index": 1,
"pageref": "group_0",
"request": {
"method": "GET",
"url": "http://test.loadimpact.com/users"
}
},
{
"index": 0,
"pageref": "group_1",
"request": {
"method": "GET",
"url": "http://test.loadimpact.com/items"
}
},
{
"index": 1,
"pageref": "group_1",
"request": {
"method": "POST",
"url": "http://test.loadimpact.com/checkout"
}
}
]
}
"log": {
"pages": [
{
"id": "group_A",
"title": "Group 0"
},
{
"id": "group_B",
"title": "Group 1"
}
],
"entries": [
{
"pageref": "group_A",
"comment": "ORDER 1",
"request": {
"method": "POST",
"url": "http://test.loadimpact.com/login"
}
},
{
"pageref": "group_A",
"comment": "ORDER 2",
"request": {
"method": "GET",
"url": "http://test.loadimpact.com/users"
}
},
{
"comment": "ORDER 3",
"request": {
"method": "POST",
"url": "http://test.loadimpact.com/checkout"
}
},
{
"comment": "ORDER 4",
"request": {
"method": "POST",
"url": "http://test.loadimpact.com/checkout"
}
},
{
"pageref": "implicit-group A",
"comment": "ORDER 5",
"request": {
"method": "POST",
"url": "http://test.loadimpact.com/checkout"
}
},
{
"pageref": "group_B",
"comment": "ORDER 7",
"request": {
"method": "GET",
"url": "http://test.loadimpact.com/items"
}
},
{
"pageref": "implicit-group A",
"comment": "ORDER 6",
"request": {
"method": "POST",
"url": "http://test.loadimpact.com/checkout"
}
},
{
"pageref": "group_B",
"comment": "ORDER 8",
"request": {
"method": "POST",
"url": "http://test.loadimpact.com/checkout"
}
},
{
"pageref": "implicit-group B",
"comment": "ORDER 9",
"request": {
"method": "POST",
"url": "http://test.loadimpact.com/checkout"
}
}
]
}
}
31 changes: 26 additions & 5 deletions src/validate/variableDefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const { InvalidArchiveError } = require('../error')
function variableDefined (archive) {
const entries = orderEntries(archive)
const defined = new Set()

for (let i = 0; i < entries.length; i++) {
const entry = entries[i]
validate(entry.request, i, defined)
Expand Down Expand Up @@ -127,15 +128,33 @@ function define (entry, defined) {
}
}

function zipGroups (entries) {
const groupedEntries = entries.reduce((result, entry) => {
if (entry.pageref) {
if (!result.has(entry.pageref)) {
result.set(entry.pageref, [])
}

result.set(entry.pageref, [...result.get(entry.pageref), entry])
} else {
result.set(entry)
}

return result
}, new Map())

return [...groupedEntries.entries()].flatMap(([item, children]) => children || item)
}

function orderEntries (archive) {
const pages = extractPages(archive.log.pages)
const entries = archive.log.entries || []
const external = entries.filter(entry => !entry.pageref).sort(sort.index)
const explicit = orderExplicit(entries, pages)
const implicit = orderImplicit(entries, pages)
return [ ...external, ...explicit, ...implicit ]

// No matter if entry is external, page is explicit or implicit the order of entries are always respected
// order should be the same order as the rendered output.
return zipGroups(entries)
}

// eslint-disable-next-line no-unused-vars
function extractPages (pages) {
if (pages) {
return new Map(pages.map(page => [ page.id, page.index ]))
Expand All @@ -144,13 +163,15 @@ function extractPages (pages) {
}
}

// eslint-disable-next-line no-unused-vars
function orderExplicit (entries, pages) {
const unordered = entries.filter(entry => pages.has(entry.pageref))
const groups = groupEntries(unordered)
const orderedGroups = orderGroupsByIndex(groups, pages)
return expand(orderedGroups)
}

// eslint-disable-next-line no-unused-vars
function orderImplicit (entries, pages) {
const unordered = entries
.filter(entry => entry.pageref && !pages.has(entry.pageref))
Expand Down
2 changes: 1 addition & 1 deletion standalone.js

Large diffs are not rendered by default.

0 comments on commit 2d6a09b

Please sign in to comment.