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

Feature request - Pass Request default options #87

Closed
thim81 opened this issue Apr 27, 2021 · 3 comments
Closed

Feature request - Pass Request default options #87

thim81 opened this issue Apr 27, 2021 · 3 comments

Comments

@thim81
Copy link
Contributor

thim81 commented Apr 27, 2021

Hi @simskij

Related to #81 , I'm looking into way to set the "headers" globally to overwrite the default" x-www-form-urlencoded".
It seems that it is not (yet) supported grafana/k6#761 (comment) .

I might have a feature request, but before I try to figure how to code it as a PR, I wanted to receive your input, if you think it is valuable.

Currently the package support "options" for setting Postman execution related data & variables (https://github.com/k6io/postman-to-k6#options)

It would help to be able to pass the Request "options", that are being used in the generated script.js file

import "../../libs/shim/urijs.js";

postman[Symbol.for("define")]({
  name: "Get all list configurations",
  id: "efc9d5ad-c0f1-4f41-ae17-e903fa56f82b",
  method: "GET",
  address: "{{baseUrl}}/lists/v2?$top=100&$skip=0&$count=false",
  headers: {
    "X-ApiKey": "{{username}}:{{password}}"
  },
  post(response) {
    // Validate status 2xx
    pm.test("[GET] /lists/v2 - Status code is 2xx", function() {
      pm.response.to.be.success;
    });
  }
});

The feature request would be pass a Request options object that will extend/overwrite the Postman Request methods like the "headers".

Idea:

--request-options ./request-headers.json

which would contain custom options like { headers: { 'Content-Type': 'application/json' } }

Which would result in the Postman request, that gets extended by the passed on request headers.
This would be "default" options that you pass, which would apply for all requests.

import "../../libs/shim/urijs.js";

postman[Symbol.for("define")]({
  name: "Get all list configurations",
  id: "efc9d5ad-c0f1-4f41-ae17-e903fa56f82b",
  method: "GET",
  address: "{{baseUrl}}/lists/v2?$top=100&$skip=0&$count=false",
  headers: {
    "X-ApiKey": "{{username}}:{{password}}",
    'Content-Type": "application/json"
  },
  post(response) {
    // Validate status 2xx
    pm.test("[GET] /lists/v2 - Status code is 2xx", function() {
      pm.response.to.be.success;
    });
  }
});

Where the "headers" section got extended from

headers: {
    "X-ApiKey": "{{username}}:{{password}}"
  }

to

headers: {
    "X-ApiKey": "{{username}}:{{password}}",
    'Content-Type": "application/json"
  }

@thim81 thim81 changed the title Feature request - Pass K6 options Feature request - Pass Request default options Apr 27, 2021
@thim81
Copy link
Contributor Author

thim81 commented Apr 27, 2021

Or another option would be to provide support in the Postman request for passing that are globally defined.

https://k6.io/docs/javascript-api/k6-http/params/

Example

export default function () {
  let params = {
    cookies: { my_cookie: 'value' },
    headers: { 'X-MyHeader': 'k6test' },
    redirects: 5,
    tags: { k6test: 'yes' },
  };
  let res = http.get('https://k6.io', params);
}

I'm not sure if the Postman request function uses all the "options"?
I tried this, but it does not seem to work, or am I missing something?

Example script.js file

// Auto-generated by the Load Impact converter

import "./libs/shim/core.js";
import { group } from "k6";
import "./requests/Lists/Get-all-list-configurations.js";

export let options = { maxRedirects: 4 , headers: { 'Content-Type': 'application/json' } };

const Request = Symbol.for("request");
postman[Symbol.for("initial")]({
  options,
  collection: {
    baseUrl: "https://example.com/api"
  },
  environment: {
    baseUrl: "https://example.com/api"
  }
});

...

Which results in the following K6 call

INFO[0006] Request:
GET /api/lists/v2?$top=100&$skip=0&$count=false HTTP/1.1
User-Agent: k6/0.29.0 (https://k6.io/)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip

UPDATE: I created a separate PR that would allow the passing of the K6 params as options, which will be used when making the Postman requests via K6. #88

@simskij
Copy link
Contributor

simskij commented Apr 27, 2021

Hi,

Got it! Looks really good. I'll try to process the PRs as soon as possible, although I'm quite swamped at the moment so I can't promise I'll get to it this week. Thanks again for your excellent contributions! 🙇🏼

Best,
Simme

@thim81
Copy link
Contributor Author

thim81 commented Apr 28, 2021

@simskij Take your time. Don't need to rush it. I have made a merged branch with which I am using the PRs combined, so I can use it until you have tested it.

Thank you for the excellent package.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants