Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Support file upload #45

Merged
merged 4 commits into from
Nov 19, 2019
Merged

Conversation

bookmoons
Copy link
Contributor

Adds support for the file upload feature of Postman collections.

Postman usage is in the request body interface with a type form-body. Each entry can be converted to a file and have a path specified. Hovering reveals the control.

Postman seems to always use absolute paths. The conversion preserves paths and leaves the files where they are.

Closes #42.

@bookmoons
Copy link
Contributor Author

Here's a collection that uploads the httpbin and displays the result.

{
	"info": {
		"_postman_id": "de6446a1-6b5f-4006-b438-1fe70466daf8",
		"name": "File Upload",
		"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
	},
	"item": [
		{
			"name": "TestFileUpload",
			"event": [
				{
					"listen": "test",
					"script": {
						"id": "e41d8c08-1399-4956-a5f9-175f90e6eade",
						"exec": [
							"console.log(pm.response.text());",
							""
						],
						"type": "text/javascript"
					}
				}
			],
			"request": {
				"method": "POST",
				"header": [],
				"body": {
					"mode": "formdata",
					"formdata": [
						{
							"key": "file",
							"type": "file",
							"src": "theultimatequestion.txt"
						}
					]
				},
				"url": {
					"raw": "http://httpbin.org/post",
					"protocol": "http",
					"host": [
						"httpbin",
						"org"
					],
					"path": [
						"post"
					]
				}
			},
			"response": []
		}
	],
	"protocolProfileBehavior": {}
}

It looks for a file theultimatequestion.txt in the same dir, eg:

Test data

@bookmoons
Copy link
Contributor Author

bookmoons commented Oct 21, 2019

@marcelo-dalmeida I wonder if you'd be willing to test this with your collection? It would help to confirm it does the right thing with real input.

You can do it like this.

git clone https://github.com/bookmoons/postman-to-k6.git
cd postman-to-k6
git branch bookmoons/file-upload
node bin/postman-to-k6.js collection.json

@marcelo-dalmeida
Copy link

@bookmoons

I'm doing it now

@marcelo-dalmeida
Copy link

@bookmoons

Feel free to ask me to test it anytime you need

Here a snippet from my attempt:

image

@bookmoons
Copy link
Contributor Author

Whoops, my fault. Got to have an npm install in there to install deps.

git clone https://github.com/bookmoons/postman-to-k6.git
cd postman-to-k6
git branch bookmoons/file-upload
npm install
node bin/postman-to-k6.js collection.json

@marcelo-dalmeida
Copy link

image

@bookmoons
Copy link
Contributor Author

Really need to test my instructions before posting. Branch was the wrong command. Checkout is the right thing. This gets it to the right code for me.

git checkout master
git branch -D bookmoons/file-upload
git checkout bookmoons/file-upload
npm install
node bin/postman-to-k6.js collection.json

@marcelo-dalmeida
Copy link

Hi @bookmoons

Sorry, I could have noted it too.

For the problem at matter

That's the generated file:
k6-script.zip

I now find the original error (as you noted in the issue, it was two different errors):
image

So I think it may be better to create another issue and associate this pull request to that

Also, thank you for working on it

@bookmoons
Copy link
Contributor Author

OK, thanks a lot for testing. It's a good bug. I included a fix for this but clearly it didn't quite do it. Will investigate.

@bookmoons
Copy link
Contributor Author

Is it possible you were executing the old conversion here? The conversion seems to work for me. Here's a tested script that pulls and converts. It doesn't find the upload file on my system but it should be there on yours.

#!/bin/bash

git clone https://github.com/bookmoons/postman-to-k6.git
cd postman-to-k6
git checkout bookmoons/file-upload
npm install
wget https://github.com/loadimpact/postman-to-k6/files/3745258/recommendation.postman_collection.zip
unzip recommendation.postman_collection.zip
rm recommendation.postman_collection.zip
node bin/postman-to-k6.js recommendation.postman_collection.json >upload.js
k6 run upload.js

I think it will also find a missing environment. In Postman you can export the environment, then the converter can take it on the command line.

node bin/postman-to-k6.js recommendation.postman_collection.json -e environment.json >upload.js

Also pushed a fix for paths, Postman seems to encode a Windows path a little differently.

@marcelo-dalmeida
Copy link

Hi @bookmoons

Is it possible you were executing the old conversion here?

Recapitulating my actions:

Well, I cleaned as you instructed:

git checkout master
git branch -D bookmoons/file-upload
git checkout bookmoons/file-upload
npm install
node bin/postman-to-k6.js collection.json

Then I saw file info that was not present before in the script. So I tested with run as the conversion seemed fine to me. I also sent you the file so you could double-check what I was running

That's the generated file:
k6-script.zip

I now find the original error (as you noted in the issue, it was two different errors):
image

It seemed 'double-fine' to me as it was giving me the very same error I found when I manually fixed the file part of the script. It was then a matter about the file being binary.

Now I ran the conversion again (from the scratch) and included the missing environment.
image

@marcelo-dalmeida
Copy link

B-Finder Copy.postman_environment.zip

I'm sending you a copy, but I just removed 'sensitive' information

@bookmoons
Copy link
Contributor Author

Appreciate the report and testing @marcelo-dalmeida. Pointing your collection at httpbin successfully uploads for me, so I think it's in good shape. After review / possible bug fixes this should land in the next release.

Your collection has a header Content-Type: application/json. I had to take this header out to make the upload work. Tests show Postman overrides it automatically.

@bookmoons
Copy link
Contributor Author

I think this is ready for review.

For consistency with Postman behavior.
@robingustafsson
Copy link
Member

Pardon delay in reviewing. LGTM. Merging.

@robingustafsson robingustafsson merged commit 199d9cc into grafana:master Nov 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: Object has no member ‘replace’
3 participants