Merge pull request #3882 from deepak1556/chromium_spec_patch

spec: serviceWorker registration with file scheme
This commit is contained in:
Cheng Zhao 2015-12-21 18:39:18 +08:00
commit c68e38f480
3 changed files with 48 additions and 1 deletions

View file

@ -5,7 +5,7 @@ path = require 'path'
ws = require 'ws'
{remote} = require 'electron'
{BrowserWindow} = remote.require 'electron'
{BrowserWindow, session} = remote.require 'electron'
describe 'chromium feature', ->
fixtures = path.resolve __dirname, 'fixtures'
@ -56,6 +56,26 @@ describe 'chromium feature', ->
it 'should not be empty', ->
assert.notEqual navigator.language, ''
describe 'navigator.serviceWorker', ->
url = "file://#{fixtures}/pages/service-worker/index.html"
w = null
afterEach ->
w?.destroy()
it 'should register for file scheme', (done) ->
w = new BrowserWindow(show:false)
w.webContents.on 'ipc-message', (event, args) ->
if args[0] == 'reload'
w.webContents.reload()
else if args[0] == 'error'
done('unexpected error : ' + args[1])
else if args[0] == 'response'
assert.equal args[1], 'Hello from serviceWorker!'
session.defaultSession.clearStorageData {storages: ['serviceworkers']}, ->
done()
w.loadURL url
describe 'window.open', ->
@timeout 10000

View file

@ -0,0 +1,18 @@
<script>
const ipcRenderer = require('electron').ipcRenderer;
navigator.serviceWorker.register('service-worker.js', {scope: './'}).then(function() {
if (navigator.serviceWorker.controller) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://dummy/echo');
xhr.setRequestHeader('X-Mock-Response', 'yes');
xhr.addEventListener('load', function() {
ipcRenderer.send('response', xhr.responseText);
});
xhr.send();
} else {
ipcRenderer.send('reload');
}
}).catch(function(error) {
ipcRenderer.send('error', error);
})
</script>

View file

@ -0,0 +1,9 @@
self.addEventListener('fetch', function(event) {
var requestUrl = new URL(event.request.url);
if (requestUrl.pathname === '/echo' &&
event.request.headers.has('X-Mock-Response')) {
var mockResponse = new Response('Hello from serviceWorker!');
event.respondWith(mockResponse);
}
});