Should I avoid using net/http/httptest in production code?

瘦欲@ 提交于 2019-12-12 22:34:25

问题


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.

Am I tempting fate?


回答1:


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

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.



来源:https://stackoverflow.com/questions/52808399/should-i-avoid-using-net-http-httptest-in-production-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!