Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 21, 2024
1 parent d1aa78f commit f03356b
Show file tree
Hide file tree
Showing 13 changed files with 675 additions and 682 deletions.
8 changes: 0 additions & 8 deletions .npmignore

This file was deleted.

23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,29 @@
"node": ">= 18.19.0"
},
"dependencies": {
"methods": "^1.1.2",
"superagent": "^9.0.1"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.1",
"@eggjs/tsconfig": "1",
"@types/node": "22",
"@types/body-parser": "^1.19.5",
"@types/cookie-parser": "^1.4.8",
"@types/express": "^5.0.0",
"@types/mocha": "10",
"@types/node": "22",
"@types/superagent": "^8.1.9",
"body-parser": "^1.20.3",
"cookie-parser": "^1.4.6",
"egg-bin": "6",
"eslint": "8",
"eslint-config-egg": "14",
"tshy": "3",
"tshy-after": "1",
"typescript": "5",
"body-parser": "^1.20.2",
"cookie-parser": "^1.4.6",
"express": "^4.18.2",
"nock": "^13.3.0",
"nyc": "^15.1.0",
"proxyquire": "^2.1.3",
"should": "^13.2.3"
"should": "^13.2.3",
"tshy": "3",
"tshy-after": "1",
"typescript": "5"
},
"scripts": {
"lint": "eslint --cache src test --ext .ts",
Expand Down Expand Up @@ -79,5 +81,6 @@
"src"
],
"types": "./dist/commonjs/index.d.ts",
"main": "./dist/commonjs/index.js"
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.js"
}
89 changes: 0 additions & 89 deletions src/agent.js

This file was deleted.

93 changes: 93 additions & 0 deletions src/agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import http from 'node:http';
import http2 from 'node:http2';
import type { Server } from 'node:net';
import { agent as Agent } from 'superagent';
import { Test } from './test.js';
import type { AgentOptions, H1RequestListener, H2RequestListener, App } from './types.js';

/**
* Initialize a new `TestAgent`.
*
* @param {Function|Server} app
* @param {Object} options
*/

export class TestAgent extends Agent {
app: Server | string;
_host: string;
#http2 = false;

constructor(appOrListener: App, options: AgentOptions = {}) {
super(options);
if (typeof appOrListener === 'function') {
if (options.http2) {
this.#http2 = true;
this.app = http2.createServer(appOrListener as H2RequestListener); // eslint-disable-line no-param-reassign
} else {
this.app = http.createServer(appOrListener as H1RequestListener); // eslint-disable-line no-param-reassign
}
} else {
this.app = appOrListener;
}
}

// set a host name
host(host: string) {
this._host = host;
return this;
}

// TestAgent.prototype.del = TestAgent.prototype.delete;

protected _testRequest(method: string, url: string) {
const req = new Test(this.app, method.toUpperCase(), url);
if (this.#http2) {
req.http2();
}

if (this._host) {
req.set('host', this._host);
}

const that = this as any;
// access not internal methods
req.on('response', that._saveCookies.bind(this));
req.on('redirect', that._saveCookies.bind(this));
req.on('redirect', that._attachCookies.bind(this, req));
that._setDefaults(req);
that._attachCookies(req);

return req;
}
delete(url: string) {
return this._testRequest('delete', url);
}
del(url: string) {
return this._testRequest('delete', url);
}
get(url: string) {
return this._testRequest('get', url);
}
head(url: string) {
return this._testRequest('head', url);
}
put(url: string) {
return this._testRequest('put', url);
}
post(url: string) {
return this._testRequest('post', url);
}
patch(url: string) {
return this._testRequest('patch', url);
}
options(url: string) {
return this._testRequest('options', url);
}
}

// allow keep use by `agent()`
export const proxyAgent = new Proxy(TestAgent, {
apply(target, _, argumentsList) {
return new target(argumentsList[0], argumentsList[1]);
},
});
12 changes: 12 additions & 0 deletions src/error/AssertError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class AssertError extends Error {
expected: any;
actual: any;

constructor(message: string, expected: any, actual: any, options?: ErrorOptions) {
super(message, options);
this.name = this.constructor.name;
this.expected = expected;
this.actual = actual;
Error.captureStackTrace(this, this.constructor);
}
}
65 changes: 0 additions & 65 deletions src/index.js

This file was deleted.

39 changes: 39 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Request, RequestOptions } from './request.js';
import { TestAgent, proxyAgent } from './agent.js';
import type { App, AgentOptions } from './types.js';

/**
* Test against the given `app`,
* returning a new `Test`.
*/
export function request(app: App, options: RequestOptions = {}) {
return new Request(app, options);
}

export {
Request, RequestOptions,
TestAgent,
// import { agent } from '@eggjs/supertest';
// agent()
proxyAgent as agent,
};

export * from './test.js';

// import request from '@eggjs/supertest';
// request()
export default new Proxy(request, {
apply(target, _, argumentsList) {
return target(argumentsList[0], argumentsList[1]);
},
get(target, property, receiver) {
// import request from '@eggjs/supertest';
// request.agent()
if (property === 'agent') {
return proxyAgent;
}
return Reflect.get(target, property, receiver);
},
}) as unknown as ((app: App, options?: RequestOptions) => Request) & {
agent: (app: App, options?: AgentOptions) => TestAgent;
};
Loading

0 comments on commit f03356b

Please sign in to comment.