
用于测试Express,Next.js和KOA路由功能的模拟“ HTTP”对象,但可用于测试任何具有代码的Node.js Web Server应用程序,这些代码需要request和response对象的模型。
该项目可作为NPM软件包。
$ npm install node-mocks-http --save-dev
$ npm install @types/node @types/express --save-dev # when using TypeScript或者
$ yarn add node-mocks-http --dev
$ yarn add @types/node @types/express --dev # when using TypeScript安装包装后,包括测试文件中的以下内容:
const httpMocks = require ( 'node-mocks-http' ) ; 假设您有以下明确路线:
app . get ( '/user/:id' , routeHandler ) ;您已经创建了一个函数来处理该路由的调用:
const routeHandler = function ( request , response ) { ... } ;您可以使用您选择的测试框架轻松地使用类似代码来测试routeHandler函数:
exports [ 'routeHandler - Simple testing' ] = function ( test ) {
const request = httpMocks . createRequest ( {
method : 'GET' ,
url : '/user/42' ,
params : {
id : 42
}
} ) ;
const response = httpMocks . createResponse ( ) ;
routeHandler ( request , response ) ;
const data = response . _getJSONData ( ) ; // short-hand for JSON.parse( response._getData() );
test . equal ( 'Bob Dog' , data . name ) ;
test . equal ( 42 , data . age ) ;
test . equal ( '[email protected]' , data . email ) ;
test . equal ( 200 , response . statusCode ) ;
test . ok ( response . _isEndCalled ( ) ) ;
test . ok ( response . _isJSON ( ) ) ;
test . ok ( response . _isUTF8 ( ) ) ;
test . done ( ) ;
} ;打字稿的打字与此项目捆绑在一起。特别是, .createRequest() ,. .createResponse()和.createMocks()方法已键入并且是通用的。除非明确指定,否则它们将返回基于明确的请求/响应对象:
it ( 'should handle expressjs requests' , ( ) => {
const mockExpressRequest = httpMocks . createRequest ( {
method : 'GET' ,
url : '/user/42' ,
params : {
id : 42
}
} ) ;
const mockExpressResponse = httpMocks . createResponse ( ) ;
routeHandler ( request , response ) ;
const data = response . _getJSONData ( ) ;
test . equal ( 'Bob Dog' , data . name ) ;
test . equal ( 42 , data . age ) ;
test . equal ( '[email protected]' , data . email ) ;
test . equal ( 200 , response . statusCode ) ;
test . ok ( response . _isEndCalled ( ) ) ;
test . ok ( response . _isJSON ( ) ) ;
test . ok ( response . _isUTF8 ( ) ) ;
test . done ( ) ;
} ) ;模拟请求和响应中的预期类型参数期望任何扩展nodejs http.IncomingRequest接口或提取API Request类的类型。这意味着您也可以模拟其他框架的请求。 NextJS请求的一个示例将是这样的:
it ( 'should handle nextjs requests' , ( ) => {
const mockExpressRequest = httpMocks . createRequest < NextApiRequest > ( {
method : 'GET' ,
url : '/user/42' ,
params : {
id : 42
}
} ) ;
const mockExpressResponse = httpMocks . createResponse < NextApiResponse > ( ) ;
// ... the rest of the test as above.
} ) ;也可以从NextJS新代理人中嘲笑请求:
it ( 'should handle nextjs app reouter requests' , ( ) => {
const mockExpressRequest = httpMocks . createRequest < NextRequest > ( {
method : 'GET' ,
url : '/user/42' ,
params : {
id : 42
}
} ) ;
const mockExpressResponse = httpMocks . createResponse < NextResponse > ( ) ;
// ... the rest of the test as above.
} ) ; httpMocks.createRequest(options)
选项是具有以下任何值的对象哈希:
| 选项 | 描述 | 默认值 |
|---|---|---|
method | 请求HTTP方法 | '得到' |
url | 请求URL | '' |
originalUrl | 请求原始URL | url |
baseUrl | 请求基本网址 | url |
path | 请求路径 | '' |
params | 带有参数的对象哈希 | {} |
session | 具有会话值的对象哈希 | undefined |
cookies | 带有请求cookie的对象哈希 | {} |
socket | 带有请求套接字的对象哈希 | {} |
signedCookies | 带有签名饼干的对象哈希 | undefined |
headers | 带有请求标题的对象哈希 | {} |
body | 物体的对象哈希 | {} |
query | 具有查询值的对象哈希 | {} |
files | 对象哈希具有值 | {} |
从此函数返回的对象还支持Express请求函数( .accepts() ,. .is() ,. .get() ,. .range()等)。请发送任何缺少功能的PR。
httpMocks . createResponse ( options ) ;选项是具有以下任何值的对象哈希:
| 选项 | 描述 | 默认值 |
|---|---|---|
locals | 包含response本地变量的对象 | {} |
eventEmitter | response对象使用的事件发射器 | mockEventEmitter |
writableStream | response对象使用的可写流 | mockWritableStream |
req | 请求响应的对象 | 无效的 |
注意:
node-mocks-http随附的开箱即用的模拟事件发射器不是功能性事件发射器,因此实际上并没有发出事件。如果您想测试活动处理程序,则需要带上自己的活动发射器。
这是一个例子:
const httpMocks = require ( 'node-mocks-http' ) ;
const res = httpMocks . createResponse ( {
eventEmitter : require ( 'events' ) . EventEmitter
} ) ;
// ...
it ( 'should do something' , function ( done ) {
res . on ( 'end' , function ( ) {
assert . equal ( ... ) ;
done ( ) ;
} ) ;
} ) ;
// ...这是发送请求主体并触发其“数据”和“结束”事件的一个示例:
const httpMocks = require ( 'node-mocks-http' ) ;
const req = httpMocks . createRequest ( ) ;
const res = httpMocks . createResponse ( {
eventEmitter : require ( 'events' ) . EventEmitter
} ) ;
// ...
it ( 'should do something' , function ( done ) {
res . on ( 'end' , function ( ) {
expect ( response . _getData ( ) ) . to . equal ( 'data sent in request' ) ;
done ( ) ;
} ) ;
route ( req , res ) ;
req . send ( 'data sent in request' ) ;
} ) ;
function route ( req , res ) {
let data = [ ] ;
req . on ( 'data' , ( chunk ) => {
data . push ( chunk ) ;
} ) ;
req . on ( 'end' , ( ) => {
data = Buffer . concat ( data ) ;
res . write ( data ) ;
res . end ( ) ;
} ) ;
}
// ... httpMocks . createMocks ( reqOptions , resOptions ) ;合并createRequest和createResponse 。通过给定选项对象的每个构造函数。返回具有属性req和res对象。
我们想要一些简单的模型,而没有大框架。
我们还希望模拟的行为就像被模拟的原始框架一样,但是在调用后打电话和检查值之前,请允许设置值。
我们正在寻找更多的志愿者为该项目带来价值,包括从HTTP模块创建更多对象。
该项目并未解决必须嘲笑的所有功能,但这是一个很好的开始。随时发送拉动请求,团队成员将及时合并它们。
如果您想贡献,请阅读我们的贡献指南。
大多数将使用我们的模型修复错误或添加类似于Node.js提供的实际Request和Response对象并通过Express扩展的功能。
有关详细信息,请参见发布历史记录。
根据麻省理工学院许可。