Fix/missing content length header on empty post req #1003
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change aims to resolve issue #996 by changing requests that contain nil body to http.NoBody when they have also specified to "SetContentLength" as true.
Validity as to whether to merge this or not depends on multiple factors.
Setting the bodyBuf value to http.NoBody signals to the http library when creating the request that the body is no longer nil and a Content-Length header component can be created for the request.
Considering this is a feature, albeit a poorly documented one in the standard http library it does seem to be behaving as intended?
In resty setting SetContentLength(true) and r.Body = nil, could be considered as the user intentionally overriding this requirement to force the Content-Length header on whilst passing in a nil body.
Ultimately, either the intention of the http libraries functionality should be obeyed or we should consider this unique resty request setting combination as an intentional force override to achieve a valid Content-Length header with a nil body.
Additional Notes:
I have had to update middleware_test.go:Test_parseRequestBody() for the reason being that now that nil body + SetContentLength(true) is supported in this PR change this now results in the expectedBodyBuf value to change since in this scenario the bodyBuf changes to http.NoBody.