Skip to content

Commit

Permalink
fix: skip cache when cache=0 in connected_content
Browse files Browse the repository at this point in the history
  • Loading branch information
Qing Ye committed May 1, 2019
1 parent 8732c12 commit f01f2ff
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/braze/tags/connectedContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ export default <ITagImplOptions>{
let cacheTTL = 300 * 1000 // default 5 mins
if (method !== 'GET') {
cacheTTL = 0
} else if (this.options.cache) {
cacheTTL = this.options.cache * 1000
} else {
const cache = parseInt(this.options.cache, 10)
if (cache > 0) {
cacheTTL = cache * 1000
} else if (cache === 0) {
// This is a hack, nano-cache will not expire cache when ttl = 0
cacheTTL = 1
}
}

let contentType = this.options.content_type
Expand Down
26 changes: 25 additions & 1 deletion test/integration/braze/tags/connectedContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('braze/tags/connected_content', function () {
})

it('should save to var', async function () {
const src = '{% connected_content http://localhost:8080/json/1 :save user %}' +
const src = '{% connected_content http://localhost:8080/json/1 :retry :save user %}' +
'{{user.first_name}} {{user.__http_status_code__}}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('Qing 200')
Expand Down Expand Up @@ -80,6 +80,20 @@ describe('braze/tags/connected_content', function () {
return expect(html).to.equal('500')
})

it('should not add status code if result is not object', async function () {
nock('http://localhost:8080', {
reqheaders: {
'User-Agent': 'brazejs-client'
}
})
.get('/200')
.reply(200, [1, 2])

const src = '{% connected_content http://localhost:8080/200 :save user %}{{user.__http_status_code__}}'
const html = await liquid.parseAndRender(src)
return expect(html).to.equal('')
})

describe('basic auth should work', async function () {
it('should set basic auth', async function () {
nock('http://localhost:8080', {
Expand Down Expand Up @@ -224,5 +238,15 @@ describe('braze/tags/connected_content', function () {
const html3 = await liquid.parseAndRender(src)
expect(html3).to.equal('')
})

it('should not cache if set to 0', async function () {
const src = '{% connected_content http://localhost:8080/cache :cache 0 %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('cached response')

clock.tick(1)
const html2 = await liquid.parseAndRender(src)
expect(html2).to.equal('')
})
})
})

0 comments on commit f01f2ff

Please sign in to comment.