@typespec/spector 0.1.0-alpha.13-dev.0 → 0.1.0-alpha.13
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/CHANGELOG.md +7 -0
- package/dist/src/actions/helper.d.ts +2 -2
- package/dist/src/actions/helper.d.ts.map +1 -1
- package/dist/src/actions/helper.js +9 -3
- package/dist/src/actions/helper.js.map +1 -1
- package/dist/src/actions/server-test.d.ts.map +1 -1
- package/dist/src/actions/server-test.js +15 -11
- package/dist/src/actions/server-test.js.map +1 -1
- package/dist/src/app/app.d.ts +1 -0
- package/dist/src/app/app.d.ts.map +1 -1
- package/dist/src/app/app.js +16 -9
- package/dist/src/app/app.js.map +1 -1
- package/dist/src/app/request-processor.d.ts +2 -2
- package/dist/src/app/request-processor.d.ts.map +1 -1
- package/dist/src/app/request-processor.js +10 -6
- package/dist/src/app/request-processor.js.map +1 -1
- package/package.json +10 -11
- package/src/actions/helper.ts +17 -4
- package/src/actions/server-test.ts +32 -21
- package/src/app/app.ts +33 -14
- package/src/app/request-processor.ts +16 -4
- package/temp/.tsbuildinfo +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Change Log - @typespec/spector
|
|
2
2
|
|
|
3
|
+
## 0.1.0-alpha.13
|
|
4
|
+
|
|
5
|
+
### Features
|
|
6
|
+
|
|
7
|
+
- [#7066](https://github.com/microsoft/typespec/pull/7066) Add dynamic value resolution in spector mock apis with a new `dyn` string template builder
|
|
8
|
+
|
|
9
|
+
|
|
3
10
|
## 0.1.0-alpha.12
|
|
4
11
|
|
|
5
12
|
### Features
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { HttpMethod, MockBody, MockMultipartBody } from "@typespec/spec-api";
|
|
1
|
+
import { HttpMethod, MockBody, MockMultipartBody, ResolverConfig } from "@typespec/spec-api";
|
|
2
2
|
export interface ServiceRequest {
|
|
3
3
|
method: HttpMethod;
|
|
4
4
|
url: string;
|
|
@@ -7,7 +7,7 @@ export interface ServiceRequest {
|
|
|
7
7
|
query?: Record<string, unknown>;
|
|
8
8
|
pathParams?: Record<string, unknown>;
|
|
9
9
|
}
|
|
10
|
-
export declare function makeServiceCall(request: ServiceRequest): Promise<Response>;
|
|
10
|
+
export declare function makeServiceCall(request: ServiceRequest, config: ResolverConfig): Promise<Response>;
|
|
11
11
|
type EncodingType = "utf-8" | "base64" | "base64url" | "hex";
|
|
12
12
|
export declare function uint8ArrayToString(bytes: Uint8Array, format: EncodingType): string;
|
|
13
13
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../../src/actions/helper.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../../src/actions/helper.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,UAAU,EACV,QAAQ,EACR,iBAAiB,EACjB,cAAc,EACf,MAAM,oBAAoB,CAAC;AAE5B,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,UAAU,CAAC;IACnB,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,QAAQ,GAAG,iBAAiB,CAAC;IACpC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACtC;AAiDD,wBAAsB,eAAe,CACnC,OAAO,EAAE,cAAc,EACvB,MAAM,EAAE,cAAc,GACrB,OAAO,CAAC,QAAQ,CAAC,CA2BnB;AAED,KAAK,YAAY,GAAG,OAAO,GAAG,QAAQ,GAAG,WAAW,GAAG,KAAK,CAAC;AAC7D,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,YAAY,GAAG,MAAM,CAElF"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { expandDyns, } from "@typespec/spec-api";
|
|
1
2
|
function renderMultipartRequest(body) {
|
|
2
3
|
const formData = new FormData();
|
|
3
4
|
if (body.parts) {
|
|
@@ -36,17 +37,22 @@ function resolveUrl(request) {
|
|
|
36
37
|
}
|
|
37
38
|
return endpoint;
|
|
38
39
|
}
|
|
39
|
-
export async function makeServiceCall(request) {
|
|
40
|
+
export async function makeServiceCall(request, config) {
|
|
40
41
|
const url = resolveUrl(request);
|
|
41
42
|
let body;
|
|
42
|
-
let headers = request.headers;
|
|
43
|
+
let headers = expandDyns(request.headers, config);
|
|
43
44
|
if (request.body) {
|
|
44
45
|
if ("kind" in request.body) {
|
|
45
46
|
const formData = renderMultipartRequest(request.body);
|
|
46
47
|
body = formData;
|
|
47
48
|
}
|
|
48
49
|
else {
|
|
49
|
-
body
|
|
50
|
+
if (typeof request.body.rawContent === "string" || Buffer.isBuffer(request.body.rawContent)) {
|
|
51
|
+
body = request.body.rawContent;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
body = request.body.rawContent?.serialize(config);
|
|
55
|
+
}
|
|
50
56
|
headers = {
|
|
51
57
|
...headers,
|
|
52
58
|
...(request.body?.contentType && {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../../../src/actions/helper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"helper.js","sourceRoot":"","sources":["../../../src/actions/helper.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,GAKX,MAAM,oBAAoB,CAAC;AAW5B,SAAS,sBAAsB,CAAC,IAAuB;IACrD,MAAM,QAAQ,GAAG,IAAI,QAAQ,EAAE,CAAC;IAChC,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YAC7B,QAAQ,CAAC,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WAAW;iBACzC,CAAC;aACH,CAAC;QACJ,CAAC;IACH,CAAC;IACD,OAAO,MAAM,KAAK,CAAC,GAAG,EAAE;QACtB,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE;QACpC,IAAI;QACJ,OAAO;KACR,CAAC,CAAC;AACL,CAAC;AAGD,MAAM,UAAU,kBAAkB,CAAC,KAAiB,EAAE,MAAoB;IACxE,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;AAC7C,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server-test.d.ts","sourceRoot":"","sources":["../../../src/actions/server-test.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"server-test.d.ts","sourceRoot":"","sources":["../../../src/actions/server-test.ts"],"names":[],"mappings":"AAiBA,MAAM,WAAW,qBAAqB;IACpC,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB;AAwFD,MAAM,WAAW,iBAAiB;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAuBD,wBAAsB,UAAU,CAAC,aAAa,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB,iBA2EtF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ValidationError } from "@typespec/spec-api";
|
|
1
|
+
import { expandDyns, ValidationError, } from "@typespec/spec-api";
|
|
2
2
|
import deepEqual from "deep-equal";
|
|
3
3
|
import micromatch from "micromatch";
|
|
4
4
|
import { inspect } from "node:util";
|
|
@@ -11,10 +11,14 @@ class ServerTestsGenerator {
|
|
|
11
11
|
name = "";
|
|
12
12
|
mockApiDefinition;
|
|
13
13
|
serverBasePath = "";
|
|
14
|
+
resolverConfig;
|
|
14
15
|
constructor(name, mockApiDefinition, serverBasePath) {
|
|
15
16
|
this.name = name;
|
|
16
17
|
this.mockApiDefinition = mockApiDefinition;
|
|
17
18
|
this.serverBasePath = serverBasePath;
|
|
19
|
+
this.resolverConfig = {
|
|
20
|
+
baseUrl: serverBasePath,
|
|
21
|
+
};
|
|
18
22
|
}
|
|
19
23
|
async executeScenario() {
|
|
20
24
|
log(`Executing ${this.name} endpoint - Method: ${this.mockApiDefinition.method}`);
|
|
@@ -25,7 +29,7 @@ class ServerTestsGenerator {
|
|
|
25
29
|
headers: this.mockApiDefinition.request?.headers,
|
|
26
30
|
query: this.mockApiDefinition.request?.query,
|
|
27
31
|
pathParams: this.mockApiDefinition.request?.pathParams,
|
|
28
|
-
});
|
|
32
|
+
}, this.resolverConfig);
|
|
29
33
|
if (this.mockApiDefinition.response.status !== response.status) {
|
|
30
34
|
throw new ValidationError("Status code mismatch", this.mockApiDefinition.response.status, response.status);
|
|
31
35
|
}
|
|
@@ -33,10 +37,10 @@ class ServerTestsGenerator {
|
|
|
33
37
|
await this.#validateBody(response, this.mockApiDefinition.response.body);
|
|
34
38
|
}
|
|
35
39
|
if (this.mockApiDefinition.response.headers) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
throw new ValidationError(`Response headers mismatch`,
|
|
40
|
+
const headers = expandDyns(this.mockApiDefinition.response.headers, this.resolverConfig);
|
|
41
|
+
for (const key in headers) {
|
|
42
|
+
if (headers[key] !== response.headers.get(key)) {
|
|
43
|
+
throw new ValidationError(`Response headers mismatch`, headers[key], response.headers.get(key));
|
|
40
44
|
}
|
|
41
45
|
}
|
|
42
46
|
}
|
|
@@ -50,18 +54,18 @@ class ServerTestsGenerator {
|
|
|
50
54
|
}
|
|
51
55
|
else {
|
|
52
56
|
const responseData = await response.text();
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
57
|
+
const raw = typeof body.rawContent === "string"
|
|
58
|
+
? body.rawContent
|
|
59
|
+
: body.rawContent?.serialize(this.resolverConfig);
|
|
56
60
|
switch (body.contentType) {
|
|
57
61
|
case "application/xml":
|
|
58
62
|
case "text/plain":
|
|
59
63
|
if (body.rawContent !== responseData) {
|
|
60
|
-
throw new ValidationError("Response data mismatch",
|
|
64
|
+
throw new ValidationError("Response data mismatch", raw, responseData);
|
|
61
65
|
}
|
|
62
66
|
break;
|
|
63
67
|
case "application/json":
|
|
64
|
-
const expected = JSON.parse(
|
|
68
|
+
const expected = JSON.parse(raw);
|
|
65
69
|
const actual = JSON.parse(responseData);
|
|
66
70
|
if (!deepEqual(actual, expected, { strict: true })) {
|
|
67
71
|
throw new ValidationError("Response data mismatch", expected, actual);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server-test.js","sourceRoot":"","sources":["../../../src/actions/server-test.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"server-test.js","sourceRoot":"","sources":["../../../src/actions/server-test.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EAIV,eAAe,GAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,SAAS,MAAM,YAAY,CAAC;AACnC,OAAO,UAAU,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,MAAM,YAAY,CAAC;AAC5B,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,MAAM,gBAAgB,GAAG,uBAAuB,CAAC;AAOjD,MAAM,oBAAoB;IAChB,IAAI,GAAW,EAAE,CAAC;IAClB,iBAAiB,CAAoB;IACrC,cAAc,GAAW,EAAE,CAAC;IAC5B,cAAc,CAAiB;IAEvC,YAAY,IAAY,EAAE,iBAAoC,EAAE,cAAsB;QACpF,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;QAC3C,IAAI,CAAC,cAAc,G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GAAG,CAAC,cAAc,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAChE,CAAC,CAAC,CAAC;IAEH,IAAI,kBAAkB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAClC,GAAG,CAAC,kBAAkB,CAAC,CAAC;QACxB,kBAAkB,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE;YACxC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC;YACpE,GAAG,CAAC,GAAG,UAAU,CAAC,OAAO,EAAE,CAAC,CAAC;QAC/B,CAAC,CAAC,CAAC;IACL,CAAC;IACD,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,KAAK,eAAe,CAAC,MAAM,SAAS,CAAC,CAAC,CAAC,CAAC;IAC7D,IAAI,kBAAkB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAClC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,KAAK,kBAAkB,CAAC,MAAM,SAAS,CAAC,CAAC,CAAC;IACvD,CAAC;IAED,OAAO,CAAC,IAAI,CAAC,kBAAkB,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACtD,CAAC;AAED,SAAS,GAAG,CAAC,OAAe;IAC1B,sCAAsC;IACtC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACvB,CAAC"}
|
package/dist/src/app/app.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../../../src/app/app.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../../../src/app/app.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,eAAe,EAChB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAQhE,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAG/C,MAAM,WAAW,6BAA6B;IAC5C,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IAC3C,iBAAiB,EAAE,iBAAiB,CAAC;CACtC;AAED,qBAAa,UAAU;IAMT,OAAO,CAAC,MAAM;IAL1B,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,eAAe,CAAkB;IACzC,OAAO,CAAC,cAAc,CAAiB;gBAEnB,MAAM,EAAE,gBAAgB;IAQ/B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAgCnC,OAAO,CAAC,gBAAgB;CAqBzB"}
|
package/dist/src/app/app.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { expandDyns, } from "@typespec/spec-api";
|
|
1
2
|
import { Router } from "express";
|
|
2
3
|
import { getScenarioMetadata } from "../coverage/common.js";
|
|
3
4
|
import { CoverageTracker } from "../coverage/coverage-tracker.js";
|
|
@@ -11,10 +12,14 @@ export class MockApiApp {
|
|
|
11
12
|
router = Router();
|
|
12
13
|
server;
|
|
13
14
|
coverageTracker;
|
|
15
|
+
resolverConfig;
|
|
14
16
|
constructor(config) {
|
|
15
17
|
this.config = config;
|
|
16
18
|
this.server = new MockApiServer({ port: config.port });
|
|
17
19
|
this.coverageTracker = new CoverageTracker(config.coverageFile);
|
|
20
|
+
this.resolverConfig = {
|
|
21
|
+
baseUrl: `http://localhost:${config.port}`,
|
|
22
|
+
};
|
|
18
23
|
}
|
|
19
24
|
async start() {
|
|
20
25
|
this.server.use("/", internalRouter);
|
|
@@ -46,10 +51,10 @@ export class MockApiApp {
|
|
|
46
51
|
registerScenario(name, scenario) {
|
|
47
52
|
for (const endpoint of scenario.apis) {
|
|
48
53
|
if (!endpoint.handler) {
|
|
49
|
-
endpoint.handler = createHandler(endpoint);
|
|
54
|
+
endpoint.handler = createHandler(endpoint, this.resolverConfig);
|
|
50
55
|
}
|
|
51
56
|
this.router.route(endpoint.uri)[endpoint.method]((req, res) => {
|
|
52
|
-
processRequest(this.coverageTracker, name, endpoint.uri, req, res, endpoint.handler).catch((e) => {
|
|
57
|
+
processRequest(this.coverageTracker, name, endpoint.uri, req, res, endpoint.handler, this.resolverConfig).catch((e) => {
|
|
53
58
|
logger.error("Unexpected request error", e);
|
|
54
59
|
res.status(500).end();
|
|
55
60
|
});
|
|
@@ -57,7 +62,7 @@ export class MockApiApp {
|
|
|
57
62
|
}
|
|
58
63
|
}
|
|
59
64
|
}
|
|
60
|
-
function validateBody(req, body) {
|
|
65
|
+
function validateBody(req, body, config) {
|
|
61
66
|
if ("kind" in body) {
|
|
62
67
|
// custom handler for now.
|
|
63
68
|
}
|
|
@@ -66,29 +71,31 @@ function validateBody(req, body) {
|
|
|
66
71
|
req.expect.rawBodyEquals(body.rawContent);
|
|
67
72
|
}
|
|
68
73
|
else {
|
|
74
|
+
const raw = typeof body.rawContent === "string" ? body.rawContent : body.rawContent?.serialize(config);
|
|
69
75
|
switch (body.contentType) {
|
|
70
76
|
case "application/json":
|
|
71
|
-
req.expect.coercedBodyEquals(JSON.parse(
|
|
77
|
+
req.expect.coercedBodyEquals(JSON.parse(raw));
|
|
72
78
|
break;
|
|
73
79
|
case "application/xml":
|
|
74
|
-
req.expect.xmlBodyEquals(
|
|
80
|
+
req.expect.xmlBodyEquals(raw.replace(`<?xml version='1.0' encoding='UTF-8'?>`, ""));
|
|
75
81
|
break;
|
|
76
82
|
default:
|
|
77
|
-
req.expect.rawBodyEquals(
|
|
83
|
+
req.expect.rawBodyEquals(raw);
|
|
78
84
|
}
|
|
79
85
|
}
|
|
80
86
|
}
|
|
81
87
|
}
|
|
82
|
-
function createHandler(apiDefinition) {
|
|
88
|
+
function createHandler(apiDefinition, config) {
|
|
83
89
|
return (req) => {
|
|
84
90
|
const body = apiDefinition.request?.body;
|
|
85
91
|
// Validate body if present in the request
|
|
86
92
|
if (body) {
|
|
87
|
-
validateBody(req, body);
|
|
93
|
+
validateBody(req, body, config);
|
|
88
94
|
}
|
|
89
95
|
// Validate headers if present in the request
|
|
90
96
|
if (apiDefinition.request?.headers) {
|
|
91
|
-
|
|
97
|
+
const headers = expandDyns(apiDefinition.request.headers, config);
|
|
98
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
92
99
|
if (key.toLowerCase() !== "content-type") {
|
|
93
100
|
if (Array.isArray(value)) {
|
|
94
101
|
req.expect.deepEqual(req.headers[key], value);
|
package/dist/src/app/app.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.js","sourceRoot":"","sources":["../../../src/app/app.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"app.js","sourceRoot":"","sources":["../../../src/app/app.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,GAQX,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAY,MAAM,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAClE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAOxD,MAAM,OAAO,UAAU;IAMD;IALZ,MAAM,GAAG,MAAM,EAAE,CAAC;IAClB,MAAM,CAAgB;IACtB,eAAe,CAAkB;IACjC,cAAc,CAAiB;IAEvC,YAAoB,MAAwB;QAA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AAC;oBACN,GAAG,CAAC,MAAM,CAAC,kBAAkB,CAAC,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;gBACpD,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC;QAED,gDAAgD;QAChD,OAAO;YACL,MAAM,EAAE,aAAa,CAAC,QAAQ,CAAC,MAAM;YACrC,IAAI,EAAE,aAAa,CAAC,QAAQ,CAAC,IAAI;YACjC,OAAO,EAAE,aAAa,CAAC,QAAQ,CAAC,OAAO;SACxC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { MockRequestHandler, RequestExt } from "@typespec/spec-api";
|
|
1
|
+
import { MockRequestHandler, RequestExt, ResolverConfig } from "@typespec/spec-api";
|
|
2
2
|
import { Response } from "express";
|
|
3
3
|
import { CoverageTracker } from "../coverage/coverage-tracker.js";
|
|
4
|
-
export declare function processRequest(coverageTracker: CoverageTracker, scenarioName: string, scenarioUri: string, request: RequestExt, response: Response, func: MockRequestHandler): Promise<void>;
|
|
4
|
+
export declare function processRequest(coverageTracker: CoverageTracker, scenarioName: string, scenarioUri: string, request: RequestExt, response: Response, func: MockRequestHandler, resolverConfig: ResolverConfig): Promise<void>;
|
|
5
5
|
//# sourceMappingURL=request-processor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-processor.d.ts","sourceRoot":"","sources":["../../../src/app/request-processor.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"request-processor.d.ts","sourceRoot":"","sources":["../../../src/app/request-processor.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,kBAAkB,EAElB,UAAU,EACV,cAAc,EAEf,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnC,OAAO,EAAE,eAAe,EAAE,MAAM,iCAAiC,CAAC;AAGlE,wBAAsB,cAAc,CAClC,eAAe,EAAE,eAAe,EAChC,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,UAAU,EACnB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,kBAAkB,EACxB,cAAc,EAAE,cAAc,GAC7B,OAAO,CAAC,IAAI,CAAC,CASf"}
|
|
@@ -1,22 +1,26 @@
|
|
|
1
|
-
import { MockRequest, ValidationError, } from "@typespec/spec-api";
|
|
1
|
+
import { expandDyns, MockRequest, ValidationError, } from "@typespec/spec-api";
|
|
2
2
|
import { inspect } from "util";
|
|
3
3
|
import { logger } from "../logger.js";
|
|
4
|
-
export async function processRequest(coverageTracker, scenarioName, scenarioUri, request, response, func) {
|
|
4
|
+
export async function processRequest(coverageTracker, scenarioName, scenarioUri, request, response, func, resolverConfig) {
|
|
5
5
|
const mockRequest = new MockRequest(request);
|
|
6
6
|
const mockResponse = await callHandler(mockRequest, response, func);
|
|
7
7
|
if (mockResponse === undefined) {
|
|
8
8
|
return;
|
|
9
9
|
}
|
|
10
10
|
await coverageTracker.trackEndpointResponse(scenarioName, scenarioUri, mockResponse);
|
|
11
|
-
processResponse(response, mockResponse);
|
|
11
|
+
processResponse(response, mockResponse, resolverConfig);
|
|
12
12
|
}
|
|
13
|
-
const processResponse = (response, mockResponse) => {
|
|
13
|
+
const processResponse = (response, mockResponse, resolverConfig) => {
|
|
14
14
|
response.status(mockResponse.status);
|
|
15
15
|
if (mockResponse.headers) {
|
|
16
|
-
response.set(mockResponse.headers);
|
|
16
|
+
response.set(expandDyns(mockResponse.headers, resolverConfig));
|
|
17
17
|
}
|
|
18
18
|
if (mockResponse.body) {
|
|
19
|
-
|
|
19
|
+
const raw = typeof mockResponse.body.rawContent === "string" ||
|
|
20
|
+
Buffer.isBuffer(mockResponse.body.rawContent)
|
|
21
|
+
? mockResponse.body.rawContent
|
|
22
|
+
: mockResponse.body.rawContent?.serialize(resolverConfig);
|
|
23
|
+
response.contentType(mockResponse.body.contentType).send(raw);
|
|
20
24
|
}
|
|
21
25
|
response.end();
|
|
22
26
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-processor.js","sourceRoot":"","sources":["../../../src/app/request-processor.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,
|
|
1
|
+
{"version":3,"file":"request-processor.js","sourceRoot":"","sources":["../../../src/app/request-processor.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,WAAW,EAKX,eAAe,GAChB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,eAAgC,EAChC,YAAoB,EACpB,WAAmB,EACnB,OAAmB,EACnB,QAAkB,EAClB,IAAwB,EACxB,cAA8B;IAE9B,MAAM,WAAW,GAAG,IAAI,WAAW,CAAC,OAAO,CAAC,CAAC;IAC7C,MAAM,YAAY,GAAG,MAAM,WAAW,CAAC,WAAW,EAAE,QAAQ,EAAE,IAAI,CAAC,CAAC;IACpE,IAAI,YAAY,KAAK,SAAS,EAAE,CAAC;QAC/B,OAAO;IACT,CAAC;IAED,MAAM,eAAe,CAAC,qBAAqB,CAAC,YAAY,EAAE,WAAW,EAAE,YAAY,CAAC,CAAC;IACrF,eAAe,CAAC,QAAQ,EAAE,YAAY,EAAE,cAAc,CAAC,CAAC;AAC1D,CAAC;AAED,MAAM,eAAe,GAAG,CACtB,QAAkB,EAClB,YAA0B,EAC1B,cAA8B,EAC9B,EAAE;IACF,QAAQ,CAAC,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;IAErC,IAAI,YAAY,CAAC,OAAO,EAAE,CAAC;QACzB,QAAQ,CAAC,GAAG,CAAC,UAAU,CAAC,YAAY,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC;IACjE,CAAC;IAED,IAAI,YAAY,CAAC,IAAI,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,CAAC;AACH,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typespec/spector",
|
|
3
|
-
"version": "0.1.0-alpha.13
|
|
3
|
+
"version": "0.1.0-alpha.13",
|
|
4
4
|
"description": "Typespec Core Tool to validate, run mock api, collect coverage.",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -28,12 +28,6 @@
|
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@azure/identity": "~4.8.0",
|
|
30
30
|
"@types/js-yaml": "^4.0.5",
|
|
31
|
-
"@typespec/compiler": "^1.0.0-rc.1",
|
|
32
|
-
"@typespec/http": "^1.0.0-rc.1",
|
|
33
|
-
"@typespec/rest": "^0.69.0 || >=0.70.0-dev <0.70.0",
|
|
34
|
-
"@typespec/spec-api": "^0.1.0-alpha.4 || >=0.1.0-alpha.5-dev <0.1.0-alpha.5",
|
|
35
|
-
"@typespec/spec-coverage-sdk": "^0.1.0-alpha.6 || >=0.1.0-alpha.7-dev <0.1.0-alpha.7",
|
|
36
|
-
"@typespec/versioning": "^0.69.0 || >=0.70.0-dev <0.70.0",
|
|
37
31
|
"ajv": "~8.17.1",
|
|
38
32
|
"body-parser": "^1.20.3",
|
|
39
33
|
"deep-equal": "^2.2.0",
|
|
@@ -46,7 +40,13 @@
|
|
|
46
40
|
"picocolors": "~1.1.1",
|
|
47
41
|
"source-map-support": "~0.5.21",
|
|
48
42
|
"xml2js": "^0.6.2",
|
|
49
|
-
"yargs": "~17.7.2"
|
|
43
|
+
"yargs": "~17.7.2",
|
|
44
|
+
"@typespec/compiler": "^1.0.0-rc.1",
|
|
45
|
+
"@typespec/rest": "^0.69.0",
|
|
46
|
+
"@typespec/http": "^1.0.0-rc.1",
|
|
47
|
+
"@typespec/spec-api": "^0.1.0-alpha.5",
|
|
48
|
+
"@typespec/spec-coverage-sdk": "^0.1.0-alpha.6",
|
|
49
|
+
"@typespec/versioning": "^0.69.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@types/body-parser": "^1.19.2",
|
|
@@ -58,11 +58,10 @@
|
|
|
58
58
|
"@types/node": "~22.13.11",
|
|
59
59
|
"@types/xml2js": "^0.4.11",
|
|
60
60
|
"@types/yargs": "~17.0.33",
|
|
61
|
-
"@typespec/tspd": "^0.69.0 || >=0.70.0-dev <0.70.0",
|
|
62
61
|
"rimraf": "~6.0.1",
|
|
63
|
-
"typescript": "~5.8.2"
|
|
62
|
+
"typescript": "~5.8.2",
|
|
63
|
+
"@typespec/tspd": "^0.69.0"
|
|
64
64
|
},
|
|
65
|
-
"peerDependencies": {},
|
|
66
65
|
"scripts": {
|
|
67
66
|
"watch": "tsc -p ./tsconfig.build.json --watch",
|
|
68
67
|
"build": "pnpm gen-extern-signature && tsc -p tsconfig.build.json",
|
package/src/actions/helper.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
expandDyns,
|
|
3
|
+
HttpMethod,
|
|
4
|
+
MockBody,
|
|
5
|
+
MockMultipartBody,
|
|
6
|
+
ResolverConfig,
|
|
7
|
+
} from "@typespec/spec-api";
|
|
2
8
|
|
|
3
9
|
export interface ServiceRequest {
|
|
4
10
|
method: HttpMethod;
|
|
@@ -56,16 +62,23 @@ function resolveUrl(request: ServiceRequest) {
|
|
|
56
62
|
return endpoint;
|
|
57
63
|
}
|
|
58
64
|
|
|
59
|
-
export async function makeServiceCall(
|
|
65
|
+
export async function makeServiceCall(
|
|
66
|
+
request: ServiceRequest,
|
|
67
|
+
config: ResolverConfig,
|
|
68
|
+
): Promise<Response> {
|
|
60
69
|
const url = resolveUrl(request);
|
|
61
70
|
let body;
|
|
62
|
-
let headers = request.headers as Record<string, string>;
|
|
71
|
+
let headers = expandDyns(request.headers, config) as Record<string, string>;
|
|
63
72
|
if (request.body) {
|
|
64
73
|
if ("kind" in request.body) {
|
|
65
74
|
const formData = renderMultipartRequest(request.body);
|
|
66
75
|
body = formData;
|
|
67
76
|
} else {
|
|
68
|
-
body
|
|
77
|
+
if (typeof request.body.rawContent === "string" || Buffer.isBuffer(request.body.rawContent)) {
|
|
78
|
+
body = request.body.rawContent;
|
|
79
|
+
} else {
|
|
80
|
+
body = request.body.rawContent?.serialize(config);
|
|
81
|
+
}
|
|
69
82
|
headers = {
|
|
70
83
|
...headers,
|
|
71
84
|
...(request.body?.contentType && {
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
expandDyns,
|
|
3
|
+
MockApiDefinition,
|
|
4
|
+
MockBody,
|
|
5
|
+
ResolverConfig,
|
|
6
|
+
ValidationError,
|
|
7
|
+
} from "@typespec/spec-api";
|
|
2
8
|
import deepEqual from "deep-equal";
|
|
3
9
|
import micromatch from "micromatch";
|
|
4
10
|
import { inspect } from "node:util";
|
|
@@ -18,24 +24,31 @@ class ServerTestsGenerator {
|
|
|
18
24
|
private name: string = "";
|
|
19
25
|
private mockApiDefinition: MockApiDefinition;
|
|
20
26
|
private serverBasePath: string = "";
|
|
27
|
+
private resolverConfig: ResolverConfig;
|
|
21
28
|
|
|
22
29
|
constructor(name: string, mockApiDefinition: MockApiDefinition, serverBasePath: string) {
|
|
23
30
|
this.name = name;
|
|
24
31
|
this.mockApiDefinition = mockApiDefinition;
|
|
25
32
|
this.serverBasePath = serverBasePath;
|
|
33
|
+
this.resolverConfig = {
|
|
34
|
+
baseUrl: serverBasePath,
|
|
35
|
+
};
|
|
26
36
|
}
|
|
27
37
|
|
|
28
38
|
public async executeScenario() {
|
|
29
39
|
log(`Executing ${this.name} endpoint - Method: ${this.mockApiDefinition.method}`);
|
|
30
40
|
|
|
31
|
-
const response = await makeServiceCall(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
41
|
+
const response = await makeServiceCall(
|
|
42
|
+
{
|
|
43
|
+
method: this.mockApiDefinition.method,
|
|
44
|
+
url: `${this.serverBasePath}${this.mockApiDefinition.uri}`,
|
|
45
|
+
body: this.mockApiDefinition.request?.body,
|
|
46
|
+
headers: this.mockApiDefinition.request?.headers,
|
|
47
|
+
query: this.mockApiDefinition.request?.query,
|
|
48
|
+
pathParams: this.mockApiDefinition.request?.pathParams,
|
|
49
|
+
},
|
|
50
|
+
this.resolverConfig,
|
|
51
|
+
);
|
|
39
52
|
|
|
40
53
|
if (this.mockApiDefinition.response.status !== response.status) {
|
|
41
54
|
throw new ValidationError(
|
|
@@ -50,14 +63,12 @@ class ServerTestsGenerator {
|
|
|
50
63
|
}
|
|
51
64
|
|
|
52
65
|
if (this.mockApiDefinition.response.headers) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
response.headers.get(key)?.replace(this.serverBasePath, "")
|
|
57
|
-
) {
|
|
66
|
+
const headers = expandDyns(this.mockApiDefinition.response.headers, this.resolverConfig);
|
|
67
|
+
for (const key in headers) {
|
|
68
|
+
if (headers[key] !== response.headers.get(key)) {
|
|
58
69
|
throw new ValidationError(
|
|
59
70
|
`Response headers mismatch`,
|
|
60
|
-
|
|
71
|
+
headers[key],
|
|
61
72
|
response.headers.get(key),
|
|
62
73
|
);
|
|
63
74
|
}
|
|
@@ -73,19 +84,19 @@ class ServerTestsGenerator {
|
|
|
73
84
|
}
|
|
74
85
|
} else {
|
|
75
86
|
const responseData = await response.text();
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
87
|
+
const raw =
|
|
88
|
+
typeof body.rawContent === "string"
|
|
89
|
+
? body.rawContent
|
|
90
|
+
: body.rawContent?.serialize(this.resolverConfig);
|
|
80
91
|
switch (body.contentType) {
|
|
81
92
|
case "application/xml":
|
|
82
93
|
case "text/plain":
|
|
83
94
|
if (body.rawContent !== responseData) {
|
|
84
|
-
throw new ValidationError("Response data mismatch",
|
|
95
|
+
throw new ValidationError("Response data mismatch", raw, responseData);
|
|
85
96
|
}
|
|
86
97
|
break;
|
|
87
98
|
case "application/json":
|
|
88
|
-
const expected = JSON.parse(
|
|
99
|
+
const expected = JSON.parse(raw as any);
|
|
89
100
|
const actual = JSON.parse(responseData);
|
|
90
101
|
if (!deepEqual(actual, expected, { strict: true })) {
|
|
91
102
|
throw new ValidationError("Response data mismatch", expected, actual);
|