httptest.ResponseRecorder and superfluous WriteHeader calls

105 views
Skip to first unread message

TheDiveO

unread,
Mar 22, 2023, 12:51:33 PM3/22/23
to golang-nuts
While maintaining my Go spaserve module for serving single-page applications with client-side DOM routing and on different routes simultaneously I noticed different logging behavior between http.timeoutWriter.WriteHeader and httptest.ResponseRecorder.WriteHeader.

While the "production" response writer explicitly logs and thus warns of superfluous WriteHeader calls, the testing response recorder doesn't and stays silent. I would have expected that an httptest response recorder would at least also log any superfluous calls, or better, somehow report such superfluous calls in a way that tests can check and fail where necessary.

Would it make sense to create a feature request issue for httptest?

What would be a good design to make httptest detect and preferably immediately report superfluous calls to WriteHeader in order immediately detect the "offending" call site? I recon that calling panic would be rather rude. Would an additional (new) "SuperfluousWriteHeader func()" struct field in ResponseRecorder be an acceptable proposal? SuperfluousWriteHeader would be called if non-nil and would be used by tests to wire into their particular testing harness.

Other ideas and suggestions?

Note bene: at the moment, I'm wrapping httptest's ResponseRecorder in my own WrappedResponseRecorder. Maybe there's a better way to do this?
Reply all
Reply to author
Forward
0 new messages