fix(responder): preserve custom Content-Type header in Data - #69
Open
AdamMagued wants to merge 1 commit into
Open
AdamMagued wants to merge 1 commit into
AdamMagued wants to merge 1 commit into
Conversation
Data previously overwrote any existing Content-Type header on the ResponseWriter with application/octet-stream. This prevented handlers from using Data to render custom binary payloads such as images or PDFs. Only set application/octet-stream when Content-Type is not already set. Add unit tests for default and custom Content-Type handling. Fixes go-chi#16
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #16
Problem
render.Dataunconditionally overwrote theContent-Typeheader onhttp.ResponseWriterwithapplication/octet-stream. This made it impossible for handlers to set a custom content type (such asimage/png,application/pdf, or custom binary media types) when writing binary responses throughrender.Data.Solution
Only set the default
application/octet-streamContent-Type whenw.Header().Get("Content-Type")is empty. If a handler or middleware has already specified aContent-Typeon the response writer, that content type is preserved.Testing
Added unit tests in
responder_test.go:Datadefaults toapplication/octet-streamwhen noContent-Typeis set.Content-Typeheaders (e.g.image/png,application/pdf,text/csv; charset=utf-8) are preserved.render.Statusare preserved alongside custom headers.