@opra/testing 0.25.5 → 0.26.1
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/cjs/test-client.js +45 -46
- package/esm/test-client.js +45 -48
- package/package.json +4 -4
- package/types/test-client.d.ts +4 -8
package/cjs/test-client.js
CHANGED
|
@@ -1,60 +1,59 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OpraTestClient = void 0;
|
|
3
|
+
exports.OpraTestClient = exports.kContext = void 0;
|
|
4
4
|
const http_1 = require("http");
|
|
5
5
|
const client_1 = require("@opra/client");
|
|
6
6
|
const common_1 = require("@opra/common");
|
|
7
7
|
const api_expect_js_1 = require("./api-expect/api-expect.js");
|
|
8
|
+
exports.kContext = Symbol.for('kContext');
|
|
8
9
|
class OpraTestClient extends client_1.OpraHttpClient {
|
|
9
10
|
constructor(app, options) {
|
|
10
11
|
super('/', options);
|
|
11
12
|
this._server = app instanceof http_1.Server ? app : (0, http_1.createServer)(app);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
headers.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
13
|
+
const superCreateResponse = this[exports.kContext].createResponse;
|
|
14
|
+
const superFetch = this[exports.kContext].fetch;
|
|
15
|
+
// Overwrite "createResponse" method
|
|
16
|
+
this[exports.kContext].createResponse = (init) => {
|
|
17
|
+
const resp = superCreateResponse(init);
|
|
18
|
+
resp.expect = new api_expect_js_1.ApiExpect(resp);
|
|
19
|
+
return resp;
|
|
20
|
+
};
|
|
21
|
+
// Overwrite "fetch" method
|
|
22
|
+
this[exports.kContext].fetch = (urlString, req = {}) => {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
const url = new common_1.OpraURL(urlString, 'http://opra.test');
|
|
25
|
+
// Set protocol to HTTP
|
|
26
|
+
url.protocol = 'http';
|
|
27
|
+
// Apply original host to request header
|
|
28
|
+
const headers = req.headers = (req.headers || {});
|
|
29
|
+
if (url.host !== 'opra.test' && headers?.host == null)
|
|
30
|
+
headers.host = url.host;
|
|
31
|
+
new Promise((subResolve) => {
|
|
32
|
+
if (this._server.listening)
|
|
33
|
+
subResolve();
|
|
34
|
+
else
|
|
35
|
+
this._server.listen(0, '127.0.0.1', () => subResolve());
|
|
36
|
+
}).then(() => {
|
|
37
|
+
const address = this._server.address();
|
|
38
|
+
url.host = '127.0.0.1';
|
|
39
|
+
url.port = address.port.toString();
|
|
40
|
+
return superFetch(url.toString(), req);
|
|
41
|
+
}).then(res => {
|
|
42
|
+
if (!this._server.listening)
|
|
43
|
+
return resolve(res);
|
|
44
|
+
this._server.once('close', () => resolve(res));
|
|
45
|
+
this._server.close();
|
|
46
|
+
this._server.unref();
|
|
47
|
+
}).then()
|
|
48
|
+
.catch(error => {
|
|
49
|
+
if (!this._server.listening)
|
|
50
|
+
return reject(error);
|
|
51
|
+
this._server.once('close', () => reject(error));
|
|
52
|
+
this._server.close();
|
|
53
|
+
this._server.unref();
|
|
54
|
+
});
|
|
51
55
|
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
_createResponse(init) {
|
|
55
|
-
const resp = super._createResponse(init);
|
|
56
|
-
resp.expect = new api_expect_js_1.ApiExpect(resp);
|
|
57
|
-
return resp;
|
|
56
|
+
};
|
|
58
57
|
}
|
|
59
58
|
}
|
|
60
59
|
exports.OpraTestClient = OpraTestClient;
|
package/esm/test-client.js
CHANGED
|
@@ -1,58 +1,55 @@
|
|
|
1
1
|
import { createServer, Server } from 'http';
|
|
2
|
-
import {
|
|
3
|
-
// BatchRequest,
|
|
4
|
-
OpraHttpClient, } from '@opra/client';
|
|
2
|
+
import { OpraHttpClient } from '@opra/client';
|
|
5
3
|
import { OpraURL } from '@opra/common';
|
|
6
4
|
import { ApiExpect } from './api-expect/api-expect.js';
|
|
5
|
+
export const kContext = Symbol.for('kContext');
|
|
7
6
|
export class OpraTestClient extends OpraHttpClient {
|
|
8
7
|
constructor(app, options) {
|
|
9
8
|
super('/', options);
|
|
10
9
|
this._server = app instanceof Server ? app : createServer(app);
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
headers.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
10
|
+
const superCreateResponse = this[kContext].createResponse;
|
|
11
|
+
const superFetch = this[kContext].fetch;
|
|
12
|
+
// Overwrite "createResponse" method
|
|
13
|
+
this[kContext].createResponse = (init) => {
|
|
14
|
+
const resp = superCreateResponse(init);
|
|
15
|
+
resp.expect = new ApiExpect(resp);
|
|
16
|
+
return resp;
|
|
17
|
+
};
|
|
18
|
+
// Overwrite "fetch" method
|
|
19
|
+
this[kContext].fetch = (urlString, req = {}) => {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
const url = new OpraURL(urlString, 'http://opra.test');
|
|
22
|
+
// Set protocol to HTTP
|
|
23
|
+
url.protocol = 'http';
|
|
24
|
+
// Apply original host to request header
|
|
25
|
+
const headers = req.headers = (req.headers || {});
|
|
26
|
+
if (url.host !== 'opra.test' && headers?.host == null)
|
|
27
|
+
headers.host = url.host;
|
|
28
|
+
new Promise((subResolve) => {
|
|
29
|
+
if (this._server.listening)
|
|
30
|
+
subResolve();
|
|
31
|
+
else
|
|
32
|
+
this._server.listen(0, '127.0.0.1', () => subResolve());
|
|
33
|
+
}).then(() => {
|
|
34
|
+
const address = this._server.address();
|
|
35
|
+
url.host = '127.0.0.1';
|
|
36
|
+
url.port = address.port.toString();
|
|
37
|
+
return superFetch(url.toString(), req);
|
|
38
|
+
}).then(res => {
|
|
39
|
+
if (!this._server.listening)
|
|
40
|
+
return resolve(res);
|
|
41
|
+
this._server.once('close', () => resolve(res));
|
|
42
|
+
this._server.close();
|
|
43
|
+
this._server.unref();
|
|
44
|
+
}).then()
|
|
45
|
+
.catch(error => {
|
|
46
|
+
if (!this._server.listening)
|
|
47
|
+
return reject(error);
|
|
48
|
+
this._server.once('close', () => reject(error));
|
|
49
|
+
this._server.close();
|
|
50
|
+
this._server.unref();
|
|
51
|
+
});
|
|
50
52
|
});
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
_createResponse(init) {
|
|
54
|
-
const resp = super._createResponse(init);
|
|
55
|
-
resp.expect = new ApiExpect(resp);
|
|
56
|
-
return resp;
|
|
53
|
+
};
|
|
57
54
|
}
|
|
58
55
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/testing",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.1",
|
|
4
4
|
"description": "Opra testing package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,15 +25,15 @@
|
|
|
25
25
|
"clean:cover": "rimraf ../../coverage/testing"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@opra/client": "^0.
|
|
29
|
-
"@opra/common": "^0.
|
|
28
|
+
"@opra/client": "^0.26.1",
|
|
29
|
+
"@opra/common": "^0.26.1",
|
|
30
30
|
"ansi-colors": "^4.1.3",
|
|
31
31
|
"lodash.isnil": "^4.0.0",
|
|
32
32
|
"lodash.omitby": "^4.6.0",
|
|
33
33
|
"rule-judgment": "^1.1.5"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@types/supertest": "^2.0.
|
|
36
|
+
"@types/supertest": "^2.0.14"
|
|
37
37
|
},
|
|
38
38
|
"type": "module",
|
|
39
39
|
"module": "./esm/index.js",
|
package/types/test-client.d.ts
CHANGED
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { IncomingMessage, Server, ServerResponse } from 'http';
|
|
3
|
-
import {
|
|
4
|
-
import { HttpCollectionNode, HttpResponse, HttpSingletonNode, OpraHttpClient, OpraHttpClientOptions } from '@opra/client';
|
|
3
|
+
import { OpraHttpClient } from '@opra/client';
|
|
5
4
|
import { ApiExpect } from './api-expect/api-expect.js';
|
|
6
5
|
declare type RequestListener = (req: IncomingMessage, res: ServerResponse) => void;
|
|
6
|
+
export declare const kContext: unique symbol;
|
|
7
7
|
export type ResponseExt = {
|
|
8
8
|
expect: ApiExpect;
|
|
9
9
|
};
|
|
10
|
-
export declare class OpraTestClient extends OpraHttpClient {
|
|
10
|
+
export declare class OpraTestClient extends OpraHttpClient<ResponseExt> {
|
|
11
11
|
protected _server: Server;
|
|
12
|
-
constructor(app: Server | RequestListener, options?:
|
|
13
|
-
collection<TType = any>(resourceName: string | Type<TType>): HttpCollectionNode<TType, ResponseExt>;
|
|
14
|
-
singleton<TType = any>(resourceName: string | Type<TType>): HttpSingletonNode<TType, ResponseExt>;
|
|
15
|
-
protected _fetch(urlString: string, req?: RequestInit): Promise<Response>;
|
|
16
|
-
protected _createResponse(init?: HttpResponse.Initiator): HttpResponse;
|
|
12
|
+
constructor(app: Server | RequestListener, options?: OpraHttpClient.Options);
|
|
17
13
|
}
|
|
18
14
|
export {};
|