@bifravst/http-api-mock 1.3.5 → 1.4.0
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/dist/src/parseMockRequest.d.ts +7 -0
- package/dist/src/parseMockRequest.js +19 -0
- package/dist/src/parseMockRequest.spec.d.ts +1 -0
- package/dist/src/parseMockRequest.spec.js +23 -0
- package/dist/src/parseMockResponse.d.ts +6 -0
- package/dist/src/parseMockResponse.js +20 -0
- package/dist/src/parseMockResponse.spec.d.ts +1 -0
- package/dist/src/parseMockResponse.spec.js +20 -0
- package/package.json +1 -1
- package/src/parseMockRequest.spec.ts +30 -0
- package/src/parseMockRequest.ts +35 -0
- package/src/parseMockResponse.spec.ts +27 -0
- package/src/parseMockResponse.ts +35 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const parseMockRequest = (r) => {
|
|
2
|
+
const lines = r.split('\n');
|
|
3
|
+
const methodResourceProtol = lines.shift();
|
|
4
|
+
const blankLineLocation = lines.indexOf('');
|
|
5
|
+
const headerLines = blankLineLocation === -1 ? lines : lines.slice(0, blankLineLocation);
|
|
6
|
+
const body = blankLineLocation === -1
|
|
7
|
+
? ''
|
|
8
|
+
: lines.slice(blankLineLocation + 1).join('\n');
|
|
9
|
+
const requestInfo = /^(?<method>[A-Z]+) (?<resource>[^ ]+) (?<protocol>HTTP\/[0-9.]+)/.exec(methodResourceProtol ?? '')?.groups;
|
|
10
|
+
if (requestInfo === null)
|
|
11
|
+
throw new Error(`Invalid request info: ${methodResourceProtol}`);
|
|
12
|
+
return {
|
|
13
|
+
...requestInfo,
|
|
14
|
+
headers: headerLines
|
|
15
|
+
.map((s) => s.split(':', 2))
|
|
16
|
+
.reduce((headers, [k, v]) => ({ ...headers, [k ?? '']: v?.trim() }), {}),
|
|
17
|
+
body,
|
|
18
|
+
};
|
|
19
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { parseMockRequest } from './parseMockRequest.js';
|
|
4
|
+
void describe('parseMockRequest()', () => {
|
|
5
|
+
void it('should parse method, resource, protocol, headers and body', () => assert.deepEqual(parseMockRequest([
|
|
6
|
+
`PATCH /v1/devices/foo/state HTTP/1.1`,
|
|
7
|
+
`Content-Length: 36`,
|
|
8
|
+
`Content-Type: application/json`,
|
|
9
|
+
`If-Match: 8835`,
|
|
10
|
+
``,
|
|
11
|
+
`{"desired":{"config":{"nod":null}}}`,
|
|
12
|
+
].join('\n')), {
|
|
13
|
+
method: 'PATCH',
|
|
14
|
+
resource: '/v1/devices/foo/state',
|
|
15
|
+
protocol: 'HTTP/1.1',
|
|
16
|
+
headers: {
|
|
17
|
+
'Content-Length': '36',
|
|
18
|
+
'Content-Type': 'application/json',
|
|
19
|
+
'If-Match': '8835',
|
|
20
|
+
},
|
|
21
|
+
body: '{"desired":{"config":{"nod":null}}}',
|
|
22
|
+
}));
|
|
23
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const parseMockResponse = (r) => {
|
|
2
|
+
const lines = r.split('\n');
|
|
3
|
+
const protocolStatusCode = lines.shift();
|
|
4
|
+
const blankLineLocation = lines.indexOf('');
|
|
5
|
+
const headerLines = blankLineLocation === -1 ? lines : lines.slice(0, blankLineLocation);
|
|
6
|
+
const body = blankLineLocation === -1
|
|
7
|
+
? ''
|
|
8
|
+
: lines.slice(blankLineLocation + 1).join('\n');
|
|
9
|
+
const responseInfo = /^(?<protocol>HTTP\/[0-9.]+) (?<statusCode>[0-9]+) /.exec(protocolStatusCode ?? '')?.groups;
|
|
10
|
+
if (responseInfo === null)
|
|
11
|
+
throw new Error(`Invalid request info: ${protocolStatusCode}`);
|
|
12
|
+
return {
|
|
13
|
+
statusCode: parseInt(responseInfo.statusCode, 10),
|
|
14
|
+
protocol: responseInfo.protocol,
|
|
15
|
+
headers: headerLines
|
|
16
|
+
.map((s) => s.split(':', 2))
|
|
17
|
+
.reduce((headers, [k, v]) => ({ ...headers, [k ?? '']: v?.trim() }), {}),
|
|
18
|
+
body,
|
|
19
|
+
};
|
|
20
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { parseMockResponse } from './parseMockResponse.js';
|
|
4
|
+
void describe('parseMockResponse()', () => {
|
|
5
|
+
void it('should parse protocol, statusCode, headers and body', () => assert.deepEqual(parseMockResponse([
|
|
6
|
+
`HTTP/1.1 202 Accepted`,
|
|
7
|
+
`Content-Length: 36`,
|
|
8
|
+
`Content-Type: application/json`,
|
|
9
|
+
``,
|
|
10
|
+
`{"desired":{"config":{"nod":null}}}`,
|
|
11
|
+
].join('\n')), {
|
|
12
|
+
statusCode: 202,
|
|
13
|
+
protocol: 'HTTP/1.1',
|
|
14
|
+
headers: {
|
|
15
|
+
'Content-Length': '36',
|
|
16
|
+
'Content-Type': 'application/json',
|
|
17
|
+
},
|
|
18
|
+
body: '{"desired":{"config":{"nod":null}}}',
|
|
19
|
+
}));
|
|
20
|
+
});
|
package/package.json
CHANGED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { describe, it } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { parseMockRequest } from './parseMockRequest.js'
|
|
4
|
+
|
|
5
|
+
void describe('parseMockRequest()', () => {
|
|
6
|
+
void it('should parse method, resource, protocol, headers and body', () =>
|
|
7
|
+
assert.deepEqual(
|
|
8
|
+
parseMockRequest(
|
|
9
|
+
[
|
|
10
|
+
`PATCH /v1/devices/foo/state HTTP/1.1`,
|
|
11
|
+
`Content-Length: 36`,
|
|
12
|
+
`Content-Type: application/json`,
|
|
13
|
+
`If-Match: 8835`,
|
|
14
|
+
``,
|
|
15
|
+
`{"desired":{"config":{"nod":null}}}`,
|
|
16
|
+
].join('\n'),
|
|
17
|
+
),
|
|
18
|
+
{
|
|
19
|
+
method: 'PATCH',
|
|
20
|
+
resource: '/v1/devices/foo/state',
|
|
21
|
+
protocol: 'HTTP/1.1',
|
|
22
|
+
headers: {
|
|
23
|
+
'Content-Length': '36',
|
|
24
|
+
'Content-Type': 'application/json',
|
|
25
|
+
'If-Match': '8835',
|
|
26
|
+
},
|
|
27
|
+
body: '{"desired":{"config":{"nod":null}}}',
|
|
28
|
+
},
|
|
29
|
+
))
|
|
30
|
+
})
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export const parseMockRequest = (
|
|
2
|
+
r: string,
|
|
3
|
+
): {
|
|
4
|
+
method: string
|
|
5
|
+
resource: string
|
|
6
|
+
protocol: string // 'HTTP/1.0' | 'HTTP/1.1'
|
|
7
|
+
headers: Record<string, string>
|
|
8
|
+
body: string
|
|
9
|
+
} => {
|
|
10
|
+
const lines = r.split('\n')
|
|
11
|
+
const methodResourceProtol = lines.shift()
|
|
12
|
+
const blankLineLocation = lines.indexOf('')
|
|
13
|
+
const headerLines =
|
|
14
|
+
blankLineLocation === -1 ? lines : lines.slice(0, blankLineLocation)
|
|
15
|
+
const body =
|
|
16
|
+
blankLineLocation === -1
|
|
17
|
+
? ''
|
|
18
|
+
: lines.slice(blankLineLocation + 1).join('\n')
|
|
19
|
+
|
|
20
|
+
const requestInfo =
|
|
21
|
+
/^(?<method>[A-Z]+) (?<resource>[^ ]+) (?<protocol>HTTP\/[0-9.]+)/.exec(
|
|
22
|
+
methodResourceProtol ?? '',
|
|
23
|
+
)?.groups as { method: string; resource: string; protocol: string }
|
|
24
|
+
|
|
25
|
+
if (requestInfo === null)
|
|
26
|
+
throw new Error(`Invalid request info: ${methodResourceProtol}`)
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
...requestInfo,
|
|
30
|
+
headers: headerLines
|
|
31
|
+
.map((s) => s.split(':', 2))
|
|
32
|
+
.reduce((headers, [k, v]) => ({ ...headers, [k ?? '']: v?.trim() }), {}),
|
|
33
|
+
body,
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { describe, it } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { parseMockResponse } from './parseMockResponse.js'
|
|
4
|
+
|
|
5
|
+
void describe('parseMockResponse()', () => {
|
|
6
|
+
void it('should parse protocol, statusCode, headers and body', () =>
|
|
7
|
+
assert.deepEqual(
|
|
8
|
+
parseMockResponse(
|
|
9
|
+
[
|
|
10
|
+
`HTTP/1.1 202 Accepted`,
|
|
11
|
+
`Content-Length: 36`,
|
|
12
|
+
`Content-Type: application/json`,
|
|
13
|
+
``,
|
|
14
|
+
`{"desired":{"config":{"nod":null}}}`,
|
|
15
|
+
].join('\n'),
|
|
16
|
+
),
|
|
17
|
+
{
|
|
18
|
+
statusCode: 202,
|
|
19
|
+
protocol: 'HTTP/1.1',
|
|
20
|
+
headers: {
|
|
21
|
+
'Content-Length': '36',
|
|
22
|
+
'Content-Type': 'application/json',
|
|
23
|
+
},
|
|
24
|
+
body: '{"desired":{"config":{"nod":null}}}',
|
|
25
|
+
},
|
|
26
|
+
))
|
|
27
|
+
})
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export const parseMockResponse = (
|
|
2
|
+
r: string,
|
|
3
|
+
): {
|
|
4
|
+
statusCode: number
|
|
5
|
+
protocol: string // 'HTTP/1.0' | 'HTTP/1.1'
|
|
6
|
+
headers: Record<string, string>
|
|
7
|
+
body: string
|
|
8
|
+
} => {
|
|
9
|
+
const lines = r.split('\n')
|
|
10
|
+
const protocolStatusCode = lines.shift()
|
|
11
|
+
const blankLineLocation = lines.indexOf('')
|
|
12
|
+
const headerLines =
|
|
13
|
+
blankLineLocation === -1 ? lines : lines.slice(0, blankLineLocation)
|
|
14
|
+
const body =
|
|
15
|
+
blankLineLocation === -1
|
|
16
|
+
? ''
|
|
17
|
+
: lines.slice(blankLineLocation + 1).join('\n')
|
|
18
|
+
|
|
19
|
+
const responseInfo =
|
|
20
|
+
/^(?<protocol>HTTP\/[0-9.]+) (?<statusCode>[0-9]+) /.exec(
|
|
21
|
+
protocolStatusCode ?? '',
|
|
22
|
+
)?.groups as { statusCode: string; protocol: string }
|
|
23
|
+
|
|
24
|
+
if (responseInfo === null)
|
|
25
|
+
throw new Error(`Invalid request info: ${protocolStatusCode}`)
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
statusCode: parseInt(responseInfo.statusCode, 10),
|
|
29
|
+
protocol: responseInfo.protocol,
|
|
30
|
+
headers: headerLines
|
|
31
|
+
.map((s) => s.split(':', 2))
|
|
32
|
+
.reduce((headers, [k, v]) => ({ ...headers, [k ?? '']: v?.trim() }), {}),
|
|
33
|
+
body,
|
|
34
|
+
}
|
|
35
|
+
}
|