
用於測試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擴展的功能。
有關詳細信息,請參見發布歷史記錄。
根據麻省理工學院許可。