@thymian/rules-testing 0.0.0-stage → 0.4.0-canary.20261123-321a806
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/README.md +225 -2
- package/dist/conforming-response.d.ts +9 -0
- package/dist/conforming-response.d.ts.map +1 -0
- package/dist/conforming-response.js +62 -0
- package/dist/conforming-response.js.map +1 -0
- package/dist/create-rule-tester.d.ts +101 -0
- package/dist/create-rule-tester.d.ts.map +1 -0
- package/dist/create-rule-tester.js +50 -0
- package/dist/create-rule-tester.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/recorded.d.ts +42 -0
- package/dist/recorded.d.ts.map +1 -0
- package/dist/recorded.js +70 -0
- package/dist/recorded.js.map +1 -0
- package/dist/rule-test-execution.d.ts +88 -0
- package/dist/rule-test-execution.d.ts.map +1 -0
- package/dist/rule-test-execution.js +118 -0
- package/dist/rule-test-execution.js.map +1 -0
- package/dist/rule-under-test.d.ts +21 -0
- package/dist/rule-under-test.d.ts.map +1 -0
- package/dist/rule-under-test.js +24 -0
- package/dist/rule-under-test.js.map +1 -0
- package/dist/run-analyze.d.ts +18 -0
- package/dist/run-analyze.d.ts.map +1 -0
- package/dist/run-analyze.js +175 -0
- package/dist/run-analyze.js.map +1 -0
- package/dist/run-lint.d.ts +10 -0
- package/dist/run-lint.d.ts.map +1 -0
- package/dist/run-lint.js +37 -0
- package/dist/run-lint.js.map +1 -0
- package/dist/run-test.d.ts +25 -0
- package/dist/run-test.d.ts.map +1 -0
- package/dist/run-test.js +173 -0
- package/dist/run-test.js.map +1 -0
- package/dist/stated-applicability.d.ts +46 -0
- package/dist/stated-applicability.d.ts.map +1 -0
- package/dist/stated-applicability.js +66 -0
- package/dist/stated-applicability.js.map +1 -0
- package/package.json +50 -4
package/README.md
CHANGED
|
@@ -1,3 +1,226 @@
|
|
|
1
|
-
|
|
1
|
+
<!-- HEADER:START - Do not remove or modify this section -->
|
|
2
|
+
<div align="center">
|
|
3
|
+
<img src="https://raw.githubusercontent.com/thymianofficial/thymian/main/astro-docs/src/assets/logo.svg" alt="Thymian Logo" width="200"/>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
# Thymian
|
|
6
|
+
|
|
7
|
+
**Add resilience and HTTP conformance to your API development workflow**
|
|
8
|
+
|
|
9
|
+
[](https://github.com/thymianofficial/thymian/blob/main/LICENSE)
|
|
10
|
+
[](https://github.com/thymianofficial/thymian/actions/workflows/ci.yaml)
|
|
11
|
+
[](https://thymian.dev)
|
|
12
|
+
[](https://discord.gg/TRSwCxbz9f)
|
|
13
|
+
[](https://www.reddit.com/r/ThymianOfficial/)
|
|
14
|
+
[](https://x.com/thymiandev)
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
<!-- HEADER:END -->
|
|
18
|
+
|
|
19
|
+
## @thymian/rules-testing
|
|
20
|
+
|
|
21
|
+
Test a rule through the real engine of a validation context it declares. You build the API the rule is tested against as a Thymian format with the factories of `@thymian/core-testing`, hand it to `createRuleTester`, and get back plain data: one execution per transaction of the format. No test framework is required or assumed.
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install --save-dev @thymian/rules-testing @thymian/core-testing
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { addTransaction, createBadRequestResponse, createCreatedResponse, createGetRequest, createOkResponse, createPostRequest, createRequiredParameter, createThymianFormat } from '@thymian/core-testing';
|
|
33
|
+
import { createRuleTester } from '@thymian/rules-testing';
|
|
34
|
+
|
|
35
|
+
import createdResponsesHaveLocation from './created-responses-have-location.rule.js';
|
|
36
|
+
|
|
37
|
+
const format = createThymianFormat();
|
|
38
|
+
const launchesListed = addTransaction(format, createGetRequest({ path: '/launches' }), createOkResponse());
|
|
39
|
+
const launchCreated = addTransaction(format, createPostRequest({ path: '/launches' }), createCreatedResponse({ headers: { location: createRequiredParameter() } }));
|
|
40
|
+
// One operation with two answers is two transactions on the same request.
|
|
41
|
+
const createAstronaut = createPostRequest({ path: '/astronauts' });
|
|
42
|
+
const astronautCreated = addTransaction(format, createAstronaut, createCreatedResponse());
|
|
43
|
+
const astronautRejected = addTransaction(format, createAstronaut, createBadRequestResponse());
|
|
44
|
+
|
|
45
|
+
const ruleTester = createRuleTester(createdResponsesHaveLocation);
|
|
46
|
+
const expected = [
|
|
47
|
+
{ location: { elementId: launchesListed }, status: 'notApplicable' },
|
|
48
|
+
{ location: { elementId: launchCreated }, status: 'passed' },
|
|
49
|
+
{
|
|
50
|
+
location: { elementId: astronautCreated },
|
|
51
|
+
status: 'failed',
|
|
52
|
+
severity: 'warn',
|
|
53
|
+
message: 'A 201 Created response must name the new resource in a Location header',
|
|
54
|
+
},
|
|
55
|
+
{ location: { elementId: astronautRejected }, status: 'notApplicable' },
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
expect(await ruleTester.runLint(format)).toMatchObject(expected);
|
|
59
|
+
expect(await ruleTester.runTest(format)).toMatchObject(expected);
|
|
60
|
+
expect(await ruleTester.runAnalyze(format)).toMatchObject(expected);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
`createRuleTester(rule)` gives one method per validation context: `runLint` evaluates the rule in `static`, with the context `thymian lint` uses, `runTest` in `test`, with the one `thymian test` uses, `runAnalyze` in `analytics`, with the one `thymian analyze` uses. Each returns a list of the same shape in the same order. Calling the method of a context the rule does not declare throws.
|
|
64
|
+
|
|
65
|
+
### The server in `test`
|
|
66
|
+
|
|
67
|
+
`test` opens no port and sends nothing over the network. The tester builds each request from the format, with the examples the format carries, and the request is answered in-process.
|
|
68
|
+
|
|
69
|
+
**Without your saying anything, the server answers as the format declares:** the conforming response of a transaction has the declared status, every declared header (required or not) with a value, the declared media type as `content-type`, and a body where the response declares a schema. A value is the first example of its schema, else its `const`, first `enum` member or `default`, else the plainest value of its type. So a format gives the same result in `test` as in `static`.
|
|
70
|
+
|
|
71
|
+
**A server that deviates** is a function. It is given the id of the transaction the request is aimed at, the conforming response and the request that was sent, and returns the response:
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
await ruleTester.runTest(format, {
|
|
75
|
+
respond: ({ transaction, conforming }) => (transaction === launchCreated ? { ...conforming, headers: {} } : conforming),
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Here `launchCreated` is `failed`: the server dropped the `Location` header the format declares. One condition can change many answers, for example `conforming.statusCode >= 400`. One operation with two answers gets two requests, one aimed at each transaction.
|
|
80
|
+
|
|
81
|
+
Two things make a transaction `skipped` in `test`, each with a `reason`, because the tester does not ask the rule about it:
|
|
82
|
+
|
|
83
|
+
- **The server answers another status than the transaction declares.** `thymian test` skips such a transaction too.
|
|
84
|
+
- **The request cannot be built**, for example for a path parameter without an example: give its schema one, `createRequiredParameter({ schema: createStringSchema({ examples: ['apollo-11'] }) })`.
|
|
85
|
+
|
|
86
|
+
### The traffic in `analytics`
|
|
87
|
+
|
|
88
|
+
`analytics` reads no recording from disk: the analyzer is handed traffic held in memory, with the format present, as `thymian analyze` hands it a HAR file together with the specification.
|
|
89
|
+
|
|
90
|
+
**The traffic is every transaction of the format recorded once, conforming:** the request the tester would send for it, with the examples the format carries and the media type the format declares for it as `content-type`, answered with the conforming response described above. So a rule that judges a transaction by what the format declares gives the same executions in `analytics` as in `static` and `test`.
|
|
91
|
+
|
|
92
|
+
Each pair is recorded the way traffic read from a HAR file is by default, as a user agent talking to an origin server, so a rule that speaks about one participant (`.appliesTo('server')`) is asked about it.
|
|
93
|
+
|
|
94
|
+
The analyzer locates an execution on the transaction of the format the recorded pair matches, so you name a transaction by the same id as in the other contexts. A rule that reads captured transactions or whole traces locates an execution on the label it is given: it keeps that location and is listed in the place of the transaction the recorded pair matched (see **Order** below). An execution the rule locates on any other string keeps that location and comes last.
|
|
95
|
+
|
|
96
|
+
The analyzer recognises a transaction in a recording by method, origin, path, status and media types, as it does for real traffic. Where that does not single out the transaction you added, `analytics` differs from `static` and `test`: two answers of one request with the same status and media type match neither, so both are `notApplicable` and an execution on their recording keeps its label; and a recording of `/launches/{id}` with the example `latest` is taken for a transaction on the literal path `/launches/latest` when the format has one. And the label of a recording names the method, the URL, the status and the response's media type, but not the request's: where recordings of two transactions share one, an execution on it comes after the transactions, and both transactions are listed as if the rule had said nothing about them.
|
|
97
|
+
|
|
98
|
+
A request that cannot be built, for example for a path parameter without an example, makes `analytics` throw and name the transaction: there is nothing to record for it. Give the parameter's schema an example, as for `test`.
|
|
99
|
+
|
|
100
|
+
**Traffic you hand in** replaces it. `traffic` is a list of recorded pairs, each a request and its response as plain HTTP objects. `recorded(format, transactionId, overrides?)` gives the conforming pair of a transaction, so you write only what deviates:
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { createRuleTester, recorded } from '@thymian/rules-testing';
|
|
104
|
+
|
|
105
|
+
const executions = await ruleTester.runAnalyze(format, {
|
|
106
|
+
traffic: [recorded(format, launchCreated), recorded(format, launchCreated, { response: { headers: {} } })],
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
expect(executions).toMatchObject([
|
|
110
|
+
{ location: { elementId: launchCreated }, status: 'passed' },
|
|
111
|
+
{ location: { elementId: launchCreated }, status: 'failed' },
|
|
112
|
+
{ location: { elementId: launchesListed }, status: 'notApplicable' },
|
|
113
|
+
// ...every other transaction of the format, none of them recorded
|
|
114
|
+
]);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
A transaction may be recorded several times, and each recording is judged on its own: here the second one dropped the `Location` header the format declares. The list is then in another order than for `static`: one entry per recording, in traffic order; then the transactions that were never recorded, as `notApplicable`; then executions on any other location.
|
|
118
|
+
|
|
119
|
+
An override replaces a field of the conforming request or response as a whole: `{ request: { headers: { authorization: 'Bearer apollo' } } }` records exactly that request header, which is how a rule about a request header is tested. One thing is kept: the media type the format declares is still named as `content-type`, because the analyzer recognises the transaction by it, unless the headers you give name a `content-type` themselves. `recorded` throws for an id the format has no transaction with, and for a request that cannot be built.
|
|
120
|
+
|
|
121
|
+
A recording that the analyzer matches to no transaction of the format, for example one with another path or status, has no transaction to appear on. What the rule says about it is located on a free string, the label of the recording, and comes last:
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
const executions = await ruleTester.runAnalyze(format, {
|
|
125
|
+
traffic: [recorded(format, launchCreated, { request: { path: '/probes' }, response: { headers: {} } })],
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
expect(executions.at(-1)).toMatchObject({ location: expect.stringContaining('/probes'), status: 'failed' });
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
The analyzer names the transaction an execution is about, not the recording. For a transaction recorded several times, `createRuleTester` therefore also runs the rule on each of its recordings alone to learn which one an execution belongs to. A rule that compares recordings with each other says something else about a recording that stands alone; what such a rule reports on the transaction then appears on every recording of it.
|
|
132
|
+
|
|
133
|
+
### Any rule runs
|
|
134
|
+
|
|
135
|
+
**A rule that ships with severity `off`** runs like any other, so a rule that is only switched on by a config or a profile is testable as it ships. A `failed` execution carries the severity the rule declares, here `off`.
|
|
136
|
+
|
|
137
|
+
**A rule with options** gets them from `createRuleTester(rule, { options })`, handed to the rule function as the `options` of the rule's entry in a config are. One call is one configuration:
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
const format = createThymianFormat();
|
|
141
|
+
const launchCreated = addTransaction(format, createPostRequest({ path: '/launches' }), createCreatedResponse({ headers: { location: createRequiredParameter() } }));
|
|
142
|
+
|
|
143
|
+
expect(await createRuleTester(createdResponsesHaveHeader, { options: { header: 'location' } }).runLint(format)).toMatchObject([{ location: { elementId: launchCreated }, status: 'passed' }]);
|
|
144
|
+
expect(await createRuleTester(createdResponsesHaveHeader, { options: { header: 'etag' } }).runLint(format)).toMatchObject([{ location: { elementId: launchCreated }, status: 'failed' }]);
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Options the rule's options schema does not accept make `createRuleTester` throw, as a config with them is rejected when it is loaded.
|
|
148
|
+
|
|
149
|
+
### Grouped rules
|
|
150
|
+
|
|
151
|
+
A rule that compares several transactions with each other (`validateGroupedCommonHttpTransactions`) is handed each group with its key and its members, and says who is at fault by the location it returns. Both readings come back per transaction, in all three contexts:
|
|
152
|
+
|
|
153
|
+
- **It returns the location of one member:** that transaction is `failed`, and the other members of the group are `passed`.
|
|
154
|
+
- **It returns the group key:** every member of the group is `failed`, and each of these executions carries the key as `group`.
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
const launchRead = addTransaction(format, createGetRequest({ path: '/launches' }), createOkResponse({ headers: { etag: createRequiredParameter() } }));
|
|
158
|
+
const launchReplaced = addTransaction(format, createPutRequest({ path: '/launches' }), createOkResponse());
|
|
159
|
+
|
|
160
|
+
// A rule that groups by `url()` and returns the key of a group whose answers disagree about ETag
|
|
161
|
+
expect(await createRuleTester(resourcesSendEtagConsistently).runLint(format)).toMatchObject([
|
|
162
|
+
{ location: { elementId: launchRead }, status: 'failed', group: expect.stringContaining('/launches') },
|
|
163
|
+
{ location: { elementId: launchReplaced }, status: 'failed', group: expect.stringContaining('/launches') },
|
|
164
|
+
]);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
The key is what the context grouped by, spelled as that context spells it: grouped by `url()`, `static` and `test` give `https://localhost:443/launches` where `analytics` gives `https://localhost/launches`. In `test` the tester sends the requests of a group together, so a server that answers one member with another status than declared makes every member of the group `skipped`.
|
|
168
|
+
|
|
169
|
+
### What comes back
|
|
170
|
+
|
|
171
|
+
A list of executions. Each one has:
|
|
172
|
+
|
|
173
|
+
| Field | Meaning |
|
|
174
|
+
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
175
|
+
| `location` | Where the engine located it. For a transaction: `{ elementType: 'edge', elementId: <transaction id>, label }`. A node location or a free string is passed through. |
|
|
176
|
+
| `status` | `failed`, `passed`, `notApplicable` or `skipped`. |
|
|
177
|
+
| `message` | When `failed`: the violation's message, or the rule's summary or description when it gives none. |
|
|
178
|
+
| `severity` | When `failed`: the severity the rule declares. |
|
|
179
|
+
| `reason` | When `skipped`: the reason the rule gave. |
|
|
180
|
+
| `group` | When a grouped rule located the execution on a group and not on one member: the group key. See [Grouped rules](#grouped-rules). |
|
|
181
|
+
| `findings` | What the rule attached to the execution, as a report carries it. Always a list; empty when the rule attached nothing. |
|
|
182
|
+
|
|
183
|
+
The `label` is the readable name of the location, so a failing comparison says which transaction it is about although ids are hashes. A label the rule set itself is kept.
|
|
184
|
+
|
|
185
|
+
**Order.** Every transaction of the format, in the order it was added. A transaction the engine reported several executions on appears once per execution, in the order the engine reported them. In `analytics` an execution on the label of a recorded pair counts as reported on the transaction the pair matched: it takes that transaction's place and keeps the label as its `location`. The analyzer locates there what a rule returns from `validateCapturedHttpTransactions` and `validateCapturedHttpTraces`. Executions on any other location come last. With traffic handed to `analytics` the recordings come first, in traffic order, as described above.
|
|
186
|
+
|
|
187
|
+
**Where `passed` and `notApplicable` come from.** In `test` the tester reports an execution for every transaction it sent a request for, `passed` and `skipped` included; `createRuleTester` takes them as reported and adds only `notApplicable`, for a transaction no request was sent for. The linter and the analyzer report only what a rule returns, which for most rules is its violations. In `static` and `analytics`, `createRuleTester` works out the rest from the `appliesTo` the rule states in its rule-context calls: a transaction inside it that the engine reported nothing on is `passed`, one outside it is `notApplicable`. Several calls count together; a transaction is applicable when any of them accepts it. `static` asks the `appliesTo` of the format, `analytics` of the recorded pair, so two recordings of one transaction can differ in it.
|
|
188
|
+
|
|
189
|
+
A rule whose applicability cannot be read from outside gets `passed` for every transaction it did not flag. That is a rule that passes only a violation condition or makes no rule-context call at all, and:
|
|
190
|
+
|
|
191
|
+
- in `static`, a rule that decides in a plain callback (`validateHttpTransactions`);
|
|
192
|
+
- in `analytics`, a rule that reads whole traces (`validateCapturedHttpTraces`). `validateHttpTransactions` takes a filter expression there, which `createRuleTester` reads like any other.
|
|
193
|
+
|
|
194
|
+
**Skips and findings.** A rule that returns a `rule-skip` finding for a location yields a `skipped` execution there, with the `reason` it gave. Any other finding comes back in `findings`, on a `passed` execution as much as on a `failed` one, in the shape of a report finding: `{ id, kind, title, message: { text } }`. The `id` is generated per run, so compare findings with `toMatchObject` or leave it out.
|
|
195
|
+
|
|
196
|
+
`passed` and `notApplicable` are filled in for transactions only, for a transaction no execution is about. A node or a free string appears only when the engine reports an execution on it.
|
|
197
|
+
|
|
198
|
+
<!-- FOOTER:START - Do not remove or modify this section -->
|
|
199
|
+
|
|
200
|
+
## 📚 Documentation
|
|
201
|
+
|
|
202
|
+
- **[Getting Started](https://thymian.dev/introduction/getting-started)** - Set up Thymian in minutes
|
|
203
|
+
- **[Documentation](https://thymian.dev)** - Comprehensive guides and API reference
|
|
204
|
+
- **[CLI Reference](https://thymian.dev/references/cli)** - Complete CLI command documentation
|
|
205
|
+
- **[HTTP Rules](https://thymian.dev/guides/http-rules/how-to-use-rules)** - Learn about HTTP conformance validation
|
|
206
|
+
|
|
207
|
+
## 🏢 Enterprise Support
|
|
208
|
+
|
|
209
|
+
Get professional consulting and dedicated support from the creators of Thymian. We offer:
|
|
210
|
+
|
|
211
|
+
- API design and governance strategies
|
|
212
|
+
- HTTP standards compliance auditing
|
|
213
|
+
- Custom plugin development
|
|
214
|
+
- Custom rule development
|
|
215
|
+
- Team training and workshops
|
|
216
|
+
|
|
217
|
+
**[Learn more about Enterprise Support](https://thymian.dev/enterprise)** | **Email: [support@thymian.dev](mailto:support@thymian.dev)**
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
<div align="center">
|
|
222
|
+
|
|
223
|
+
**Shipped with 🌱 by [qupaya](https://qupaya.com)**
|
|
224
|
+
|
|
225
|
+
</div>
|
|
226
|
+
<!-- FOOTER:END -->
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type HttpResponse, type ThymianHttpResponse } from '@thymian/core';
|
|
2
|
+
/**
|
|
3
|
+
* The response that answers a transaction exactly as the format declares it:
|
|
4
|
+
* the declared status, every declared header (required or not) with a value,
|
|
5
|
+
* the declared media type as `content-type`, and a body where the format
|
|
6
|
+
* declares a schema for one.
|
|
7
|
+
*/
|
|
8
|
+
export declare function conformingResponse(declared: ThymianHttpResponse): HttpResponse;
|
|
9
|
+
//# sourceMappingURL=conforming-response.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conforming-response.d.ts","sourceRoot":"","sources":["../src/conforming-response.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EAGjB,KAAK,mBAAmB,EAEzB,MAAM,eAAe,CAAC;AAEvB;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,mBAAmB,GAC5B,YAAY,CA4Bd"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { serializeBody, serializeHeaderParameter, } from '@thymian/core';
|
|
2
|
+
/**
|
|
3
|
+
* The response that answers a transaction exactly as the format declares it:
|
|
4
|
+
* the declared status, every declared header (required or not) with a value,
|
|
5
|
+
* the declared media type as `content-type`, and a body where the format
|
|
6
|
+
* declares a schema for one.
|
|
7
|
+
*/
|
|
8
|
+
export function conformingResponse(declared) {
|
|
9
|
+
const headers = {};
|
|
10
|
+
for (const [name, { schema, style }] of Object.entries(declared.headers)) {
|
|
11
|
+
const value = valueOf(schema);
|
|
12
|
+
headers[name.toLowerCase()] =
|
|
13
|
+
typeof value === 'string'
|
|
14
|
+
? value
|
|
15
|
+
: serializeHeaderParameter(name, value, style);
|
|
16
|
+
}
|
|
17
|
+
if (declared.mediaType !== '') {
|
|
18
|
+
headers['content-type'] = declared.mediaType;
|
|
19
|
+
}
|
|
20
|
+
const body = declared.schema === undefined
|
|
21
|
+
? undefined
|
|
22
|
+
: serializeBody(valueOf(declared.schema));
|
|
23
|
+
return {
|
|
24
|
+
statusCode: declared.statusCode,
|
|
25
|
+
headers,
|
|
26
|
+
...(body === undefined ? {} : { body }),
|
|
27
|
+
trailers: {},
|
|
28
|
+
duration: 0,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A value of the schema: the one the schema names (its first example, its
|
|
33
|
+
* constant, its first enum member or its default), else the plainest value of
|
|
34
|
+
* its type.
|
|
35
|
+
*/
|
|
36
|
+
function valueOf(schema) {
|
|
37
|
+
const named = [
|
|
38
|
+
schema.examples?.[0],
|
|
39
|
+
schema.const,
|
|
40
|
+
schema.enum?.[0],
|
|
41
|
+
schema.default,
|
|
42
|
+
].find((value) => value !== undefined);
|
|
43
|
+
if (named !== undefined) {
|
|
44
|
+
return named;
|
|
45
|
+
}
|
|
46
|
+
switch (Array.isArray(schema.type) ? schema.type[0] : schema.type) {
|
|
47
|
+
case 'object':
|
|
48
|
+
return {};
|
|
49
|
+
case 'array':
|
|
50
|
+
return [];
|
|
51
|
+
case 'number':
|
|
52
|
+
case 'integer':
|
|
53
|
+
return 0;
|
|
54
|
+
case 'boolean':
|
|
55
|
+
return true;
|
|
56
|
+
case 'null':
|
|
57
|
+
return null;
|
|
58
|
+
default:
|
|
59
|
+
return 'string';
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=conforming-response.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conforming-response.js","sourceRoot":"","sources":["../src/conforming-response.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,aAAa,EACb,wBAAwB,GAGzB,MAAM,eAAe,CAAC;AAEvB;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAChC,QAA6B;IAE7B,MAAM,OAAO,GAA4B,EAAE,CAAC;IAE5C,KAAK,MAAM,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QACzE,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;QAE9B,OAAO,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;YACzB,OAAO,KAAK,KAAK,QAAQ;gBACvB,CAAC,CAAC,KAAK;gBACP,CAAC,CAAC,wBAAwB,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IACrD,CAAC;IAED,IAAI,QAAQ,CAAC,SAAS,KAAK,EAAE,EAAE,CAAC;QAC9B,OAAO,CAAC,cAAc,CAAC,GAAG,QAAQ,CAAC,SAAS,CAAC;IAC/C,CAAC;IAED,MAAM,IAAI,GACR,QAAQ,CAAC,MAAM,KAAK,SAAS;QAC3B,CAAC,CAAC,SAAS;QACX,CAAC,CAAC,aAAa,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;IAE9C,OAAO;QACL,UAAU,EAAE,QAAQ,CAAC,UAAU;QAC/B,OAAO;QACP,GAAG,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC;QACvC,QAAQ,EAAE,EAAE;QACZ,QAAQ,EAAE,CAAC;KACZ,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,SAAS,OAAO,CAAC,MAAqB;IACpC,MAAM,KAAK,GAAG;QACZ,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC;QACpB,MAAM,CAAC,KAAK;QACZ,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;QAChB,MAAM,CAAC,OAAO;KACf,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC;IAEvC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,QAAQ,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;QAClE,KAAK,QAAQ;YACX,OAAO,EAAE,CAAC;QACZ,KAAK,OAAO;YACV,OAAO,EAAE,CAAC;QACZ,KAAK,QAAQ,CAAC;QACd,KAAK,SAAS;YACZ,OAAO,CAAC,CAAC;QACX,KAAK,SAAS;YACZ,OAAO,IAAI,CAAC;QACd,KAAK,MAAM;YACT,OAAO,IAAI,CAAC;QACd;YACE,OAAO,QAAQ,CAAC;IACpB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { Rule, ThymianFormat } from '@thymian/core';
|
|
2
|
+
import type { RecordedPair } from './recorded.js';
|
|
3
|
+
import type { RuleTestExecution } from './rule-test-execution.js';
|
|
4
|
+
import { type Respond } from './run-test.js';
|
|
5
|
+
/**
|
|
6
|
+
* One method per validation context a rule can declare.
|
|
7
|
+
*/
|
|
8
|
+
export type RuleTester = {
|
|
9
|
+
/**
|
|
10
|
+
* Runs the rule as `thymian lint` does and returns one execution per
|
|
11
|
+
* transaction of the format, in the order the transactions were added,
|
|
12
|
+
* followed by the executions the engine located anywhere else.
|
|
13
|
+
*
|
|
14
|
+
* Throws when the rule does not declare `static`.
|
|
15
|
+
*/
|
|
16
|
+
runLint(format: ThymianFormat): Promise<RuleTestExecution[]>;
|
|
17
|
+
/**
|
|
18
|
+
* Runs the rule as `thymian test` does, without a network, and returns the
|
|
19
|
+
* same list in the same order as `static`. Every request the tester builds
|
|
20
|
+
* is answered in-process: as the format declares it, or by `respond`.
|
|
21
|
+
*
|
|
22
|
+
* Throws when the rule does not declare `test`.
|
|
23
|
+
*/
|
|
24
|
+
runTest(format: ThymianFormat, configuration?: RunTestConfiguration): Promise<RuleTestExecution[]>;
|
|
25
|
+
/**
|
|
26
|
+
* Runs the rule as `thymian analyze` does, against traffic held in memory.
|
|
27
|
+
* Without `traffic` that is every transaction of the format recorded once,
|
|
28
|
+
* the request the format describes answered as the format declares it, and
|
|
29
|
+
* the list is the same, in the same order, as for `static`.
|
|
30
|
+
*
|
|
31
|
+
* With `traffic` the list is one entry per recording, in traffic order, on
|
|
32
|
+
* the transaction it matches; then the transactions that were never
|
|
33
|
+
* recorded, as `notApplicable`; then the executions the engine located
|
|
34
|
+
* anywhere else.
|
|
35
|
+
*
|
|
36
|
+
* Throws when the rule does not declare `analytics`, and without `traffic`
|
|
37
|
+
* when the request of a transaction cannot be built from the format.
|
|
38
|
+
*/
|
|
39
|
+
runAnalyze(format: ThymianFormat, configuration?: RunAnalyzeConfiguration): Promise<RuleTestExecution[]>;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* The traffic a rule is tested against in `analytics`.
|
|
43
|
+
*/
|
|
44
|
+
export type RunAnalyzeConfiguration = {
|
|
45
|
+
/**
|
|
46
|
+
* The recorded pairs the analyzer reads, in place of every transaction
|
|
47
|
+
* recorded once. `recorded(format, transactionId, overrides?)` gives the
|
|
48
|
+
* conforming pair of a transaction. A transaction may be recorded several
|
|
49
|
+
* times, and each recording is judged on its own.
|
|
50
|
+
*/
|
|
51
|
+
traffic?: RecordedPair[];
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* The server a rule is tested against in `test`.
|
|
55
|
+
*/
|
|
56
|
+
export type RunTestConfiguration = {
|
|
57
|
+
/**
|
|
58
|
+
* Decides what the server answers to a request. It is given the id of the
|
|
59
|
+
* transaction the request is aimed at, the conforming response of that
|
|
60
|
+
* transaction and the request that was sent. Without it the server answers
|
|
61
|
+
* every request with the conforming response.
|
|
62
|
+
*
|
|
63
|
+
* An answer with another status than the transaction declares makes the
|
|
64
|
+
* transaction `skipped`: the tester does not ask the rule about it.
|
|
65
|
+
*/
|
|
66
|
+
respond?: Respond;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* How the rule is configured for the run.
|
|
70
|
+
*/
|
|
71
|
+
export type RuleTesterConfiguration<Options extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> = {
|
|
72
|
+
/**
|
|
73
|
+
* The rule's options, handed to the rule function as the `options` of the
|
|
74
|
+
* rule's entry in a config are. Call `createRuleTester` once per configuration to
|
|
75
|
+
* test. `createRuleTester` throws when the rule's options schema does not accept
|
|
76
|
+
* them.
|
|
77
|
+
*/
|
|
78
|
+
options?: Options;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Tests a rule through the real engine of a validation context it declares.
|
|
82
|
+
*
|
|
83
|
+
* The rule runs whatever severity it ships with, `off` included, and a
|
|
84
|
+
* `failed` execution carries the severity the rule declares.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const format = createThymianFormat();
|
|
89
|
+
* const launchCreated = addTransaction(
|
|
90
|
+
* format,
|
|
91
|
+
* createPostRequest({ path: '/launches' }),
|
|
92
|
+
* createCreatedResponse(),
|
|
93
|
+
* );
|
|
94
|
+
*
|
|
95
|
+
* expect(await createRuleTester(createdResponsesHaveLocation).runLint(format)).toMatchObject([
|
|
96
|
+
* { location: { elementId: launchCreated }, status: 'failed' },
|
|
97
|
+
* ]);
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export declare function createRuleTester<Options extends Record<PropertyKey, unknown>>(rule: Rule<Options>, { options }?: RuleTesterConfiguration<Options>): RuleTester;
|
|
101
|
+
//# sourceMappingURL=create-rule-tester.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-rule-tester.d.ts","sourceRoot":"","sources":["../src/create-rule-tester.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAEzD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAIlE,OAAO,EAAE,KAAK,OAAO,EAAW,MAAM,eAAe,CAAC;AAEtD;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB;;;;;;OAMG;IACH,OAAO,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAAC;IAC7D;;;;;;OAMG;IACH,OAAO,CACL,MAAM,EAAE,aAAa,EACrB,aAAa,CAAC,EAAE,oBAAoB,GACnC,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAAC;IAChC;;;;;;;;;;;;;OAaG;IACH,UAAU,CACR,MAAM,EAAE,aAAa,EACrB,aAAa,CAAC,EAAE,uBAAuB,GACtC,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAAC;CACjC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG;IACpC;;;;;OAKG;IACH,OAAO,CAAC,EAAE,YAAY,EAAE,CAAC;CAC1B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC;;;;;;;;OAQG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,uBAAuB,CACjC,OAAO,SAAS,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,IACzE;IACF;;;;;OAKG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,SAAS,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,EAC3E,IAAI,EAAE,IAAI,CAAC,OAAO,CAAC,EACnB,EAAE,OAAO,EAAE,GAAE,uBAAuB,CAAC,OAAO,CAAM,GACjD,UAAU,CAoBZ"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ruleUnderTest } from './rule-under-test.js';
|
|
2
|
+
import { runAnalyze } from './run-analyze.js';
|
|
3
|
+
import { runLint } from './run-lint.js';
|
|
4
|
+
import { runTest } from './run-test.js';
|
|
5
|
+
/**
|
|
6
|
+
* Tests a rule through the real engine of a validation context it declares.
|
|
7
|
+
*
|
|
8
|
+
* The rule runs whatever severity it ships with, `off` included, and a
|
|
9
|
+
* `failed` execution carries the severity the rule declares.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const format = createThymianFormat();
|
|
14
|
+
* const launchCreated = addTransaction(
|
|
15
|
+
* format,
|
|
16
|
+
* createPostRequest({ path: '/launches' }),
|
|
17
|
+
* createCreatedResponse(),
|
|
18
|
+
* );
|
|
19
|
+
*
|
|
20
|
+
* expect(await createRuleTester(createdResponsesHaveLocation).runLint(format)).toMatchObject([
|
|
21
|
+
* { location: { elementId: launchCreated }, status: 'failed' },
|
|
22
|
+
* ]);
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function createRuleTester(rule, { options } = {}) {
|
|
26
|
+
// A rule's options type is fixed by its own schema; the engines take rules
|
|
27
|
+
// of any options type the same way.
|
|
28
|
+
const anyRule = rule;
|
|
29
|
+
const underTest = ruleUnderTest(anyRule, options);
|
|
30
|
+
return {
|
|
31
|
+
async runLint(format) {
|
|
32
|
+
assertDeclares(anyRule, 'runLint', 'static');
|
|
33
|
+
return runLint(underTest, format);
|
|
34
|
+
},
|
|
35
|
+
async runTest(format, { respond } = {}) {
|
|
36
|
+
assertDeclares(anyRule, 'runTest', 'test');
|
|
37
|
+
return runTest(underTest, format, respond);
|
|
38
|
+
},
|
|
39
|
+
async runAnalyze(format, { traffic } = {}) {
|
|
40
|
+
assertDeclares(anyRule, 'runAnalyze', 'analytics');
|
|
41
|
+
return runAnalyze(underTest, format, traffic);
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function assertDeclares(rule, method, context) {
|
|
46
|
+
if (!rule.meta.type.includes(context)) {
|
|
47
|
+
throw new Error(`${method} runs a rule in "${context}", and rule "${rule.meta.name}" does not declare it. It declares: ${rule.meta.type.join(', ')}.`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=create-rule-tester.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-rule-tester.js","sourceRoot":"","sources":["../src/create-rule-tester.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAgB,OAAO,EAAE,MAAM,eAAe,CAAC;AAyFtD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,gBAAgB,CAC9B,IAAmB,EACnB,EAAE,OAAO,KAAuC,EAAE;IAElD,2EAA2E;IAC3E,oCAAoC;IACpC,MAAM,OAAO,GAAG,IAAuB,CAAC;IACxC,MAAM,SAAS,GAAG,aAAa,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAElD,OAAO;QACL,KAAK,CAAC,OAAO,CAAC,MAAM;YAClB,cAAc,CAAC,OAAO,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAC;YAC7C,OAAO,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QACpC,CAAC;QACD,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE;YACpC,cAAc,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;YAC3C,OAAO,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QAC7C,CAAC;QACD,KAAK,CAAC,UAAU,CAAC,MAAM,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE;YACvC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,WAAW,CAAC,CAAC;YACnD,OAAO,UAAU,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QAChD,CAAC;KACF,CAAC;AACJ,CAAC;AAED,SAAS,cAAc,CACrB,IAAU,EACV,MAAwB,EACxB,OAAwC;IAExC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QACtC,MAAM,IAAI,KAAK,CACb,GAAG,MAAM,oBAAoB,OAAO,gBAAgB,IAAI,CAAC,IAAI,CAAC,IAAI,uCAAuC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CACtI,CAAC;IACJ,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createRuleTester, type RuleTester, type RuleTesterConfiguration, type RunAnalyzeConfiguration, type RunTestConfiguration, } from './create-rule-tester.js';
|
|
2
|
+
export { recorded, type RecordedOverrides, type RecordedPair, } from './recorded.js';
|
|
3
|
+
export type { RuleTestExecution, RuleTestExecutionStatus, } from './rule-test-execution.js';
|
|
4
|
+
export type { Respond, RespondInput } from './run-test.js';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,KAAK,UAAU,EACf,KAAK,uBAAuB,EAC5B,KAAK,uBAAuB,EAC5B,KAAK,oBAAoB,GAC1B,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,QAAQ,EACR,KAAK,iBAAiB,EACtB,KAAK,YAAY,GAClB,MAAM,eAAe,CAAC;AACvB,YAAY,EACV,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,0BAA0B,CAAC;AAClC,YAAY,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,GAKjB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,QAAQ,GAGT,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type HttpRequest, type HttpResponse, type ThymianFormat } from '@thymian/core';
|
|
2
|
+
/**
|
|
3
|
+
* One request and the response it got, as traffic a rule is tested against in
|
|
4
|
+
* `analytics`.
|
|
5
|
+
*/
|
|
6
|
+
export type RecordedPair = {
|
|
7
|
+
request: HttpRequest;
|
|
8
|
+
response: HttpResponse;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* What a recording differs in from the conforming pair. A field given here
|
|
12
|
+
* replaces the conforming one as a whole: `{ response: { headers: {} } }`
|
|
13
|
+
* records a response without the headers the format declares.
|
|
14
|
+
*
|
|
15
|
+
* The media type the format declares is still named as `content-type`, since
|
|
16
|
+
* the analyzer recognises the transaction in a recording by it among other
|
|
17
|
+
* things. Headers that name a `content-type` themselves record that one.
|
|
18
|
+
*/
|
|
19
|
+
export type RecordedOverrides = {
|
|
20
|
+
request?: Partial<HttpRequest>;
|
|
21
|
+
response?: Partial<HttpResponse>;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The conforming recording of a transaction: the request the format describes,
|
|
25
|
+
* with the examples it carries, answered as the format declares it. `overrides`
|
|
26
|
+
* replace fields of the request and of the response.
|
|
27
|
+
*
|
|
28
|
+
* Throws when the format has no transaction with that id, and when the request
|
|
29
|
+
* cannot be built from the format.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* await createRuleTester(createdResponsesHaveLocation).runAnalyze(format, {
|
|
34
|
+
* traffic: [
|
|
35
|
+
* recorded(format, launchCreated),
|
|
36
|
+
* recorded(format, launchCreated, { response: { headers: {} } }),
|
|
37
|
+
* ],
|
|
38
|
+
* });
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function recorded(format: ThymianFormat, transactionId: string, overrides?: RecordedOverrides): RecordedPair;
|
|
42
|
+
//# sourceMappingURL=recorded.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recorded.d.ts","sourceRoot":"","sources":["../src/recorded.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,YAAY,EAEjB,KAAK,aAAa,EAGnB,MAAM,eAAe,CAAC;AAKvB;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,WAAW,CAAC;IACrB,QAAQ,EAAE,YAAY,CAAC;CACxB,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,OAAO,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IAC/B,QAAQ,CAAC,EAAE,OAAO,CAAC,YAAY,CAAC,CAAC;CAClC,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,QAAQ,CACtB,MAAM,EAAE,aAAa,EACrB,aAAa,EAAE,MAAM,EACrB,SAAS,GAAE,iBAAsB,GAChC,YAAY,CA0Bd"}
|
package/dist/recorded.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { serializeRequest, thymianHttpTransactionToString, } from '@thymian/core';
|
|
2
|
+
import { exampleRequestTemplate } from '@thymian/core-testing';
|
|
3
|
+
import { conformingResponse } from './conforming-response.js';
|
|
4
|
+
/**
|
|
5
|
+
* The conforming recording of a transaction: the request the format describes,
|
|
6
|
+
* with the examples it carries, answered as the format declares it. `overrides`
|
|
7
|
+
* replace fields of the request and of the response.
|
|
8
|
+
*
|
|
9
|
+
* Throws when the format has no transaction with that id, and when the request
|
|
10
|
+
* cannot be built from the format.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* await createRuleTester(createdResponsesHaveLocation).runAnalyze(format, {
|
|
15
|
+
* traffic: [
|
|
16
|
+
* recorded(format, launchCreated),
|
|
17
|
+
* recorded(format, launchCreated, { response: { headers: {} } }),
|
|
18
|
+
* ],
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function recorded(format, transactionId, overrides = {}) {
|
|
23
|
+
const transaction = format.getThymianHttpTransactionById(transactionId);
|
|
24
|
+
if (transaction === undefined) {
|
|
25
|
+
throw new Error(`Cannot record "${transactionId}": the format has no transaction with that id.`);
|
|
26
|
+
}
|
|
27
|
+
const { thymianReq, thymianRes } = transaction;
|
|
28
|
+
const request = { ...conformingRequest(transaction), ...overrides.request };
|
|
29
|
+
const response = {
|
|
30
|
+
...conformingResponse(thymianRes),
|
|
31
|
+
...overrides.response,
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
request: {
|
|
35
|
+
...request,
|
|
36
|
+
headers: namingMediaType(request.headers, thymianReq.mediaType),
|
|
37
|
+
},
|
|
38
|
+
response: {
|
|
39
|
+
...response,
|
|
40
|
+
headers: namingMediaType(response.headers, thymianRes.mediaType),
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The request the tester would send for the transaction.
|
|
46
|
+
*/
|
|
47
|
+
function conformingRequest(transaction) {
|
|
48
|
+
const { thymianReq, thymianRes } = transaction;
|
|
49
|
+
try {
|
|
50
|
+
return serializeRequest({
|
|
51
|
+
requestTemplate: exampleRequestTemplate(transaction),
|
|
52
|
+
source: transaction,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
throw new Error(`Cannot record "${thymianHttpTransactionToString(thymianReq, thymianRes)}": ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The headers, naming the media type the format declares as `content-type`
|
|
61
|
+
* unless they name one themselves: without it the analyzer does not recognise
|
|
62
|
+
* the transaction in the recording.
|
|
63
|
+
*/
|
|
64
|
+
function namingMediaType(headers = {}, mediaType) {
|
|
65
|
+
const named = Object.keys(headers).some((name) => name.toLowerCase() === 'content-type');
|
|
66
|
+
return mediaType === '' || named
|
|
67
|
+
? headers
|
|
68
|
+
: { 'content-type': mediaType, ...headers };
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=recorded.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recorded.js","sourceRoot":"","sources":["../src/recorded.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,gBAAgB,EAGhB,8BAA8B,GAC/B,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAE/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAyB9D;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,QAAQ,CACtB,MAAqB,EACrB,aAAqB,EACrB,YAA+B,EAAE;IAEjC,MAAM,WAAW,GAAG,MAAM,CAAC,6BAA6B,CAAC,aAAa,CAAC,CAAC;IAExE,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CACb,kBAAkB,aAAa,gDAAgD,CAChF,CAAC;IACJ,CAAC;IAED,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,WAAW,CAAC;IAC/C,MAAM,OAAO,GAAG,EAAE,GAAG,iBAAiB,CAAC,WAAW,CAAC,EAAE,GAAG,SAAS,CAAC,OAAO,EAAE,CAAC;IAC5E,MAAM,QAAQ,GAAG;QACf,GAAG,kBAAkB,CAAC,UAAU,CAAC;QACjC,GAAG,SAAS,CAAC,QAAQ;KACtB,CAAC;IAEF,OAAO;QACL,OAAO,EAAE;YACP,GAAG,OAAO;YACV,OAAO,EAAE,eAAe,CAAC,OAAO,CAAC,OAAO,EAAE,UAAU,CAAC,SAAS,CAAC;SAChE;QACD,QAAQ,EAAE;YACR,GAAG,QAAQ;YACX,OAAO,EAAE,eAAe,CAAC,QAAQ,CAAC,OAAO,EAAE,UAAU,CAAC,SAAS,CAAC;SACjE;KACF,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAS,iBAAiB,CAAC,WAAmC;IAC5D,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,WAAW,CAAC;IAE/C,IAAI,CAAC;QACH,OAAO,gBAAgB,CAAC;YACtB,eAAe,EAAE,sBAAsB,CAAC,WAAW,CAAC;YACpD,MAAM,EAAE,WAAW;SACpB,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CACb,kBAAkB,8BAA8B,CAAC,UAAU,EAAE,UAAU,CAAC,MAAM,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EACtI,EAAE,KAAK,EAAE,KAAK,EAAE,CACjB,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,SAAS,eAAe,CACtB,UAAmC,EAAE,EACrC,SAAiB;IAEjB,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,IAAI,CACrC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,cAAc,CAChD,CAAC;IAEF,OAAO,SAAS,KAAK,EAAE,IAAI,KAAK;QAC9B,CAAC,CAAC,OAAO;QACT,CAAC,CAAC,EAAE,cAAc,EAAE,SAAS,EAAE,GAAG,OAAO,EAAE,CAAC;AAChD,CAAC"}
|