且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我应该避免在生产代码中使用net/http/httptest吗?

更新时间:2022-06-11 05:33:12

您不应在生产中使用此代码.无论您要实现什么目标.

You should not use this code in production. No matter what you are attempting to achieve.

httptest.ResponseRecorder 的最终目的是为了方便 testing ,因此在进行测试时,将始终对其进行扩展,优化( rewrite ).头脑.可以在此软件包中进行 Any 假设,只要它可以使软件包更好地进行测试即可.软件包作者打算为此与 go test 一起使用" ¹.我将其解释为不鼓励生产使用,因为这样他们就可以***休息,例如您的生产使用情况,以支持 go test 使用情况.

httptest.ResponseRecorder's ultimate purpose is to facilitate testing, and will thus always be extended, optimized—rewritten—with testing in mind. Any assumption can be made in this package, as long as it makes the package better at testing. The package authors intended for this to be "used in concert with go test"¹. I interpret that as discouraging production use, because they are then free to break e.g. your production usage in favor of go test usage.

另一个论点是安全性.任何测试包的安全模型都将是使用受信任,而非测试代码则相反.

Another argument is that of security. The security model of any test package is going to be that the usage is trusted, whereas non-testing code will be the opposite.

软件世界充满危险.您正在要求使其更加危险.

The world of software a perilous place. You are asking to make it even more perilous.