@gatling.io/postman 3.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,114 @@
1
+ import * as pm from "postman-collection";
2
+
3
+ import { Duration, ScenarioBuilder, scenario } from "@gatling.io/core";
4
+ import { HttpRequestActionBuilder } from "@gatling.io/http";
5
+
6
+ import { onInit } from "../jvm";
7
+ import { postmanRequest } from "./request";
8
+ import { findItem, findItemGroup, listItems, readCollectionFromResource, readEnvironmentFromResource } from "./utils";
9
+
10
+ onInit();
11
+
12
+ export interface Postman {
13
+ /**
14
+ * @param collectionPath - Path to an exported collection file in the project resources
15
+ */
16
+ fromResource(collectionPath: string): PostmanCollection;
17
+ }
18
+
19
+ export interface PostmanCollection extends PostmanCollectionFolder {
20
+ /**
21
+ * Inject an environment in the collection.
22
+ *
23
+ * @param variables - A dictionary of variables, or the path to an exported environment file in the project resources
24
+ */
25
+ environment(variables: Record<string, string> | string): PostmanCollection;
26
+ /**
27
+ * Inject global variables in the collection.
28
+ *
29
+ * @param variables - A dictionary of variables, or the path to an exported environment file in the project resources
30
+ */
31
+ globals(variables: Record<string, string> | string): PostmanCollection;
32
+ }
33
+
34
+ export interface PostmanCollectionFolder {
35
+ /**
36
+ * Move down one level to a folder contained in the current folder.
37
+ *
38
+ * @param folder - Name of a folder contained in the current folder
39
+ */
40
+ folder(folder: string): PostmanCollectionFolder;
41
+ /**
42
+ * Create a Gatling HTTP request from a Postman request contained in the current folder.
43
+ *
44
+ * @param request - Name of a request contained in the current folder
45
+ * @param overrideName - Optionally override the Gatling request name, otherwise the Postman request name will be used
46
+ */
47
+ request(request: string, overrideName?: string): HttpRequestActionBuilder;
48
+ /**
49
+ * Create a Gatling scenario from the list of requests contained in the current folder.
50
+ *
51
+ * @param name - Name of the scenario
52
+ * @param options -
53
+ */
54
+ scenario(name: string, options?: ScenarioOptions): ScenarioBuilder;
55
+ }
56
+
57
+ export interface ScenarioOptions {
58
+ /** Include requests in sub-folders, false by default. */
59
+ recursive?: boolean;
60
+ /** Include this pause time between requests, no pause by default. */
61
+ pauses?: Duration;
62
+ }
63
+
64
+ export const postman: Postman = {
65
+ fromResource: (collectionPath) => postmanCollectionImpl(readCollectionFromResource(collectionPath), {}, {})
66
+ };
67
+
68
+ const postmanCollectionImpl = (
69
+ collection: pm.Collection,
70
+ environment: Record<string, string>,
71
+ globals: Record<string, string>
72
+ ): PostmanCollection => ({
73
+ environment: (variables) => postmanCollectionImpl(collection, resolveEnvironment(variables), globals),
74
+ globals: (variables) => postmanCollectionImpl(collection, environment, resolveEnvironment(variables)),
75
+ ...postmanCollectionFolder(collection, collection, environment, globals)
76
+ });
77
+
78
+ const resolveEnvironment = (variables: Record<string, string> | string): Record<string, string> =>
79
+ typeof variables === "string" ? readEnvironmentFromResource(variables) : variables;
80
+
81
+ const postmanCollectionFolder = (
82
+ collection: pm.Collection,
83
+ currentItemGroup: pm.ItemGroup,
84
+ environment: Record<string, string>,
85
+ globals: Record<string, string>
86
+ ): PostmanCollectionFolder => ({
87
+ folder: (folder) => {
88
+ const newItemGroup = findItemGroup(currentItemGroup, folder);
89
+ if (newItemGroup === undefined) {
90
+ throw Error(`Postman folder '${folder}' not found`);
91
+ }
92
+ return postmanCollectionFolder(collection, newItemGroup, environment, globals);
93
+ },
94
+ request: (request, overrideName) => {
95
+ const name = overrideName !== undefined ? overrideName : request;
96
+ const item = findItem(currentItemGroup, request);
97
+ if (item === undefined) {
98
+ throw Error(`Postman request '${request}' not found`);
99
+ }
100
+ return postmanRequest(name, collection, item, environment, globals);
101
+ },
102
+ scenario: (name, options) => {
103
+ let scn = scenario(name);
104
+ const recursive = options?.recursive || false;
105
+ const pauses = options?.pauses;
106
+ listItems(currentItemGroup, recursive).forEach((item, idx) => {
107
+ if (pauses !== undefined && idx > 0) {
108
+ scn = scn.pause(pauses);
109
+ }
110
+ scn = scn.exec(postmanRequest(item.name, collection, item, environment, globals));
111
+ });
112
+ return scn;
113
+ }
114
+ });
@@ -0,0 +1,104 @@
1
+ import { lookup as mimeLookup } from "mime-types";
2
+ import * as pm from "postman-collection";
3
+
4
+ import { RawFileBody, StringBody } from "@gatling.io/core";
5
+ import { http, HttpRequestActionBuilder } from "@gatling.io/http";
6
+
7
+ import { wrapUrl } from "../jvm";
8
+
9
+ export const postmanRequest = (
10
+ name: string,
11
+ collection: pm.Collection,
12
+ item: pm.Item,
13
+ environment: Record<string, string>,
14
+ globals: Record<string, string>
15
+ ): HttpRequestActionBuilder => {
16
+ const variableDefinitions = [environment, collection.variables, globals];
17
+ const resolvedItem = new pm.Item(item.toObjectResolved(null, variableDefinitions, { ignoreOwnVariables: true }));
18
+
19
+ const method = resolvedItem.request.method;
20
+ const url = resolvedItem.request.url.toString();
21
+ const w = wrapUrl;
22
+ let gatlingRequest: HttpRequestActionBuilder =
23
+ w !== undefined
24
+ ? http(name).httpRequest(method, (session) => w(url, session.userId()))
25
+ : http(name).httpRequest(method, url);
26
+
27
+ // Body
28
+ if (resolvedItem.request.body !== undefined) {
29
+ gatlingRequest = setBody(gatlingRequest, resolvedItem.request.body);
30
+ }
31
+
32
+ // Headers
33
+ resolvedItem.request.headers.each((h) => {
34
+ if (!h.disabled) {
35
+ gatlingRequest = gatlingRequest.header(h.key, h.value);
36
+ }
37
+ });
38
+
39
+ return gatlingRequest;
40
+ };
41
+
42
+ const setBody = (gatlingRequest: HttpRequestActionBuilder, body: pm.RequestBody): HttpRequestActionBuilder => {
43
+ switch (body.mode) {
44
+ case "formdata":
45
+ gatlingRequest = gatlingRequest.asMultipartForm();
46
+ if (body.formdata) {
47
+ body.formdata.each((param) => {
48
+ if (!param.disabled) {
49
+ if (param.type === "file") {
50
+ const filePath = resolveFile(param.src as string);
51
+ gatlingRequest = gatlingRequest.formUpload(param.key, filePath);
52
+ } else {
53
+ gatlingRequest = gatlingRequest.formParam(param.key, param.value);
54
+ }
55
+ }
56
+ });
57
+ }
58
+ return gatlingRequest;
59
+ case "urlencoded":
60
+ gatlingRequest = gatlingRequest.asFormUrlEncoded();
61
+ if (body.urlencoded) {
62
+ body.urlencoded.each((param) => {
63
+ if (!param.disabled) {
64
+ gatlingRequest = gatlingRequest.formParam(param.key, param.value);
65
+ }
66
+ });
67
+ }
68
+ return gatlingRequest;
69
+ case "raw":
70
+ const language: string = body.options?.raw?.language || "text";
71
+ return gatlingRequest.body(StringBody(body.raw || "")).header("content-type", rawContentTypes[language]);
72
+ case "file":
73
+ const src = resolveFile(body.file?.src || "");
74
+ const mimeType = mimeLookup(src) || "application/octet-stream";
75
+ return gatlingRequest.body(RawFileBody(src)).header("content-type", mimeType);
76
+ case "graphql":
77
+ const query = body.graphql?.query as string;
78
+ const jsonBody =
79
+ body.graphql?.variables !== undefined && body.graphql.variables.length > 0
80
+ ? { query, variables: JSON.parse(body.graphql.variables) }
81
+ : { query };
82
+ return gatlingRequest.body(StringBody(JSON.stringify(jsonBody))).asJson();
83
+ }
84
+ };
85
+
86
+ const rawContentTypes: Record<string, string> = {
87
+ text: "text/plain",
88
+ javascript: "application/javascript",
89
+ json: "application/json",
90
+ html: "text/html",
91
+ xml: "application/xml"
92
+ };
93
+
94
+ const resolveFile = (src: string) => {
95
+ if (src.startsWith("postman-cloud://")) {
96
+ // TODO implement downloading files from Postman Cloud
97
+ throw Error("Downloading files from Postman Cloud is not implemented yet, unable to retrieve " + src);
98
+ } else {
99
+ // TODO check if file exists
100
+ // File is assumed to be in the project resources by default
101
+ // TODO allow configuring location of Postman working directory in the protocol
102
+ return src;
103
+ }
104
+ };
@@ -0,0 +1,48 @@
1
+ import { Collection, Item, ItemGroup } from "postman-collection";
2
+
3
+ import { readResourceAsString } from "@gatling.io/core";
4
+
5
+ interface Environment {
6
+ id: string;
7
+ name: string;
8
+ values: EnvironmentValue[];
9
+ }
10
+
11
+ interface EnvironmentValue {
12
+ key: string;
13
+ value: string;
14
+ type: "default" | "secret";
15
+ enabled: boolean;
16
+ }
17
+
18
+ export const readEnvironmentFromResource = (filePath: string): Record<string, string> => {
19
+ const env: Environment = JSON.parse(readResourceAsString(filePath)); // TODO validate format
20
+ return Object.fromEntries(env.values.filter((v) => v.enabled).map((v) => [v.key, v.value]));
21
+ };
22
+
23
+ export const readCollectionFromResource = (filePath: string, environment?: Environment): Collection =>
24
+ new Collection(JSON.parse(readResourceAsString(filePath)), environment?.values);
25
+
26
+ export const findItemGroup = (parent: ItemGroup, name: string): ItemGroup | undefined => {
27
+ return parent.items.find((i) => isItemGroup(i) && i.name === name) as ItemGroup | undefined;
28
+ };
29
+
30
+ export const findItem = (parent: ItemGroup, name: string): Item | undefined => {
31
+ return parent.items.find((i) => isItem(i) && i.name === name) as Item | undefined;
32
+ };
33
+
34
+ export const listItems = (parent: ItemGroup, recursive: boolean): Item[] => {
35
+ const items: Item[] = [];
36
+ parent.items.each((item) => {
37
+ if (isItem(item)) {
38
+ items.push(item);
39
+ } else if (recursive && isItemGroup(item)) {
40
+ items.concat(listItems(item, true));
41
+ }
42
+ });
43
+ return items;
44
+ };
45
+
46
+ const isItem = (i: Item | ItemGroup): i is Item => Item.isItem(i);
47
+
48
+ const isItemGroup = (i: Item | ItemGroup): i is ItemGroup => ItemGroup.isItemGroup(i);