Skip to content

Commit

Permalink
Stop wrapping non-JSON responses in quotes
Browse files Browse the repository at this point in the history
Fix Issue #32:
- Prevent non-JSON response types from being wrapped in quotes
  - This bug required clients to do additional parsing of non-JSON types
    (removing quotes and escaped characters)
- Add test for checking non-JSON response Content-Type
- Update change log
  • Loading branch information
kahmali committed Apr 23, 2015
1 parent 6b97277 commit 4b30919
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 220 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Change Log

## Unreleased

#### Fixed
- Issue #32:
- Prevent non-JSON response types from being wrapped in quotes
- This bug required clients to do additional parsing of non-JSON types
(removing quotes and escaped characters)


## [v0.6.4] - 2015-04-14

#### Added
Expand Down
38 changes: 27 additions & 11 deletions lib/route.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,37 @@ class @Route
###
Respond to an HTTP request
###
_respond: (endpointContext, body, statusCode=200, headers) ->
_respond: (endpointContext, body, statusCode=200, headers={}) ->
# Allow cross-domain requests to be made from the browser
endpointContext.response.setHeader 'Access-Control-Allow-Origin', '*'

# Ensure that a content type is set (will be overridden if also included in given headers)
# TODO: Consider enforcing a text/json-only content type (override any user-defined content-type)
endpointContext.response.setHeader 'Content-Type', 'text/json'
# TODO: Consider only lowercasing the header keys we need normalized, like Content-Type
headers = @_lowerCaseKeys(headers)

# Prettify JSON if configured in API
if @api.config.prettyJson
bodyAsJson = JSON.stringify body, undefined, 2
else
bodyAsJson = JSON.stringify body
# Ensure that a Content-Type is set (will be overridden if also included in given headers)
if not headers['content-type']
headers['content-type'] = 'text/json'

# Prepare JSON body for response when Content-Type indicates JSON type
if headers['content-type'].indexOf('json') >= 0 or headers['content-type'].indexOf('javascript') >= 0
if @api.config.prettyJson
body = JSON.stringify body, undefined, 2
else
body = JSON.stringify body

# Send response
endpointContext.response.writeHead statusCode, headers
endpointContext.response.write bodyAsJson
endpointContext.response.end()
endpointContext.response.write body
endpointContext.response.end()


###
Return the object with all of the keys converted to lowercase
###
_lowerCaseKeys: (object) ->
_.chain object
.pairs()
.map (attr) ->
[attr[0].toLowerCase(), attr[1]]
.object()
.value()
4 changes: 2 additions & 2 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ Package.onTest(function (api) {
api.use('coffeescript');
api.use('peterellisjones:describe');

api.addFiles('test/route_tests.coffee');
api.addFiles('test/api_tests.coffee');
api.addFiles('test/route_tests.coffee', 'server');
api.addFiles('test/api_tests.coffee', 'server');
});
Loading

0 comments on commit 4b30919

Please sign in to comment.