Skip to content

Commit ee43e82

Browse files
committed
Added plugin support
1 parent 04523a3 commit ee43e82

File tree

7 files changed

+408
-364
lines changed

7 files changed

+408
-364
lines changed

.prettierrc.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"semi": false
4+
}

src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import runHTTPStep, { HTTPStep, HTTPStepRequest, HTTPStepResponse } from './step
2020
import runGRPCStep, { gRPCStep, gRPCStepRequest, gRPCStepResponse } from './steps/grpc'
2121
import runSSEStep, { SSEStep, SSEStepRequest, SSEStepResponse } from './steps/sse'
2222
import runDelayStep from './steps/delay'
23+
import runPluginStep, { PluginStep } from './steps/plugin'
2324

2425
export type Workflow = {
2526
version: string
@@ -107,6 +108,7 @@ export type Step = {
107108
grpc?: gRPCStep
108109
sse?: SSEStep
109110
delay?: string
111+
plugin?: PluginStep
110112
}
111113

112114
export type StepCheckValue = {
@@ -317,6 +319,10 @@ async function runTest(id: string, test: Test, schemaValidator: Ajv, options?: W
317319
runResult = await runDelayStep(step.delay)
318320
}
319321

322+
if (step.plugin) {
323+
runResult = await runPluginStep(step.plugin, captures, cookies, schemaValidator, options, config)
324+
}
325+
320326
stepResult = { ...stepResult, ...runResult }
321327
stepResult.passed = didChecksPass(stepResult)
322328
} catch (error) {

src/steps/delay.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import parseDuration from "parse-duration";
2-
import { StepRunResult } from "..";
1+
import parseDuration from 'parse-duration'
2+
import { StepRunResult } from '..'
33

4-
export default async function DelayStep(
5-
params: string,
6-
) {
4+
export default async function DelayStep(params: string) {
75
const stepResult: StepRunResult = {
8-
type: "delay",
9-
};
6+
type: 'delay',
7+
}
108

11-
stepResult.type = "delay";
9+
stepResult.type = 'delay'
1210
await new Promise((resolve) =>
13-
setTimeout(resolve, parseDuration(params || "5000"))
14-
);
11+
setTimeout(resolve, parseDuration(params || '5000'))
12+
)
1513

16-
return stepResult;
14+
return stepResult
1715
}

src/steps/grpc.ts

+46-41
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
import { StepCheckCaptures, StepCheckJSONPath, StepCheckMatcher, StepCheckPerformance } from "..";
2-
import { CapturesStorage } from "./../utils/runner";
3-
import { TLSCertificate, getTLSCertificate } from "./../utils/auth";
1+
import {
2+
StepCheckCaptures,
3+
StepCheckJSONPath,
4+
StepCheckMatcher,
5+
StepCheckPerformance,
6+
} from '..'
7+
import { CapturesStorage } from './../utils/runner'
8+
import { TLSCertificate, getTLSCertificate } from './../utils/auth'
49
import { Credential } from './../utils/auth'
510

6-
import path from "node:path";
7-
import { JSONPath } from "jsonpath-plus";
8-
import Ajv from "ajv";
9-
import { StepRunResult, WorkflowConfig, WorkflowOptions } from "..";
10-
import { Matcher, checkResult } from "../matcher";
11-
import { gRPCRequestMetadata, makeRequest } from "cool-grpc";
12-
const { co2 } = require("@tgwf/co2");
11+
import path from 'node:path'
12+
import { JSONPath } from 'jsonpath-plus'
13+
import Ajv from 'ajv'
14+
import { StepRunResult, WorkflowConfig, WorkflowOptions } from '..'
15+
import { Matcher, checkResult } from '../matcher'
16+
import { gRPCRequestMetadata, makeRequest } from 'cool-grpc'
17+
const { co2 } = require('@tgwf/co2')
1318

1419
export type gRPCStep = {
1520
proto: string | string[]
@@ -74,33 +79,33 @@ export default async function gRPCStep(
7479
config?: WorkflowConfig
7580
) {
7681
const stepResult: StepRunResult = {
77-
type: "grpc",
78-
};
82+
type: 'grpc',
83+
}
7984

80-
const ssw = new co2();
85+
const ssw = new co2()
8186

8287
// Load TLS configuration from file or string
83-
let tlsConfig: TLSCertificate | undefined;
88+
let tlsConfig: TLSCertificate | undefined
8489
if (params.auth) {
8590
tlsConfig = await getTLSCertificate(params.auth.tls, {
8691
workflowPath: options?.path,
87-
});
92+
})
8893
}
8994

90-
const protos: string[] = [];
95+
const protos: string[] = []
9196
if (config?.grpc?.proto) {
92-
protos.push(...config.grpc.proto);
97+
protos.push(...config.grpc.proto)
9398
}
9499

95100
if (params.proto) {
96101
protos.push(
97102
...(Array.isArray(params.proto) ? params.proto : [params.proto])
98-
);
103+
)
99104
}
100105

101106
const proto = protos.map((p) =>
102107
path.join(path.dirname(options?.path || __dirname), p)
103-
);
108+
)
104109

105110
const request: gRPCStepRequest = {
106111
proto,
@@ -109,110 +114,110 @@ export default async function gRPCStep(
109114
service: params.service,
110115
method: params.method,
111116
data: params.data,
112-
};
117+
}
113118

114119
const { metadata, statusCode, statusMessage, data, size } = await makeRequest(
115120
proto,
116121
{
117122
...request,
118123
tls: tlsConfig,
119124
beforeRequest: (req) => {
120-
options?.ee?.emit("step:grpc_request", request);
125+
options?.ee?.emit('step:grpc_request', request)
121126
},
122127
afterResponse: (res) => {
123-
options?.ee?.emit("step:grpc_response", res);
128+
options?.ee?.emit('step:grpc_response', res)
124129
},
125130
}
126-
);
131+
)
127132

128-
stepResult.request = request;
133+
stepResult.request = request
129134
stepResult.response = {
130135
body: data,
131136
co2: ssw.perByte(size),
132137
size: size,
133138
status: statusCode,
134139
statusText: statusMessage,
135140
metadata,
136-
};
141+
}
137142

138143
// Captures
139144
if (params.captures) {
140145
for (const name in params.captures) {
141-
const capture = params.captures[name];
146+
const capture = params.captures[name]
142147
if (capture.jsonpath) {
143-
captures[name] = JSONPath({ path: capture.jsonpath, json: data })[0];
148+
captures[name] = JSONPath({ path: capture.jsonpath, json: data })[0]
144149
}
145150
}
146151
}
147152

148153
if (params.check) {
149-
stepResult.checks = {};
154+
stepResult.checks = {}
150155

151156
// Check JSON
152157
if (params.check.json) {
153-
stepResult.checks.json = checkResult(data, params.check.json);
158+
stepResult.checks.json = checkResult(data, params.check.json)
154159
}
155160

156161
// Check Schema
157162
if (params.check.schema) {
158-
const validate = schemaValidator.compile(params.check.schema);
163+
const validate = schemaValidator.compile(params.check.schema)
159164
stepResult.checks.schema = {
160165
expected: params.check.schema,
161166
given: data,
162167
passed: validate(data),
163-
};
168+
}
164169
}
165170

166171
// Check JSONPath
167172
if (params.check.jsonpath) {
168-
stepResult.checks.jsonpath = {};
173+
stepResult.checks.jsonpath = {}
169174

170175
for (const path in params.check.jsonpath) {
171-
const result = JSONPath({ path, json: data });
176+
const result = JSONPath({ path, json: data })
172177
stepResult.checks.jsonpath[path] = checkResult(
173178
result[0],
174179
params.check.jsonpath[path]
175-
);
180+
)
176181
}
177182
}
178183

179184
// Check captures
180185
if (params.check.captures) {
181-
stepResult.checks.captures = {};
186+
stepResult.checks.captures = {}
182187

183188
for (const capture in params.check.captures) {
184189
stepResult.checks.captures[capture] = checkResult(
185190
captures[capture],
186191
params.check.captures[capture]
187-
);
192+
)
188193
}
189194
}
190195

191196
// Check performance
192197
if (params.check.performance) {
193-
stepResult.checks.performance = {};
198+
stepResult.checks.performance = {}
194199

195200
if (params.check.performance.total) {
196201
stepResult.checks.performance.total = checkResult(
197202
stepResult.response?.duration,
198203
params.check.performance.total
199-
);
204+
)
200205
}
201206
}
202207

203208
// Check byte size
204209
if (params.check.size) {
205-
stepResult.checks.size = checkResult(size, params.check.size);
210+
stepResult.checks.size = checkResult(size, params.check.size)
206211
}
207212

208213
// Check co2 emissions
209214
if (params.check.co2) {
210215
stepResult.checks.co2 = checkResult(
211216
stepResult.response?.co2,
212217
params.check.co2
213-
);
218+
)
214219
}
215220
}
216221

217-
return stepResult;
222+
return stepResult
218223
}

0 commit comments

Comments
 (0)