Skip to content

Commit

Permalink
Merge pull request #149 from recurly/normalize-response-headers
Browse files Browse the repository at this point in the history
Makes response header references case insensitive
  • Loading branch information
douglasmiller authored Oct 6, 2020
2 parents 372e59a + fcf6012 commit b776402
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions lib/recurly/Http.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ function makeRequest (options, requestBody) {

const submitRequest = () => {
const httpRequest = https.request(options, (httpResponse) => {
// downcase response headers
const responseHeaders = {}
for (const header in httpResponse.headers) {
responseHeaders[header.toLowerCase()] = httpResponse.headers[header]
}

var responseStream
const encoding = httpResponse.headers['content-encoding']
const encoding = responseHeaders['content-encoding']

if (encoding === 'gzip') {
responseStream = zlib.createGunzip(ZLIB_OPTIONS)
Expand All @@ -42,7 +48,7 @@ function makeRequest (options, requestBody) {
} else {
responseStream = httpResponse
}
responseStream.setEncoding(responseEncoding(httpResponse.headers['content-type']))
responseStream.setEncoding(responseEncoding(responseHeaders['content-type']))

const chunks = []
responseStream.on('data', chunk => chunks.push(chunk))
Expand Down Expand Up @@ -107,30 +113,35 @@ class Response {
if (body && body.length > 0) {
resp.body = body
}
// downcase response headers
const responseHeaders = {}
for (const header in response.headers) {
responseHeaders[header.toLowerCase()] = response.headers[header]
}
resp.status = response.statusCode
resp.requestId = response.headers['x-request-id']
resp.rateLimit = parseInt(response.headers['x-ratelimit-limit'], 10)
resp.rateLimitRemaining = parseInt(response.headers['x-ratelimit-remaining'], 10)
resp.rateLimitReset = new Date(parseInt(response.headers['x-ratelimit-reset'], 10) * 1000)
if (response.headers['content-type']) {
resp.contentType = response.headers['content-type'].split(';')[0]
resp.requestId = responseHeaders['x-request-id']
resp.rateLimit = parseInt(responseHeaders['x-ratelimit-limit'], 10)
resp.rateLimitRemaining = parseInt(responseHeaders['x-ratelimit-remaining'], 10)
resp.rateLimitReset = new Date(parseInt(responseHeaders['x-ratelimit-reset'], 10) * 1000)
if (responseHeaders['content-type']) {
resp.contentType = responseHeaders['content-type'].split(';')[0]
}
resp.recordCount = null
resp.apiDeprecated = false
resp.apiSunsetDate = null
const deprecated = response.headers['Recurly-Deprecated'] || ''
const deprecated = responseHeaders['recurly-deprecated'] || ''
if (deprecated.toUpperCase() === 'TRUE') {
resp.apiDeprecated = true
resp.apiSunsetDate = response.headers['Recurly-Sunset-Date']
resp.apiSunsetDate = responseHeaders['recurly-sunset-date']
}

if (response.headers['recurly-total-records']) {
resp.recordCount = parseInt(response.headers['recurly-total-records'], 10)
if (responseHeaders['recurly-total-records']) {
resp.recordCount = parseInt(responseHeaders['recurly-total-records'], 10)
}
resp.date = response.headers['date']
resp.date = responseHeaders['date']
resp.proxyMetadata = {
'server': response.headers['server'],
'cf-ray': response.headers['cf-ray']
'server': responseHeaders['server'],
'cf-ray': responseHeaders['cf-ray']
}

return resp
Expand Down

0 comments on commit b776402

Please sign in to comment.