gpt4 book ai didi

node.js - 如何创建模拟 http.ServerRequest 和 http.ServerResponse?

转载 作者:行者123 更新时间:2023-11-28 21:05:36 25 4
gpt4 key购买 nike

我已经为 http.Serverrequest event 编写了一个处理程序(即带有签名的函数 function (request, response) { ... }),我想测试它。我想通过模拟 http.ServerRequesthttp.ServerResponse 对象来做到这一点。我怎样才能创建这些?

显而易见的方法似乎行不通:

$ node
> var http = require('http');
> new http.ServerRequest();
TypeError: undefined is not a function
at repl:1:9
...

我需要通过“真正的”HTTP 服务器和客户端来测试吗?

最佳答案

至少有两个项目允许模拟http.ServerRequesthttp.ServerResponse:https://github.com/howardabrams/node-mocks-httphttps://github.com/vojtajina/node-mocks .

出于某种原因,通过真实的 HTTP 请求进行测试似乎更常见; https://github.com/flatiron/nock似乎是这里使用的工具。

另见 node.js: Mock http request and response .

关于node.js - 如何创建模拟 http.ServerRequest 和 http.ServerResponse?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15022544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com