Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Fix etag not saving
Browse files Browse the repository at this point in the history
This adds some basic tests that etag is saving and also fixes an
intermittent problem in automated tests where the downloaded files are
not availble yet.

I'm doing more download of data file work for extensions soon so I'll
add further tests soon.

Fix #3222

Auditors: @bridiver
  • Loading branch information
bbondy committed Aug 17, 2016
1 parent 876c8bc commit b681acd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
10 changes: 9 additions & 1 deletion app/dataFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ const storagePath = (url) =>
const downloadPath = (url) => `${storagePath(url)}.temp`

function downloadSingleFile (resourceName, url, version, force, resolve, reject) {
// console.log('downloading file for: ', resourceName, url)
let headers = {}
const AppStore = require('../js/stores/appStore')
const etag = AppStore.getState().getIn([resourceName, 'etag'])
if (!force && etag) {
// console.log('setting etag: ', etag)
headers = {
'If-None-Match': etag
}
Expand All @@ -34,6 +36,7 @@ function downloadSingleFile (resourceName, url, version, force, resolve, reject)
reject('could not rename downloaded file')
} else {
appActions.setResourceETag(resourceName, newEtag)
// console.log('set resource last check: ', resourceName, version, new Date().getTime())
appActions.setResourceLastCheck(resourceName, version, new Date().getTime())
resolve()
}
Expand Down Expand Up @@ -107,8 +110,13 @@ module.exports.init = (resourceName, startExtension, onInitDone, forceDownload)
})
})

// console.log('should redownload first? ', resourceName, version, module.exports.shouldRedownloadFirst(resourceName, version))
// If the last check version changes we always want to force a download, otherwise we always don't want to force
const AppStore = require('../js/stores/appStore')
const lastCheckVersion = AppStore.getState().getIn([resourceName, 'lastCheckVersion'])
// console.log('lastCheckVersion, version: ', lastCheckVersion, version, lastCheckVersion !== version)
if (forceDownload || module.exports.shouldRedownloadFirst(resourceName, version)) {
module.exports.downloadDataFile(resourceName, url, version, false)
module.exports.downloadDataFile(resourceName, url, version, lastCheckVersion !== version)
.then(loadProcess.bind(null, resourceName, version))
.catch(loadProcess.bind(null, resourceName, version))
} else {
Expand Down
8 changes: 7 additions & 1 deletion js/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ module.exports.requestDataFile = (url, headers, path, reject, resolve) => {
if (!defaultSession) {
reject('Request failed, no session available')
} else {
// console.log('webRequest.fetch: ', url, headers, path)
defaultSession.webRequest.fetch(url, { headers, path }, (err, response) => {
// console.log('response: ', response)
if (!err && response.statusCode === 200) {
resolve(headers['etag'])
let etag = response.headers['etag']
if (etag && etag.constructor === Array) {
etag = etag[0]
}
resolve(etag)
} else {
reject(`Got HTTP status code ${response.statusCode}`)
}
Expand Down
12 changes: 11 additions & 1 deletion test/components/braveryPanelTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ describe('Bravery Panel', function () {
}

function * openBraveMenu (client) {
yield client
return client
.windowByUrl(Brave.browserWindowUrl)
.waitForVisible(braveMenu)
.click(braveMenu)
.waitForVisible(braveryPanel)
}

function * waitForDataFile (client, dataFile) {
return client.waitUntil(function () {
return this.getAppState().then((val) =>
val.value[dataFile].etag && val.value[dataFile].etag.length > 0)
})
}

describe('General', function () {
Brave.beforeAll(this)
before(function * () {
Expand All @@ -44,6 +51,7 @@ describe('Bravery Panel', function () {
yield setup(this.app.client)
})
it('detects blocked elements', function * () {
yield waitForDataFile(this.app.client, 'trackingProtection')
const url = Brave.server.url('tracking.html')
yield this.app.client
.tabByIndex(0)
Expand All @@ -56,6 +64,7 @@ describe('Bravery Panel', function () {
})
})
it('detects adblock elements', function * () {
yield waitForDataFile(this.app.client, 'adblock')
const url = Brave.server.url('adblock.html')
yield this.app.client
.tabByIndex(0)
Expand All @@ -68,6 +77,7 @@ describe('Bravery Panel', function () {
})
})
it('detects https upgrades', function * () {
yield waitForDataFile(this.app.client, 'httpsEverywhere')
const url = Brave.server.url('httpsEverywhere.html')
yield this.app.client
.tabByIndex(0)
Expand Down

0 comments on commit b681acd

Please sign in to comment.