简体   繁体   中英

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

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?

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 eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM