@toa.io/extensions.exposition 0.24.0-alpha.2 → 0.24.0-alpha.4

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.
@@ -1,119 +1,46 @@
1
- import { AssertionError } from 'node:assert'
2
1
  import { binding, then, when } from 'cucumber-tsflow'
3
2
  import * as http from '@toa.io/http'
4
- import { trim } from '@toa.io/generic'
5
- import { buffer } from '@toa.io/streams'
6
3
  import { open } from '../../../storages/source/test/util'
7
4
  import { Parameters } from './Parameters'
8
5
  import { Gateway } from './Gateway'
9
6
 
10
7
  @binding([Gateway, Parameters])
11
- export class HTTP {
8
+ export class HTTP extends http.Agent {
12
9
  private readonly gateway: Gateway
13
- private readonly origin: string
14
- private response: string = ''
15
- private readonly variables: Record<string, string> = {}
16
10
 
17
11
  public constructor (gateway: Gateway, parameters: Parameters) {
12
+ super(parameters.origin)
18
13
  this.gateway = gateway
19
- this.origin = parameters.origin
20
14
  }
21
15
 
22
16
  @when('the following request is received:')
23
- public async request (input: string): Promise<any> {
24
- let [headers, body] = trim(input).split('\n\n')
25
-
26
- if (body !== undefined)
27
- headers += '\ncontent-length: ' + body?.length
28
-
29
- const text = headers + '\n\n' + (body ?? '')
30
- const request = text.replaceAll(SUBSTITUTE, (_, name) => this.variables[name])
31
-
17
+ public override async request (input: string): Promise<any> {
32
18
  await this.gateway.start()
33
-
34
- this.response = await http.request(request, this.origin)
19
+ await super.request(input)
35
20
  }
36
21
 
37
22
  @then('the following reply is sent:')
38
- public responseIncludes (expected: string): void {
39
- const lines = trim(expected).split('\n')
40
-
41
- for (const line of lines) {
42
- const escaped = line.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
43
-
44
- const expression = escaped.replace(CAPTURE,
45
- (_, name) => `(?<${name}>\\S{1,2048})`)
46
-
47
- const rx = new RegExp(expression, 'i')
48
- const match = this.response.match(rx)
49
-
50
- if (match === null)
51
- throw new AssertionError({
52
- message: `Response is missing '${line}'`,
53
- expected: line,
54
- actual: this.response
55
- })
56
-
57
- Object.assign(this.variables, match.groups)
58
- }
23
+ public override responseIncludes (expected: string): void {
24
+ super.responseIncludes(expected)
59
25
  }
60
26
 
61
27
  @then('the reply does not contain:')
62
- public responseExcludes (expected: string): void {
63
- const lines = trim(expected).split('\n')
64
-
65
- for (const line of lines) {
66
- line.replace(SUBSTITUTE, (_, name) => this.variables[name])
67
-
68
- const includes = this.response.includes(line)
69
-
70
- if (includes)
71
- throw new AssertionError({
72
- message: `Response contains '${line}'`,
73
- expected: line,
74
- actual: this.response
75
- })
76
- }
28
+ public override responseExcludes (expected: string): void {
29
+ super.responseExcludes(expected)
77
30
  }
78
31
 
79
32
  @when('the stream of `{word}` is received with the following headers:')
80
33
  public async streamRequest (filename: string, head: string): Promise<any> {
81
- head = trim(head) + '\n\n'
34
+ const stream = open(filename)
82
35
 
83
36
  await this.gateway.start()
84
-
85
- const { url, method, headers } = http.parse.request(head)
86
- const href = new URL(url, this.origin).href
87
- const body = open(filename)
88
-
89
- const request = {
90
- method,
91
- headers,
92
- body: body as unknown as ReadableStream,
93
- duplex: 'half'
94
- }
95
-
96
- try {
97
- const response = await fetch(href, request)
98
-
99
- this.response = await http.parse.response(response)
100
- } catch (e: any) {
101
- console.error(e)
102
- console.error(e.cause)
103
-
104
- throw e
105
- }
37
+ await super.stream(head, stream)
106
38
  }
107
39
 
108
40
  @then('the stream equals to `{word}` is sent with the following headers:')
109
41
  public async responseStreamMatch (filename: string, head: string): Promise<any> {
110
- const buf = await buffer(open(filename))
111
- const text = buf.toString('utf8')
112
- const expected = head + '\n\n' + text
42
+ const stream = open(filename)
113
43
 
114
- this.responseIncludes(expected)
44
+ await super.streamMatch(head, stream)
115
45
  }
116
46
  }
117
-
118
- const CAPTURE = /\\\$\\{\\{ (?<name>[A-Za-z_]{0,32}) \\}\\}/g
119
- const SUBSTITUTE = /\${{ (?<name>[A-Za-z_]{0,32}) }}/g
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/extensions.exposition",
3
- "version": "0.24.0-alpha.2",
3
+ "version": "0.24.0-alpha.4",
4
4
  "description": "Toa Exposition",
5
5
  "author": "temich <tema.gurtovoy@gmail.com>",
6
6
  "homepage": "https://github.com/toa-io/toa#readme",
@@ -17,11 +17,10 @@
17
17
  "access": "public"
18
18
  },
19
19
  "dependencies": {
20
- "@toa.io/core": "0.24.0-alpha.2",
21
- "@toa.io/generic": "0.24.0-alpha.2",
22
- "@toa.io/http": "0.24.0-alpha.2",
23
- "@toa.io/schemas": "0.24.0-alpha.2",
24
- "@toa.io/streams": "0.24.0-alpha.2",
20
+ "@toa.io/core": "0.24.0-alpha.4",
21
+ "@toa.io/generic": "0.24.0-alpha.4",
22
+ "@toa.io/schemas": "0.24.0-alpha.4",
23
+ "@toa.io/streams": "0.24.0-alpha.4",
25
24
  "bcryptjs": "2.4.3",
26
25
  "cors": "2.8.5",
27
26
  "error-value": "0.3.0",
@@ -45,7 +44,8 @@
45
44
  "features": "npx cucumber-js"
46
45
  },
47
46
  "devDependencies": {
48
- "@toa.io/extensions.storages": "0.24.0-alpha.2",
47
+ "@toa.io/extensions.storages": "0.24.0-alpha.4",
48
+ "@toa.io/http": "0.24.0-alpha.4",
49
49
  "@types/bcryptjs": "2.4.3",
50
50
  "@types/cors": "2.8.13",
51
51
  "@types/express": "4.17.17",
@@ -53,5 +53,5 @@
53
53
  "@types/negotiator": "0.6.1",
54
54
  "fs-extra": "11.1.1"
55
55
  },
56
- "gitHead": "36cea9ed2e8add73748dc921b670ec92a227fed4"
56
+ "gitHead": "41f549ae652b625d65f4ba12ea1025f0214fdb0f"
57
57
  }