stretchr/codecs — pull request #23 by nelsam (2014-02-11 21:54:51+00:00)
Title: File extension precedence

My PR that was just merged apparently broke some tests in stretchr, because it was preferring the Accept header to the extension. Based on the code here: https://github.com/stretchr/codecs/blob/2536901b7e82f6065e7670e83be1525b6961c0e6/services/web_codec_service.go#L58-L90 it looks like the old code didn't prefer any method - it just returned the first codec that matched _any_ case (except that it would always try to return JSONP when hasCallback is true). @tylerb pointed out that the extension should be preferred over the Accept header.

I wasn't told explicitly that hasCallback should be preferred over extension and accept, but the original code would always return JSONP if hasCallback was true, so running the hasCallback check before anything else will retain the most compatibility with existing code.

This PR does the following:

1. Adds tests to ensure that the extension is preferred over the Accept header, and hasCallback is preferred over everything.
2. Reorders GetCodecForResponding to:
1. If hasCallback is true, return the first codec found that CanMarshalWithCallback()
2. If extension is non-empty, return the first codec that can support the extension
3. If accept is non-empty, return the first codec that can support the requested MIME type
4. return the first codec

I just made an assumption about hasCallback based on previous code, so let me know if the hasCallback loop should be moved elsewhere.