Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Postman files upload #95

Merged
merged 4 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ converter, actively maintained and open for new contributions.
- [x] Variables (at all scopes + dynamic).
- [x] Data files.
- [x] Authentication methods (except Hawk).
- [x] File uploads.
- [x] File uploads (experimental).
- [x] `postman.*` interface ([exceptions below](#unsupported-features)).
- [x] `pm.*` interface ([exceptions below](#unsupported-features)).
- [x] Support for
Expand Down
7 changes: 5 additions & 2 deletions lib/generate/Request/analyze.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const aid = require('../../aid');
const Auth = require('../../auth');
const dataForm = require('./form');
const URI = require('urijs');
const { InheritAuth } = require('../../sym');
const { readAuth } = require('../../common');
const { BodyItemType, BodyMode } = require('../../enum');
const { formDataForm, formDataFile } = require('./form');

function analyze(request, result, block) {
const feature = {
Expand Down Expand Up @@ -82,14 +82,17 @@ function data(request, feature, result) {
dataRaw(body, feature);
return;
case BodyMode.formdata:
dataForm(body, feature, result);
formDataForm(body, feature, result);
return;
case BodyMode.urlencoded:
dataUrl(body, feature);
return;
case BodyMode.graphql:
dataGraphQL(body, feature);
return;
case BodyMode.file:
formDataFile(body, feature, result);
return;
default:
throw new Error(`Unrecognized body mode: ${mode}`);
}
Expand Down
25 changes: 20 additions & 5 deletions lib/generate/Request/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ function dataForm(body, feature, result) {
feature.data = data;
}

function dataFile(body, feature, result) {
const data = {};
const item = body.file;
item.type = 'file';
convertItem(item, data, result);

feature.data = data;
}

function convertItem(item, data, result) {
if (!item.key) {
throw new Error('Form item missing key');
}
// if (!item.key) {
// throw new Error('Form item missing key');
// }
switch (item.type) {
case 'text':
convertItemText(item, data);
Expand All @@ -36,7 +45,8 @@ function convertItemText(item, data) {

function convertItemFile(item, data, result) {
result.imports.set('http', 'k6/http');
const { key } = item;
// const { key } = item;
const key = 'file';
const path = extractPath(item);
result.files.add(path);
data[key] = {
Expand All @@ -55,4 +65,9 @@ function extractPath(item) {
}
}

module.exports = dataForm;
// Module export for multiple functions

module.exports = {
formDataForm: dataForm,
formDataFile: dataFile
};
24 changes: 24 additions & 0 deletions lib/generate/separate/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Item = require('../Item/separate');
const postman = require('postman-collection');
const prettier = require('prettier');
const { Auth } = require('./sym');
const { basename } = require('path');

function render(mapped, result) {
return location(mapped, 1, null, result);
Expand Down Expand Up @@ -58,6 +59,7 @@ function item(node, depth, auth, itemResult) {
function simplify(result, block) {
return {
imports: result.imports,
files: result.files,
effectImports: result.effectImports,
declares: block.declares,
logic: block.main.join('\n\n')
Expand All @@ -69,6 +71,7 @@ function text(result, depth) {
const raw = [
imports(result, false, depth),
declares(result),
files(result),
result.logic
].filter(item => item).join('\n\n');
return prettier.format(raw, { semi: true, parser: 'babel' });
Expand All @@ -82,4 +85,25 @@ function declares(result) {
}
}

function files(result) {
if (result.files.size) {
const items = [];
for (const path of result.files) {
items.push(fileLoad(path));
}
return `const files = {};
${items.join('\n')}`;
} else {
return null;
}
}

function fileLoad(path) {
const name = basename(path);
return `files[${JSON.stringify(path)}] = http.file(
open(${JSON.stringify(path)}, "b"),
${JSON.stringify(name)}
);`;
}

module.exports = render;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
"pkginfo": "^0.4.1",
"postman-collection": "^4.1.0",
"postman-collection-transformer": "^4.1.3",
"prettier": "^1.19.1",
"spo-gpo": "^1.0.0",
"strip-json-comments": "^3.1.1",
"urijs": "^1.19.11",
Expand All @@ -84,6 +83,7 @@
"husky": "^7.0.4",
"mock-require": "^3.0.3",
"npm-run-all": "^4.1.5",
"prettier": "^1.19.1",
"sinon": "^7.5.0",
"snazzy": "^8.0.0",
"standard": "^14.3.1"
Expand Down
8 changes: 3 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3813,11 +3813,9 @@ json-stable-stringify@~0.0.0:
jsonify "~0.0.0"

json5@^2.1.2:
version "2.1.3"
resolved "https://registry.npmjs.org/json5/-/json5-2.1.3.tgz"
integrity "sha1-ybD3+pIzv+WAf+ZvzzpWF+1ZfUM= sha512-KXPvOm8K9IJKFM0bmdn8QXh7udDh1g/giieX0NLCaMnb4hEiVFqnop2ImTXCc5e0/oHz3LTqmHGtExn5hfMkOA=="
dependencies:
minimist "^1.2.5"
version "2.2.3"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
integrity "sha1-eM1vGhm9wStz21rQxh79ZsHikoM= sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg=="

jsonfile@^4.0.0:
version "4.0.0"
Expand Down