@api-client/core 0.3.4 → 0.3.7
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.
- package/build/browser.d.ts +6 -0
- package/build/browser.js +15 -0
- package/build/browser.js.map +1 -1
- package/build/index.d.ts +14 -2
- package/build/index.js +26 -2
- package/build/index.js.map +1 -1
- package/build/src/lib/calculators/DataCalculator.d.ts +27 -0
- package/build/src/lib/calculators/DataCalculator.js +88 -0
- package/build/src/lib/calculators/DataCalculator.js.map +1 -0
- package/build/src/lib/fs/Fs.d.ts +52 -0
- package/build/src/lib/fs/Fs.js +245 -0
- package/build/src/lib/fs/Fs.js.map +1 -0
- package/build/src/lib/parsers/UrlEncoder.d.ts +51 -0
- package/build/src/lib/parsers/UrlEncoder.js +74 -0
- package/build/src/lib/parsers/UrlEncoder.js.map +1 -0
- package/build/src/lib/parsers/UrlParser.d.ts +104 -0
- package/build/src/lib/parsers/UrlParser.js +189 -0
- package/build/src/lib/parsers/UrlParser.js.map +1 -0
- package/build/src/lib/parsers/UrlValueParser.d.ts +92 -0
- package/build/src/lib/parsers/UrlValueParser.js +172 -0
- package/build/src/lib/parsers/UrlValueParser.js.map +1 -0
- package/build/src/lib/timers/Timers.d.ts +5 -0
- package/build/src/lib/timers/Timers.js +10 -0
- package/build/src/lib/timers/Timers.js.map +1 -0
- package/build/src/mocking/ProjectMock.d.ts +13 -0
- package/build/src/mocking/ProjectMock.js +16 -0
- package/build/src/mocking/ProjectMock.js.map +1 -0
- package/build/src/mocking/lib/Request.d.ts +32 -0
- package/build/src/mocking/lib/Request.js +63 -0
- package/build/src/mocking/lib/Request.js.map +1 -0
- package/build/src/mocking/lib/Response.d.ts +33 -0
- package/build/src/mocking/lib/Response.js +79 -0
- package/build/src/mocking/lib/Response.js.map +1 -0
- package/build/src/models/ErrorResponse.d.ts +5 -4
- package/build/src/models/ErrorResponse.js +18 -5
- package/build/src/models/ErrorResponse.js.map +1 -1
- package/build/src/models/SerializableError.d.ts +30 -0
- package/build/src/models/SerializableError.js +63 -0
- package/build/src/models/SerializableError.js.map +1 -0
- package/build/src/runtime/http-engine/ArcEngine.js +8 -4
- package/build/src/runtime/http-engine/ArcEngine.js.map +1 -1
- package/build/src/runtime/http-engine/HttpEngine.d.ts +3 -3
- package/build/src/runtime/http-engine/HttpEngine.js +3 -3
- package/build/src/runtime/http-engine/HttpEngine.js.map +1 -1
- package/build/src/runtime/http-engine/NodeEngine.js +9 -4
- package/build/src/runtime/http-engine/NodeEngine.js.map +1 -1
- package/build/src/runtime/http-engine/NodeEngineDirect.js +8 -2
- package/build/src/runtime/http-engine/NodeEngineDirect.js.map +1 -1
- package/build/src/runtime/node/BaseRunner.d.ts +21 -0
- package/build/src/runtime/node/BaseRunner.js +27 -0
- package/build/src/runtime/node/BaseRunner.js.map +1 -0
- package/build/src/runtime/node/ProjectParallelRunner.d.ts +81 -0
- package/build/src/runtime/node/ProjectParallelRunner.js +173 -0
- package/build/src/runtime/node/ProjectParallelRunner.js.map +1 -0
- package/build/src/runtime/node/ProjectRequestRunner.d.ts +125 -0
- package/build/src/runtime/node/ProjectRequestRunner.js +185 -0
- package/build/src/runtime/node/ProjectRequestRunner.js.map +1 -0
- package/build/src/runtime/node/ProjectRunner.d.ts +166 -64
- package/build/src/runtime/node/ProjectRunner.js +191 -139
- package/build/src/runtime/node/ProjectRunner.js.map +1 -1
- package/build/src/runtime/node/ProjectRunnerWorker.d.ts +1 -0
- package/build/src/runtime/node/ProjectRunnerWorker.js +58 -0
- package/build/src/runtime/node/ProjectRunnerWorker.js.map +1 -0
- package/build/src/runtime/node/ProjectSerialRunner.d.ts +11 -0
- package/build/src/runtime/node/ProjectSerialRunner.js +34 -0
- package/build/src/runtime/node/ProjectSerialRunner.js.map +1 -0
- package/build/src/runtime/reporters/ProjectRunCliReporter.d.ts +7 -0
- package/build/src/runtime/reporters/ProjectRunCliReporter.js +73 -0
- package/build/src/runtime/reporters/ProjectRunCliReporter.js.map +1 -0
- package/build/src/runtime/reporters/Reporter.d.ts +62 -0
- package/build/src/runtime/reporters/Reporter.js +98 -0
- package/build/src/runtime/reporters/Reporter.js.map +1 -0
- package/build/src/testing/TestCliHelper.d.ts +29 -0
- package/build/src/testing/TestCliHelper.js +80 -0
- package/build/src/testing/TestCliHelper.js.map +1 -0
- package/build/src/testing/getPort.d.ts +52 -0
- package/build/src/testing/getPort.js +169 -0
- package/build/src/testing/getPort.js.map +1 -0
- package/package.json +2 -1
- package/src/lib/calculators/DataCalculator.ts +91 -0
- package/src/lib/fs/Fs.ts +258 -0
- package/src/lib/parsers/UrlEncoder.ts +74 -0
- package/src/lib/parsers/UrlParser.ts +201 -0
- package/src/lib/parsers/UrlValueParser.ts +211 -0
- package/src/lib/timers/Timers.ts +9 -0
- package/src/mocking/LegacyInterfaces.ts +1 -1
- package/src/mocking/ProjectMock.ts +20 -0
- package/src/mocking/lib/Request.ts +85 -0
- package/src/mocking/lib/Response.ts +101 -0
- package/src/models/ErrorResponse.ts +20 -8
- package/src/models/SerializableError.ts +80 -0
- package/src/runtime/http-engine/ArcEngine.ts +8 -4
- package/src/runtime/http-engine/HttpEngine.ts +5 -5
- package/src/runtime/http-engine/NodeEngine.ts +10 -5
- package/src/runtime/http-engine/NodeEngineDirect.ts +9 -3
- package/src/runtime/node/BaseRunner.ts +29 -0
- package/src/runtime/node/ProjectParallelRunner.ts +234 -0
- package/src/runtime/node/ProjectRequestRunner.ts +281 -0
- package/src/runtime/node/ProjectRunner.ts +281 -182
- package/src/runtime/node/ProjectRunnerWorker.ts +62 -0
- package/src/runtime/node/ProjectSerialRunner.ts +36 -0
- package/src/runtime/reporters/ProjectRunCliReporter.ts +79 -0
- package/src/runtime/reporters/Reporter.ts +142 -0
- package/src/testing/TestCliHelper.ts +87 -0
- package/src/testing/getPort.ts +212 -0
- package/build/src/runtime/http-engine/Errors.d.ts +0 -10
- package/build/src/runtime/http-engine/Errors.js +0 -14
- package/build/src/runtime/http-engine/Errors.js.map +0 -1
- package/src/runtime/http-engine/Errors.ts +0 -13
|
@@ -1,62 +1,87 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
1
|
+
import { HttpProject } from '../../models/HttpProject.js';
|
|
2
|
+
import { ProjectFolder } from '../../models/ProjectFolder.js';
|
|
3
3
|
import { Environment } from '../../models/Environment.js';
|
|
4
4
|
import { Logger } from '../../lib/logging/Logger.js';
|
|
5
5
|
import { IRequestLog } from '../../models/RequestLog.js';
|
|
6
|
-
import { ProjectFolder } from '../../models/ProjectFolder.js';
|
|
7
|
-
import { ProjectRequest } from '../../models/ProjectRequest.js';
|
|
8
6
|
import { IHttpRequest } from '../../models/HttpRequest.js';
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
7
|
+
import { IProjectExecutionIteration, IProjectExecutionLog } from '../reporters/Reporter.js';
|
|
8
|
+
import { BaseRunner } from './BaseRunner.js';
|
|
9
|
+
declare type ProjectParent = HttpProject | ProjectFolder;
|
|
10
|
+
export interface IProjectRunnerOptions {
|
|
12
11
|
/**
|
|
13
|
-
*
|
|
12
|
+
* The environment to use.
|
|
13
|
+
* This can be a name or the key of the environment located under the parent or root.
|
|
14
|
+
* It can also be a path to the environment definition. If the file exists it is used. Otherwise it tried to
|
|
15
|
+
* find the environment in the project.
|
|
14
16
|
*/
|
|
15
|
-
environment?:
|
|
17
|
+
environment?: string;
|
|
16
18
|
/**
|
|
17
|
-
*
|
|
18
|
-
* This can be use to pass system variables, when needed.
|
|
19
|
-
*
|
|
20
|
-
* To use system variables tou can use `init.variables = process.env`;
|
|
19
|
+
* The parent folder to execute.
|
|
21
20
|
*/
|
|
22
|
-
|
|
21
|
+
parent?: string;
|
|
23
22
|
/**
|
|
24
|
-
*
|
|
23
|
+
* The names or the keys of requests to execute.
|
|
24
|
+
* This can be used to limit the number of requests.
|
|
25
25
|
*/
|
|
26
|
-
|
|
26
|
+
request?: string[];
|
|
27
27
|
/**
|
|
28
|
-
* The
|
|
29
|
-
*
|
|
28
|
+
* The number of times the execution should be repeated.
|
|
29
|
+
* Default to 1.
|
|
30
30
|
*/
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
export interface ProjectRunnerRunOptions extends IProjectRequestIterator {
|
|
34
|
-
}
|
|
35
|
-
export interface RunResult {
|
|
31
|
+
iterations?: number;
|
|
36
32
|
/**
|
|
37
|
-
* The
|
|
33
|
+
* The number of milliseconds to wait between each iteration.
|
|
34
|
+
* Default to the next frame (vary from 1 to tens of milliseconds).
|
|
38
35
|
*/
|
|
39
|
-
|
|
36
|
+
iterationDelay?: number;
|
|
40
37
|
/**
|
|
41
|
-
*
|
|
38
|
+
* When set it performs parallel execution for each iteration.
|
|
39
|
+
* The number of executions at the same time depends on the number of processor cores
|
|
40
|
+
* available on the current machine. The maximum of the parallel execution
|
|
41
|
+
* is the number of available cores. When the `iterations` number is higher
|
|
42
|
+
* then the "rest" is added to the iterations per each core.
|
|
42
43
|
*/
|
|
43
|
-
|
|
44
|
+
parallel?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* When set it includes requests in the current folder and sub-folder according to the order
|
|
47
|
+
* defined in the folder.
|
|
48
|
+
*/
|
|
49
|
+
recursive?: boolean;
|
|
44
50
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
51
|
+
* The opposite of the `requests`. The list of names or keys of requests or folders to ignore.
|
|
52
|
+
* Note, ignore is tested before the `requests`.
|
|
47
53
|
*/
|
|
48
|
-
|
|
54
|
+
ignore?: string[];
|
|
49
55
|
/**
|
|
50
|
-
* The
|
|
56
|
+
* The logger to use with the request factory.
|
|
57
|
+
* When not set it uses the dummy logger (no output).
|
|
51
58
|
*/
|
|
52
|
-
|
|
59
|
+
logger?: Logger;
|
|
53
60
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
61
|
+
* When true it copies all system variables to the execution environment.
|
|
62
|
+
* When an array of strings, only takes system variables that are listed in the array.
|
|
63
|
+
* When a map, it uses this map as a list of variables.
|
|
64
|
+
* When not set it does not read system variables.
|
|
56
65
|
*/
|
|
57
|
-
|
|
66
|
+
variables?: boolean | string[] | Record<string, string>;
|
|
58
67
|
}
|
|
59
68
|
export interface ProjectRunner {
|
|
69
|
+
/**
|
|
70
|
+
* Event dispatched when an iteration is about to start.
|
|
71
|
+
*/
|
|
72
|
+
on(event: 'before-iteration', listener: (index: number, iterated: boolean) => void): this;
|
|
73
|
+
/**
|
|
74
|
+
* Event dispatched when an iteration finished.
|
|
75
|
+
*/
|
|
76
|
+
on(event: 'after-iteration', listener: (index: number, iterated: boolean) => void): this;
|
|
77
|
+
/**
|
|
78
|
+
* Event dispatched before the iteration is going to sleep for the set period of time.
|
|
79
|
+
*/
|
|
80
|
+
on(event: 'before-sleep', listener: () => void): this;
|
|
81
|
+
/**
|
|
82
|
+
* Event dispatched after the iteration is woke up.
|
|
83
|
+
*/
|
|
84
|
+
on(event: 'after-sleep', listener: () => void): this;
|
|
60
85
|
/**
|
|
61
86
|
* The request object is prepared and about to be sent to the HTTP engine
|
|
62
87
|
*/
|
|
@@ -68,7 +93,23 @@ export interface ProjectRunner {
|
|
|
68
93
|
/**
|
|
69
94
|
* There was a general error during the request
|
|
70
95
|
*/
|
|
71
|
-
on(event: 'error', listener: (key: string,
|
|
96
|
+
on(event: 'error', listener: (key: string, log: IRequestLog, message: string) => void): this;
|
|
97
|
+
/**
|
|
98
|
+
* Event dispatched when an iteration is about to start.
|
|
99
|
+
*/
|
|
100
|
+
once(event: 'before-iteration', listener: (index: number, iterated: boolean) => void): this;
|
|
101
|
+
/**
|
|
102
|
+
* Event dispatched when an iteration finished.
|
|
103
|
+
*/
|
|
104
|
+
once(event: 'after-iteration', listener: (index: number, iterated: boolean) => void): this;
|
|
105
|
+
/**
|
|
106
|
+
* Event dispatched before the iteration is going to sleep for the set period of time.
|
|
107
|
+
*/
|
|
108
|
+
once(event: 'before-sleep', listener: () => void): this;
|
|
109
|
+
/**
|
|
110
|
+
* Event dispatched after the iteration is woke up.
|
|
111
|
+
*/
|
|
112
|
+
once(event: 'after-sleep', listener: () => void): this;
|
|
72
113
|
/**
|
|
73
114
|
* The request object is prepared and about to be sent to the HTTP engine
|
|
74
115
|
*/
|
|
@@ -80,46 +121,107 @@ export interface ProjectRunner {
|
|
|
80
121
|
/**
|
|
81
122
|
* There was a general error during the request
|
|
82
123
|
*/
|
|
83
|
-
once(event: 'error', listener: (key: string,
|
|
124
|
+
once(event: 'error', listener: (key: string, log: IRequestLog, message: string) => void): this;
|
|
84
125
|
}
|
|
85
126
|
/**
|
|
86
|
-
*
|
|
87
|
-
* Developers can run the entire project with the `recursive` flag set. They can also
|
|
88
|
-
* set the starting point with the `parent` options.
|
|
127
|
+
* A class to be extended to run an entire API Project for the given configuration options.
|
|
89
128
|
*
|
|
90
|
-
*
|
|
129
|
+
* The main purpose of this class (and its children) is to iterate over requests
|
|
130
|
+
* in the project and execute them one-by-one.
|
|
131
|
+
*
|
|
132
|
+
* Implementations allow to execute the requests in a serial model or in parallel mode, where
|
|
133
|
+
* a separate workers are created to run the same HTTP requests in each worker.
|
|
134
|
+
*
|
|
135
|
+
* This class generates a run report which other programs can use to build all kinds of UIs
|
|
136
|
+
* around the data collected during the run.
|
|
91
137
|
*/
|
|
92
|
-
export declare class ProjectRunner extends
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
138
|
+
export declare abstract class ProjectRunner extends BaseRunner {
|
|
139
|
+
/**
|
|
140
|
+
* The HTTP project to run requests from.
|
|
141
|
+
*/
|
|
142
|
+
project?: HttpProject;
|
|
143
|
+
/**
|
|
144
|
+
* The execution options for the project.
|
|
145
|
+
*/
|
|
146
|
+
options?: IProjectRunnerOptions;
|
|
147
|
+
/**
|
|
148
|
+
* The root object (project or a folder) where the program starts iterating over the requests.
|
|
149
|
+
*/
|
|
150
|
+
root?: ProjectParent;
|
|
151
|
+
/**
|
|
152
|
+
* The selected environment to apply to the requests.
|
|
153
|
+
*/
|
|
154
|
+
environment?: Environment;
|
|
155
|
+
/**
|
|
156
|
+
* The events target instance for events dispatched by the request factory.
|
|
157
|
+
*/
|
|
158
|
+
target: EventTarget;
|
|
159
|
+
/**
|
|
160
|
+
* This is used with `--iterations`. The index of the current iteration.
|
|
161
|
+
*/
|
|
162
|
+
protected index: number;
|
|
163
|
+
/**
|
|
164
|
+
* The currently executed iteration loop.
|
|
165
|
+
*/
|
|
166
|
+
protected currentIteration?: IProjectExecutionIteration;
|
|
167
|
+
/**
|
|
168
|
+
* The number of remaining iterations to run.
|
|
169
|
+
*/
|
|
170
|
+
protected remaining: number;
|
|
171
|
+
/**
|
|
172
|
+
* Whether the configuration allows iterations (the parallel mode).
|
|
173
|
+
*/
|
|
174
|
+
protected hasIterations: boolean;
|
|
175
|
+
/**
|
|
176
|
+
* When set it won't amit any event.
|
|
177
|
+
* This can be used in a background worker when events are never handled.
|
|
178
|
+
*/
|
|
179
|
+
noEmit: boolean;
|
|
180
|
+
constructor();
|
|
181
|
+
/**
|
|
182
|
+
* A required step before running the project.
|
|
183
|
+
* It configures the execution context. It may throw an error when configuration is not valid.
|
|
184
|
+
*/
|
|
185
|
+
configure(project: HttpProject, opts?: IProjectRunnerOptions): Promise<void>;
|
|
186
|
+
/**
|
|
187
|
+
* Executes the requests in the project.
|
|
188
|
+
* @returns The execution log created by calling the `createReport()` function.
|
|
189
|
+
*/
|
|
190
|
+
abstract execute(): Promise<IProjectExecutionLog>;
|
|
191
|
+
/**
|
|
192
|
+
* Creates the report of the execution.
|
|
193
|
+
*/
|
|
194
|
+
protected createReport(): Promise<IProjectExecutionLog>;
|
|
195
|
+
/**
|
|
196
|
+
* Reads the environment data to use with the execution.
|
|
197
|
+
* If the configured environment is a location of a file
|
|
198
|
+
* it is read as API Client's environment and used in the execution.
|
|
199
|
+
* Otherwise it searches for the environment in the list of the defined
|
|
200
|
+
* environments by the name of the key.
|
|
201
|
+
*
|
|
202
|
+
* It throws when the environment cannot be found or when the file contents is invalid.
|
|
203
|
+
*/
|
|
204
|
+
protected getEnvironment(): Promise<Environment | undefined>;
|
|
98
205
|
/**
|
|
99
|
-
*
|
|
206
|
+
* Runs the requests from the project as configured.
|
|
100
207
|
*/
|
|
101
|
-
protected
|
|
102
|
-
constructor(project: HttpProject, opts?: ProjectRunnerOptions);
|
|
208
|
+
protected executeIteration(): Promise<void>;
|
|
103
209
|
/**
|
|
104
|
-
*
|
|
105
|
-
* @param options Run options.
|
|
106
|
-
* @returns A promise with the run result.
|
|
210
|
+
* Retargets the "request" event from the factory.
|
|
107
211
|
*/
|
|
108
|
-
|
|
109
|
-
protected execute(request: ProjectRequest, variables: Record<string, string>): Promise<RunResult>;
|
|
110
|
-
protected getVariables(parent: HttpProject | ProjectFolder): Promise<Record<string, string>>;
|
|
111
|
-
protected createEnvironment(parent: HttpProject | ProjectFolder): Promise<Record<string, string>>;
|
|
212
|
+
protected _requestHandler(key: string, request: IHttpRequest): void;
|
|
112
213
|
/**
|
|
113
|
-
*
|
|
214
|
+
* Retargets the "response" event from the factory.
|
|
114
215
|
*/
|
|
115
|
-
protected
|
|
216
|
+
protected _responseHandler(key: string, log: IRequestLog): void;
|
|
116
217
|
/**
|
|
117
|
-
*
|
|
218
|
+
* Retargets the "error" event from the factory.
|
|
118
219
|
*/
|
|
119
|
-
protected
|
|
220
|
+
protected _errorHandler(key: string, log: IRequestLog, message: string): void;
|
|
120
221
|
/**
|
|
121
|
-
*
|
|
122
|
-
* @param currentUrl The URL to process.
|
|
222
|
+
* @returns Reads the system variables based on the library configuration.
|
|
123
223
|
*/
|
|
124
|
-
protected
|
|
224
|
+
protected getSystemVariables(): Record<string, string>;
|
|
225
|
+
private _readSystemVariables;
|
|
125
226
|
}
|
|
227
|
+
export {};
|
|
@@ -1,178 +1,230 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { SerializableError } from '../../models/SerializableError.js';
|
|
2
|
+
import { Environment } from '../../models/Environment.js';
|
|
3
|
+
import { DummyLogger } from '../../lib/logging/DummyLogger.js';
|
|
4
|
+
import { ProjectRequestRunner } from './ProjectRequestRunner.js';
|
|
5
|
+
import { pathExists, readJson } from '../../lib/fs/Fs.js';
|
|
6
|
+
import { BaseRunner } from './BaseRunner.js';
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
* Developers can run the entire project with the `recursive` flag set. They can also
|
|
10
|
-
* set the starting point with the `parent` options.
|
|
8
|
+
* A class to be extended to run an entire API Project for the given configuration options.
|
|
11
9
|
*
|
|
12
|
-
*
|
|
10
|
+
* The main purpose of this class (and its children) is to iterate over requests
|
|
11
|
+
* in the project and execute them one-by-one.
|
|
12
|
+
*
|
|
13
|
+
* Implementations allow to execute the requests in a serial model or in parallel mode, where
|
|
14
|
+
* a separate workers are created to run the same HTTP requests in each worker.
|
|
15
|
+
*
|
|
16
|
+
* This class generates a run report which other programs can use to build all kinds of UIs
|
|
17
|
+
* around the data collected during the run.
|
|
13
18
|
*/
|
|
14
|
-
export class ProjectRunner extends
|
|
15
|
-
|
|
16
|
-
|
|
19
|
+
export class ProjectRunner extends BaseRunner {
|
|
20
|
+
/**
|
|
21
|
+
* The HTTP project to run requests from.
|
|
22
|
+
*/
|
|
17
23
|
project;
|
|
18
|
-
masterEnvironment;
|
|
19
|
-
extraVariables;
|
|
20
24
|
/**
|
|
21
|
-
* The
|
|
25
|
+
* The execution options for the project.
|
|
26
|
+
*/
|
|
27
|
+
options;
|
|
28
|
+
/**
|
|
29
|
+
* The root object (project or a folder) where the program starts iterating over the requests.
|
|
30
|
+
*/
|
|
31
|
+
root;
|
|
32
|
+
/**
|
|
33
|
+
* The selected environment to apply to the requests.
|
|
34
|
+
*/
|
|
35
|
+
environment;
|
|
36
|
+
/**
|
|
37
|
+
* The events target instance for events dispatched by the request factory.
|
|
38
|
+
*/
|
|
39
|
+
target = new EventTarget();
|
|
40
|
+
/**
|
|
41
|
+
* This is used with `--iterations`. The index of the current iteration.
|
|
42
|
+
*/
|
|
43
|
+
index = 0;
|
|
44
|
+
/**
|
|
45
|
+
* The currently executed iteration loop.
|
|
22
46
|
*/
|
|
23
|
-
|
|
24
|
-
|
|
47
|
+
currentIteration;
|
|
48
|
+
/**
|
|
49
|
+
* The number of remaining iterations to run.
|
|
50
|
+
*/
|
|
51
|
+
remaining = 1;
|
|
52
|
+
/**
|
|
53
|
+
* Whether the configuration allows iterations (the parallel mode).
|
|
54
|
+
*/
|
|
55
|
+
hasIterations = false;
|
|
56
|
+
/**
|
|
57
|
+
* When set it won't amit any event.
|
|
58
|
+
* This can be used in a background worker when events are never handled.
|
|
59
|
+
*/
|
|
60
|
+
noEmit = false;
|
|
61
|
+
constructor() {
|
|
25
62
|
super();
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
29
|
-
this.masterEnvironment = opts.environment;
|
|
30
|
-
this.extraVariables = opts.variables;
|
|
63
|
+
this._requestHandler = this._requestHandler.bind(this);
|
|
64
|
+
this._responseHandler = this._responseHandler.bind(this);
|
|
65
|
+
this._errorHandler = this._errorHandler.bind(this);
|
|
31
66
|
}
|
|
32
67
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const parent = request.getParent() || project;
|
|
42
|
-
let variables;
|
|
43
|
-
if (VariablesStore.has(parent)) {
|
|
44
|
-
variables = VariablesStore.get(parent);
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
variables = await this.getVariables(parent);
|
|
48
|
-
VariablesStore.set(parent, variables);
|
|
49
|
-
}
|
|
50
|
-
const info = await this.execute(request, variables);
|
|
51
|
-
executed.push(info);
|
|
68
|
+
* A required step before running the project.
|
|
69
|
+
* It configures the execution context. It may throw an error when configuration is not valid.
|
|
70
|
+
*/
|
|
71
|
+
async configure(project, opts = {}) {
|
|
72
|
+
this.project = project;
|
|
73
|
+
this.options = opts || {};
|
|
74
|
+
if (typeof this.options.iterations === 'number' && this.options.iterations >= 0) {
|
|
75
|
+
this.remaining = this.options.iterations;
|
|
52
76
|
}
|
|
53
|
-
|
|
77
|
+
this.hasIterations = this.remaining > 1;
|
|
78
|
+
const root = opts.parent ? project.findFolder(opts.parent) : project;
|
|
79
|
+
if (!root) {
|
|
80
|
+
throw new Error(`Unable to locate the folder: ${opts.parent}`);
|
|
81
|
+
}
|
|
82
|
+
this.root = root;
|
|
83
|
+
this.environment = await this.getEnvironment();
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Creates the report of the execution.
|
|
87
|
+
*/
|
|
88
|
+
async createReport() {
|
|
89
|
+
const log = {
|
|
90
|
+
started: this.startTime,
|
|
91
|
+
ended: this.endTime,
|
|
92
|
+
iterations: this.executed,
|
|
93
|
+
};
|
|
94
|
+
return log;
|
|
54
95
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Reads the environment data to use with the execution.
|
|
98
|
+
* If the configured environment is a location of a file
|
|
99
|
+
* it is read as API Client's environment and used in the execution.
|
|
100
|
+
* Otherwise it searches for the environment in the list of the defined
|
|
101
|
+
* environments by the name of the key.
|
|
102
|
+
*
|
|
103
|
+
* It throws when the environment cannot be found or when the file contents is invalid.
|
|
104
|
+
*/
|
|
105
|
+
async getEnvironment() {
|
|
106
|
+
const { options } = this;
|
|
107
|
+
if (!options) {
|
|
108
|
+
throw new Error(`Run configure() first.`);
|
|
61
109
|
}
|
|
62
|
-
if (
|
|
63
|
-
|
|
110
|
+
if (!options.environment) {
|
|
111
|
+
return;
|
|
64
112
|
}
|
|
65
|
-
|
|
66
|
-
|
|
113
|
+
const fileExists = await pathExists(options.environment);
|
|
114
|
+
if (fileExists) {
|
|
115
|
+
const contents = await readJson(options.environment);
|
|
116
|
+
return new Environment(contents);
|
|
67
117
|
}
|
|
68
|
-
|
|
69
|
-
|
|
118
|
+
const root = this.root;
|
|
119
|
+
const envs = root.getEnvironments();
|
|
120
|
+
const env = envs.find(i => i.key === options.environment || i.info.name === options.environment);
|
|
121
|
+
if (!env) {
|
|
122
|
+
throw new SerializableError(`The environment cannot be found: ${options.environment}.`, 'EENVNOTFOUND');
|
|
123
|
+
}
|
|
124
|
+
return env;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Runs the requests from the project as configured.
|
|
128
|
+
*/
|
|
129
|
+
async executeIteration() {
|
|
130
|
+
const { environment, project, options, hasIterations, index, noEmit } = this;
|
|
131
|
+
if (!options || !project) {
|
|
132
|
+
throw new Error(`Run configure() first.`);
|
|
70
133
|
}
|
|
71
|
-
if (
|
|
72
|
-
|
|
134
|
+
if (!noEmit) {
|
|
135
|
+
this.emit('before-iteration', index, hasIterations);
|
|
73
136
|
}
|
|
74
|
-
const
|
|
75
|
-
|
|
137
|
+
const runner = new ProjectRequestRunner(project, {
|
|
138
|
+
environment,
|
|
139
|
+
logger: options.logger ? options.logger : new DummyLogger(),
|
|
140
|
+
eventTarget: this.target,
|
|
141
|
+
variables: this.getSystemVariables(),
|
|
142
|
+
});
|
|
143
|
+
runner.on('request', this._requestHandler);
|
|
144
|
+
runner.on('response', this._responseHandler);
|
|
145
|
+
runner.on('error', this._errorHandler);
|
|
146
|
+
this.currentIteration = {
|
|
147
|
+
index: this.index,
|
|
148
|
+
executed: [],
|
|
76
149
|
};
|
|
77
|
-
const requestData = request.expects.toJSON();
|
|
78
|
-
requestData.url = this.prepareRequestUrl(requestData.url, variables);
|
|
79
|
-
function variableHandler(e) {
|
|
80
|
-
if (e.defaultPrevented) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
const { name, value } = e.detail;
|
|
84
|
-
variables[name] = value;
|
|
85
|
-
e.preventDefault();
|
|
86
|
-
e.detail.result = Promise.resolve();
|
|
87
|
-
}
|
|
88
|
-
this.eventTarget.addEventListener(EventTypes.Environment.set, variableHandler);
|
|
89
150
|
try {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
await factory.processResponse(result);
|
|
97
|
-
request.setLog(result);
|
|
98
|
-
info.log = result;
|
|
99
|
-
this.emit('response', request.key, { ...result });
|
|
151
|
+
await runner.run({
|
|
152
|
+
parent: options.parent,
|
|
153
|
+
requests: options.request,
|
|
154
|
+
ignore: options.ignore,
|
|
155
|
+
recursive: options.recursive
|
|
156
|
+
});
|
|
100
157
|
}
|
|
101
158
|
catch (e) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
this.
|
|
159
|
+
const cause = e;
|
|
160
|
+
console.error(e);
|
|
161
|
+
this.currentIteration.error = cause.message || 'Unknown error ocurred';
|
|
162
|
+
// ...
|
|
105
163
|
}
|
|
106
|
-
this.
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
async getVariables(parent) {
|
|
110
|
-
if (this.masterEnvironment) {
|
|
111
|
-
return this.applyVariables([this.masterEnvironment]);
|
|
164
|
+
this.executed.push(this.currentIteration);
|
|
165
|
+
if (!noEmit) {
|
|
166
|
+
this.emit('after-iteration', index, hasIterations);
|
|
112
167
|
}
|
|
113
|
-
return this.createEnvironment(parent);
|
|
114
168
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
169
|
+
/**
|
|
170
|
+
* Retargets the "request" event from the factory.
|
|
171
|
+
*/
|
|
172
|
+
_requestHandler(key, request) {
|
|
173
|
+
if (!this.noEmit) {
|
|
174
|
+
this.emit('request', key, request);
|
|
175
|
+
}
|
|
118
176
|
}
|
|
119
177
|
/**
|
|
120
|
-
*
|
|
178
|
+
* Retargets the "response" event from the factory.
|
|
121
179
|
*/
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
180
|
+
_responseHandler(key, log) {
|
|
181
|
+
this.currentIteration?.executed.push(log);
|
|
182
|
+
if (!this.noEmit) {
|
|
183
|
+
this.emit('response', key, log);
|
|
184
|
+
}
|
|
125
185
|
}
|
|
126
186
|
/**
|
|
127
|
-
*
|
|
187
|
+
* Retargets the "error" event from the factory.
|
|
128
188
|
*/
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const { server, variables: envVariables } = environment;
|
|
134
|
-
if (server) {
|
|
135
|
-
baseUri = server.readUri();
|
|
136
|
-
}
|
|
137
|
-
if (envVariables.length) {
|
|
138
|
-
envVariables.forEach((item) => {
|
|
139
|
-
const defined = variables.findIndex(i => i.name === item.name);
|
|
140
|
-
if (defined >= 0) {
|
|
141
|
-
variables[defined] = item;
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
variables.push(item);
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
});
|
|
149
|
-
const { extraVariables } = this;
|
|
150
|
-
const ctx = VariablesProcessor.createContextFromProperties(variables);
|
|
151
|
-
if (extraVariables) {
|
|
152
|
-
Object.keys(extraVariables).forEach((key) => {
|
|
153
|
-
ctx[key] = extraVariables[key];
|
|
154
|
-
});
|
|
189
|
+
_errorHandler(key, log, message) {
|
|
190
|
+
this.currentIteration?.executed.push(log);
|
|
191
|
+
if (!this.noEmit) {
|
|
192
|
+
this.emit('error', key, log, message);
|
|
155
193
|
}
|
|
156
|
-
// the `baseUri` is reserved and always set to the environment's `baseUri`.
|
|
157
|
-
ctx.baseUri = baseUri || '';
|
|
158
|
-
return this.variablesProcessor.buildContext(ctx);
|
|
159
194
|
}
|
|
160
195
|
/**
|
|
161
|
-
*
|
|
162
|
-
* @param currentUrl The URL to process.
|
|
196
|
+
* @returns Reads the system variables based on the library configuration.
|
|
163
197
|
*/
|
|
164
|
-
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
198
|
+
getSystemVariables() {
|
|
199
|
+
const result = {};
|
|
200
|
+
const { options } = this;
|
|
201
|
+
if (!options) {
|
|
202
|
+
return result;
|
|
203
|
+
}
|
|
204
|
+
const { variables } = options;
|
|
205
|
+
if (typeof variables === 'undefined') {
|
|
206
|
+
return result;
|
|
168
207
|
}
|
|
169
|
-
if (
|
|
170
|
-
return
|
|
208
|
+
if (typeof variables === 'boolean') {
|
|
209
|
+
return variables ? this._readSystemVariables() : result;
|
|
171
210
|
}
|
|
172
|
-
if (
|
|
173
|
-
return
|
|
211
|
+
if (Array.isArray(variables)) {
|
|
212
|
+
return this._readSystemVariables(variables);
|
|
174
213
|
}
|
|
175
|
-
return
|
|
214
|
+
return variables;
|
|
215
|
+
}
|
|
216
|
+
_readSystemVariables(names) {
|
|
217
|
+
const result = {};
|
|
218
|
+
Object.keys(process.env).forEach((key) => {
|
|
219
|
+
if (names && !names.includes(key)) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const value = process.env[key];
|
|
223
|
+
if (value) {
|
|
224
|
+
result[key] = value;
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
return result;
|
|
176
228
|
}
|
|
177
229
|
}
|
|
178
230
|
//# sourceMappingURL=ProjectRunner.js.map
|