问题描述
我喜欢在net/http.ResponseWriter 呈现为 net/http.Response
的功能.golang.org/pkg/net/http/httptest/#ResponseRecorder"rel =" nofollow noreferrer> net/http/httptest.ResponseRecorder
会给我,但包名让我有点担心在生产代码中使用它.我了解它打算主要用于测试,但是不确定我是否在生产代码中使用它时会遇到麻烦.
I like the ability to present a buffered net/http.ResponseWriter
as a net/http.Response
in some situations which net/http/httptest.ResponseRecorder
would give me, but the package name makes me a bit concerned about using it in production code. I understand that it’s intended to be used primarily for testing, but am unsure if I am asking for trouble using it in production code.
我有缘分吗?
推荐答案
您不应在生产中使用此代码.无论您要实现什么目标.
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.
这篇关于我应该避免在生产代码中使用net/http/httptest吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!