问题
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