forked from ladjs/supertest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
675 additions
and
682 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.