Skip to content

Commit

Permalink
Fixed some issues parsing headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Christians authored and yq314 committed Oct 29, 2020
1 parent 439cb26 commit 0e539a2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
25 changes: 16 additions & 9 deletions src/braze/tags/connectedContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import Context from '../../context/context'
import ITagImplOptions from '../../template/tag/itag-impl-options'
// @ts-ignore
import * as rp_ from 'request-promise-cache'
import { OperationCanceledException } from 'typescript'
const rp = rp_

const re = new RegExp(`(https?(?:[^\\s\\{\\}]+|\\{\\{.*?\\}\\})+)(\\s+.*)?$`)
const re = new RegExp(`(https?(?:[^\\s\\{\\}]+|\\{\\{.*?\\}\\})+)(\\s+(\\s|.)*)?$`)
const headerRegex = new RegExp(`:headers\\s+(\\{(.|\\s)*?\\})`)

// supported options: :basic_auth, :content_type, :save, :cache, :method, :body, :headers
export default <ITagImplOptions>{
Expand All @@ -18,11 +20,17 @@ export default <ITagImplOptions>{
const options = match[2]
this.options = {}
if (options) {
options.split(/\s+:/).forEach((optStr) => {
// first extract the headers option if it exists, because the headers JSON may contain a /\s+:/
const headersMatch = options.match(headerRegex)
if( headersMatch != null ) {
this.options.headers = JSON.parse(headersMatch[1])
console.log(headersMatch[1])
}
options.replace(headerRegex, '').split(/\s+:/).forEach((optStr) => {
if (optStr === '') return

const opts = optStr.split(/\s+/)
this.options[opts[0]] = opts.length > 1 ? opts.slice(1).join(" ") : true;
this.options[opts[0]] = opts.length > 1 ? opts[1] : true;
})
}
},
Expand All @@ -49,19 +57,18 @@ export default <ITagImplOptions>{
contentType = this.options.content_type || 'application/x-www-form-urlencoded'
}

let headers = {
const headers = {
'User-Agent': 'brazejs-client',
'Content-Type': contentType,
'Accept': this.options.content_type
}
if( this.options.headers ) {
// Add specified headers to the headers
Object.keys(JSON.parse(this.options.headers)).forEach( key => {
// but not if they are already specified
if( !headers[key] ) {
headers[key] = this.options[key];
}
Object.keys(this.options.headers).forEach( key => {
console.log(key)
headers[key] = this.options.headers[key];
})
console.log(headers)
}

const rpOption = {
Expand Down
32 changes: 24 additions & 8 deletions test/integration/braze/tags/connectedContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,21 +265,37 @@ describe('braze/tags/connected_content', function () {
'testHeader': 'headerValue'
}
})
.get('/headertest')
.reply(200, { success: true })
.post('/headertest')
.reply(200, 'pass')
.persist()

nock('http://localhost:8080', {
reqheaders: {
'User-Agent': 'differentAgent',
'testHeader': 'headerValue'
}
})
.post('/agent2')
.reply(200, 'pass')
.persist()
})

it('should not pull incorrect (already existing) header from connected content block', async function () {
const src = '{% connected_content http://localhost:8080/headertest :headers { "User-Agent": "someOtherAgent", "testHeader": "headerValue" } :method post %}'
it('should pull correct header from connected content block', async function() {
const src = '{% connected_content http://localhost:8080/headertest :headers { "testHeader": "headerValue" } :method post %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('')
expect(html).to.equal('pass')
})

it('should pull correct header from connected content block', async function() {
const src = '{% connected_content http://localhost:8080/headertest :headers { "testHeader": "headerValue" } %}'
it('should pull correct header from multi-line connected content block', async function() {
const src = '{% connected_content \nhttp://localhost:8080/headertest \n:body p \n:headers { \n"testHeader": "headerValue" \n} \n:content_type application/json \n:method post \n%}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('pass')
})

it('should overwrite user-agent header from connected content block', async function () {
const src = '{% connected_content http://localhost:8080/agent2 :headers { "User-Agent": "differentAgent", "testHeader": "headerValue" } :method post %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('{ success: true }')
expect(html).to.equal('pass')
})

})
Expand Down

0 comments on commit 0e539a2

Please sign in to comment.