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