| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  | const assert = require('assert') | 
					
						
							|  |  |  | const http = require('http') | 
					
						
							| 
									
										
										
										
											2016-10-24 16:12:49 +09:00
										 |  |  | const https = require('https') | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  | const path = require('path') | 
					
						
							|  |  |  | const fs = require('fs') | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:39 +05:30
										 |  |  | const send = require('send') | 
					
						
							| 
									
										
										
										
											2016-12-01 01:32:17 +05:30
										 |  |  | const auth = require('basic-auth') | 
					
						
							| 
									
										
										
										
											2016-08-03 12:47:53 -07:00
										 |  |  | const {closeWindow} = require('./window-helpers') | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-12 22:38:50 +09:00
										 |  |  | const {ipcRenderer, remote} = require('electron') | 
					
						
							| 
									
										
										
										
											2016-12-01 01:32:17 +05:30
										 |  |  | const {ipcMain, session, BrowserWindow, net} = remote | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  | describe('session module', function () { | 
					
						
							|  |  |  |   var fixtures = path.resolve(__dirname, 'fixtures') | 
					
						
							|  |  |  |   var w = null | 
					
						
							| 
									
										
										
										
											2016-12-19 15:25:56 -08:00
										 |  |  |   var webview = null | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |   var url = 'http://127.0.0.1' | 
					
						
							| 
									
										
										
										
											2016-02-16 17:09:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |   beforeEach(function () { | 
					
						
							| 
									
										
										
										
											2016-02-16 17:39:11 -08:00
										 |  |  |     w = new BrowserWindow({ | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  |       show: false, | 
					
						
							|  |  |  |       width: 400, | 
					
						
							|  |  |  |       height: 400 | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-02-16 17:09:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |   afterEach(function () { | 
					
						
							| 
									
										
										
										
											2016-12-19 16:37:47 -08:00
										 |  |  |     if (webview != null) { | 
					
						
							|  |  |  |       if (!document.body.contains(webview)) { | 
					
						
							|  |  |  |         document.body.appendChild(webview) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       webview.remove() | 
					
						
							| 
									
										
										
										
											2016-12-19 15:25:56 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-03 12:47:53 -07:00
										 |  |  |     return closeWindow(w).then(function () { w = null }) | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-12 22:24:09 +09:00
										 |  |  |   describe('session.defaultSession', function () { | 
					
						
							|  |  |  |     it('returns the default session', function () { | 
					
						
							|  |  |  |       assert.equal(session.defaultSession, session.fromPartition('')) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-12 22:38:50 +09:00
										 |  |  |   describe('session.fromPartition(partition, options)', function () { | 
					
						
							|  |  |  |     it('returns existing session with same partition', function () { | 
					
						
							|  |  |  |       assert.equal(session.fromPartition('test'), session.fromPartition('test')) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('created session is ref-counted', function () { | 
					
						
							|  |  |  |       const partition = 'test2' | 
					
						
							|  |  |  |       const userAgent = 'test-agent' | 
					
						
							|  |  |  |       const ses1 = session.fromPartition(partition) | 
					
						
							|  |  |  |       ses1.setUserAgent(userAgent) | 
					
						
							|  |  |  |       assert.equal(ses1.getUserAgent(), userAgent) | 
					
						
							|  |  |  |       ses1.destroy() | 
					
						
							|  |  |  |       const ses2 = session.fromPartition(partition) | 
					
						
							|  |  |  |       assert.notEqual(ses2.getUserAgent(), userAgent) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-12 22:25:09 +09:00
										 |  |  |   describe('ses.cookies', function () { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |     it('should get cookies', function (done) { | 
					
						
							|  |  |  |       var server = http.createServer(function (req, res) { | 
					
						
							|  |  |  |         res.setHeader('Set-Cookie', ['0=0']) | 
					
						
							|  |  |  |         res.end('finished') | 
					
						
							|  |  |  |         server.close() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       server.listen(0, '127.0.0.1', function () { | 
					
						
							|  |  |  |         var port = server.address().port | 
					
						
							|  |  |  |         w.loadURL(url + ':' + port) | 
					
						
							|  |  |  |         w.webContents.on('did-finish-load', function () { | 
					
						
							| 
									
										
										
										
											2016-02-16 17:39:11 -08:00
										 |  |  |           w.webContents.session.cookies.get({ | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |             url: url | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |           }, function (error, list) { | 
					
						
							|  |  |  |             var cookie, i, len | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |             if (error) { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |               return done(error) | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |             for (i = 0, len = list.length; i < len; i++) { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |               cookie = list[i] | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |               if (cookie.name === '0') { | 
					
						
							|  |  |  |                 if (cookie.value === '0') { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |                   return done() | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |                 } else { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |                   return done('cookie value is ' + cookie.value + ' while expecting 0') | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |                 } | 
					
						
							|  |  |  |               } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |             done('Can not find cookie') | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-02-16 17:09:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-28 18:44:00 -07:00
										 |  |  |     it('calls back with an error when setting a cookie with missing required fields', function (done) { | 
					
						
							|  |  |  |       session.defaultSession.cookies.set({ | 
					
						
							|  |  |  |         url: '', | 
					
						
							|  |  |  |         name: '1', | 
					
						
							|  |  |  |         value: '1' | 
					
						
							|  |  |  |       }, function (error) { | 
					
						
							|  |  |  |         assert.equal(error.message, 'Setting cookie failed') | 
					
						
							|  |  |  |         done() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |     it('should over-write the existent cookie', function (done) { | 
					
						
							| 
									
										
										
										
											2016-02-16 17:39:11 -08:00
										 |  |  |       session.defaultSession.cookies.set({ | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |         url: url, | 
					
						
							|  |  |  |         name: '1', | 
					
						
							|  |  |  |         value: '1' | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |       }, function (error) { | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |         if (error) { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |           return done(error) | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-02-16 17:39:11 -08:00
										 |  |  |         session.defaultSession.cookies.get({ | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  |           url: url | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |         }, function (error, list) { | 
					
						
							|  |  |  |           var cookie, i, len | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  |           if (error) { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |             return done(error) | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  |           } | 
					
						
							|  |  |  |           for (i = 0, len = list.length; i < len; i++) { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |             cookie = list[i] | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |             if (cookie.name === '1') { | 
					
						
							|  |  |  |               if (cookie.value === '1') { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |                 return done() | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  |               } else { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |                 return done('cookie value is ' + cookie.value + ' while expecting 1') | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  |               } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |           done('Can not find cookie') | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-02-16 17:09:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |     it('should remove cookies', function (done) { | 
					
						
							| 
									
										
										
										
											2016-02-16 17:39:11 -08:00
										 |  |  |       session.defaultSession.cookies.set({ | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |         url: url, | 
					
						
							|  |  |  |         name: '2', | 
					
						
							|  |  |  |         value: '2' | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |       }, function (error) { | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  |         if (error) { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |           return done(error) | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |         session.defaultSession.cookies.remove(url, '2', function () { | 
					
						
							| 
									
										
										
										
											2016-02-16 17:39:11 -08:00
										 |  |  |           session.defaultSession.cookies.get({ | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |             url: url | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |           }, function (error, list) { | 
					
						
							|  |  |  |             var cookie, i, len | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |             if (error) { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |               return done(error) | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |             for (i = 0, len = list.length; i < len; i++) { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |               cookie = list[i] | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |               if (cookie.name === '2') { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |                 return done('Cookie not deleted') | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  |               } | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |             done() | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-08-03 16:09:03 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should set cookie for standard scheme', function (done) { | 
					
						
							|  |  |  |       const standardScheme = remote.getGlobal('standardScheme') | 
					
						
							|  |  |  |       const origin = standardScheme + '://fake-host' | 
					
						
							|  |  |  |       session.defaultSession.cookies.set({ | 
					
						
							|  |  |  |         url: origin, | 
					
						
							|  |  |  |         name: 'custom', | 
					
						
							|  |  |  |         value: '1' | 
					
						
							|  |  |  |       }, function (error) { | 
					
						
							|  |  |  |         if (error) { | 
					
						
							|  |  |  |           return done(error) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         session.defaultSession.cookies.get({ | 
					
						
							|  |  |  |           url: origin | 
					
						
							|  |  |  |         }, function (error, list) { | 
					
						
							|  |  |  |           if (error) { | 
					
						
							|  |  |  |             return done(error) | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           assert.equal(list.length, 1) | 
					
						
							|  |  |  |           assert.equal(list[0].name, 'custom') | 
					
						
							|  |  |  |           assert.equal(list[0].value, '1') | 
					
						
							|  |  |  |           assert.equal(list[0].domain, 'fake-host') | 
					
						
							|  |  |  |           done() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-09-28 17:08:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-28 17:39:56 -07:00
										 |  |  |     it('emits a changed event when a cookie is added or removed', function (done) { | 
					
						
							|  |  |  |       const {cookies} = session.fromPartition('cookies-changed') | 
					
						
							| 
									
										
										
										
											2016-09-28 17:08:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-28 17:39:56 -07:00
										 |  |  |       cookies.once('changed', function (event, cookie, cause, removed) { | 
					
						
							|  |  |  |         assert.equal(cookie.name, 'foo') | 
					
						
							|  |  |  |         assert.equal(cookie.value, 'bar') | 
					
						
							|  |  |  |         assert.equal(cause, 'explicit') | 
					
						
							|  |  |  |         assert.equal(removed, false) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cookies.once('changed', function (event, cookie, cause, removed) { | 
					
						
							| 
									
										
										
										
											2016-09-28 17:08:25 -07:00
										 |  |  |           assert.equal(cookie.name, 'foo') | 
					
						
							|  |  |  |           assert.equal(cookie.value, 'bar') | 
					
						
							|  |  |  |           assert.equal(cause, 'explicit') | 
					
						
							| 
									
										
										
										
											2016-09-28 17:39:56 -07:00
										 |  |  |           assert.equal(removed, true) | 
					
						
							| 
									
										
										
										
											2016-09-28 17:08:25 -07:00
										 |  |  |           done() | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2016-09-28 17:39:56 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         cookies.remove(url, 'foo', function (error) { | 
					
						
							| 
									
										
										
										
											2016-09-28 17:08:25 -07:00
										 |  |  |           if (error) return done(error) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-28 17:39:56 -07:00
										 |  |  |       cookies.set({ | 
					
						
							|  |  |  |         url: url, | 
					
						
							|  |  |  |         name: 'foo', | 
					
						
							|  |  |  |         value: 'bar' | 
					
						
							|  |  |  |       }, function (error) { | 
					
						
							|  |  |  |         if (error) return done(error) | 
					
						
							| 
									
										
										
										
											2016-09-28 17:08:25 -07:00
										 |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-01-13 17:19:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-12 22:25:09 +09:00
										 |  |  |   describe('ses.clearStorageData(options)', function () { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |     fixtures = path.resolve(__dirname, 'fixtures') | 
					
						
							|  |  |  |     it('clears localstorage data', function (done) { | 
					
						
							|  |  |  |       ipcMain.on('count', function (event, count) { | 
					
						
							|  |  |  |         ipcMain.removeAllListeners('count') | 
					
						
							| 
									
										
										
										
											2017-03-27 10:21:01 -07:00
										 |  |  |         assert.equal(count, 0) | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |         done() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       w.loadURL('file://' + path.join(fixtures, 'api', 'localstorage.html')) | 
					
						
							|  |  |  |       w.webContents.on('did-finish-load', function () { | 
					
						
							| 
									
										
										
										
											2016-02-17 09:27:25 -08:00
										 |  |  |         var options = { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |           origin: 'file://', | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  |           storages: ['localstorage'], | 
					
						
							|  |  |  |           quotas: ['persistent'] | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |         } | 
					
						
							|  |  |  |         w.webContents.session.clearStorageData(options, function () { | 
					
						
							|  |  |  |           w.webContents.send('getcount') | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-02-02 15:54:51 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-12 22:25:09 +09:00
										 |  |  |   describe('will-download event', function () { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |     beforeEach(function () { | 
					
						
							| 
									
										
										
										
											2016-11-29 11:52:31 -08:00
										 |  |  |       if (w != null) w.destroy() | 
					
						
							| 
									
										
										
										
											2016-02-02 15:54:51 +05:30
										 |  |  |       w = new BrowserWindow({ | 
					
						
							|  |  |  |         show: false, | 
					
						
							|  |  |  |         width: 400, | 
					
						
							|  |  |  |         height: 400 | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-02-16 17:09:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |     it('can cancel default download behavior', function (done) { | 
					
						
							|  |  |  |       const mockFile = new Buffer(1024) | 
					
						
							|  |  |  |       const contentDisposition = 'inline; filename="mockFile.txt"' | 
					
						
							|  |  |  |       const downloadServer = http.createServer(function (req, res) { | 
					
						
							| 
									
										
										
										
											2016-02-02 15:54:51 +05:30
										 |  |  |         res.writeHead(200, { | 
					
						
							|  |  |  |           'Content-Length': mockFile.length, | 
					
						
							|  |  |  |           'Content-Type': 'application/plain', | 
					
						
							|  |  |  |           'Content-Disposition': contentDisposition | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |         }) | 
					
						
							|  |  |  |         res.end(mockFile) | 
					
						
							|  |  |  |         downloadServer.close() | 
					
						
							|  |  |  |       }) | 
					
						
							| 
									
										
										
										
											2016-02-02 15:54:51 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |       downloadServer.listen(0, '127.0.0.1', function () { | 
					
						
							|  |  |  |         const port = downloadServer.address().port | 
					
						
							|  |  |  |         const url = 'http://127.0.0.1:' + port + '/' | 
					
						
							| 
									
										
										
										
											2016-02-02 15:54:51 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |         ipcRenderer.sendSync('set-download-option', false, true) | 
					
						
							|  |  |  |         w.loadURL(url) | 
					
						
							|  |  |  |         ipcRenderer.once('download-error', function (event, downloadUrl, filename, error) { | 
					
						
							|  |  |  |           assert.equal(downloadUrl, url) | 
					
						
							|  |  |  |           assert.equal(filename, 'mockFile.txt') | 
					
						
							|  |  |  |           assert.equal(error, 'Object has been destroyed') | 
					
						
							|  |  |  |           done() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-02-02 15:54:51 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |   describe('DownloadItem', function () { | 
					
						
							|  |  |  |     var mockPDF = new Buffer(1024 * 1024 * 5) | 
					
						
							|  |  |  |     var contentDisposition = 'inline; filename="mock.pdf"' | 
					
						
							|  |  |  |     var downloadFilePath = path.join(fixtures, 'mock.pdf') | 
					
						
							|  |  |  |     var downloadServer = http.createServer(function (req, res) { | 
					
						
							| 
									
										
										
										
											2016-09-10 17:43:27 +05:30
										 |  |  |       if (req.url === '/?testFilename') { | 
					
						
							|  |  |  |         contentDisposition = 'inline' | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2016-01-11 18:40:23 -08:00
										 |  |  |       res.writeHead(200, { | 
					
						
							|  |  |  |         'Content-Length': mockPDF.length, | 
					
						
							|  |  |  |         'Content-Type': 'application/pdf', | 
					
						
							|  |  |  |         'Content-Disposition': contentDisposition | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |       }) | 
					
						
							|  |  |  |       res.end(mockPDF) | 
					
						
							|  |  |  |       downloadServer.close() | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:39 +05:30
										 |  |  |     var assertDownload = function (event, state, url, mimeType, | 
					
						
							|  |  |  |                                    receivedBytes, totalBytes, disposition, | 
					
						
							|  |  |  |                                    filename, port, savePath) { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |       assert.equal(state, 'completed') | 
					
						
							|  |  |  |       assert.equal(filename, 'mock.pdf') | 
					
						
							| 
									
										
										
										
											2016-07-27 17:52:36 -07:00
										 |  |  |       assert.equal(savePath, path.join(__dirname, 'fixtures', 'mock.pdf')) | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |       assert.equal(url, 'http://127.0.0.1:' + port + '/') | 
					
						
							|  |  |  |       assert.equal(mimeType, 'application/pdf') | 
					
						
							|  |  |  |       assert.equal(receivedBytes, mockPDF.length) | 
					
						
							|  |  |  |       assert.equal(totalBytes, mockPDF.length) | 
					
						
							|  |  |  |       assert.equal(disposition, contentDisposition) | 
					
						
							|  |  |  |       assert(fs.existsSync(downloadFilePath)) | 
					
						
							|  |  |  |       fs.unlinkSync(downloadFilePath) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-02-16 17:09:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 19:11:09 +05:30
										 |  |  |     it('can download using WebContents.downloadURL', function (done) { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |       downloadServer.listen(0, '127.0.0.1', function () { | 
					
						
							|  |  |  |         var port = downloadServer.address().port | 
					
						
							|  |  |  |         ipcRenderer.sendSync('set-download-option', false, false) | 
					
						
							| 
									
										
										
										
											2017-01-23 19:11:09 +05:30
										 |  |  |         w.webContents.downloadURL(url + ':' + port) | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:39 +05:30
										 |  |  |         ipcRenderer.once('download-done', function (event, state, url, | 
					
						
							|  |  |  |                                                     mimeType, receivedBytes, | 
					
						
							|  |  |  |                                                     totalBytes, disposition, | 
					
						
							|  |  |  |                                                     filename, savePath) { | 
					
						
							|  |  |  |           assertDownload(event, state, url, mimeType, receivedBytes, | 
					
						
							|  |  |  |                          totalBytes, disposition, filename, port, savePath) | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |           done() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-02-16 17:09:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |     it('can download using WebView.downloadURL', function (done) { | 
					
						
							|  |  |  |       downloadServer.listen(0, '127.0.0.1', function () { | 
					
						
							|  |  |  |         var port = downloadServer.address().port | 
					
						
							|  |  |  |         ipcRenderer.sendSync('set-download-option', false, false) | 
					
						
							| 
									
										
										
										
											2016-12-19 15:25:56 -08:00
										 |  |  |         webview = new WebView() | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |         webview.src = 'file://' + fixtures + '/api/blank.html' | 
					
						
							|  |  |  |         webview.addEventListener('did-finish-load', function () { | 
					
						
							|  |  |  |           webview.downloadURL(url + ':' + port + '/') | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:39 +05:30
										 |  |  |         ipcRenderer.once('download-done', function (event, state, url, | 
					
						
							|  |  |  |                                                     mimeType, receivedBytes, | 
					
						
							|  |  |  |                                                     totalBytes, disposition, | 
					
						
							|  |  |  |                                                     filename, savePath) { | 
					
						
							|  |  |  |           assertDownload(event, state, url, mimeType, receivedBytes, | 
					
						
							|  |  |  |                          totalBytes, disposition, filename, port, savePath) | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |           document.body.removeChild(webview) | 
					
						
							|  |  |  |           done() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         document.body.appendChild(webview) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-02-16 17:09:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |     it('can cancel download', function (done) { | 
					
						
							|  |  |  |       downloadServer.listen(0, '127.0.0.1', function () { | 
					
						
							|  |  |  |         var port = downloadServer.address().port | 
					
						
							|  |  |  |         ipcRenderer.sendSync('set-download-option', true, false) | 
					
						
							| 
									
										
										
										
											2017-01-23 19:11:09 +05:30
										 |  |  |         w.webContents.downloadURL(url + ':' + port + '/') | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:39 +05:30
										 |  |  |         ipcRenderer.once('download-done', function (event, state, url, | 
					
						
							|  |  |  |                                                     mimeType, receivedBytes, | 
					
						
							|  |  |  |                                                     totalBytes, disposition, | 
					
						
							|  |  |  |                                                     filename) { | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |           assert.equal(state, 'cancelled') | 
					
						
							|  |  |  |           assert.equal(filename, 'mock.pdf') | 
					
						
							|  |  |  |           assert.equal(mimeType, 'application/pdf') | 
					
						
							|  |  |  |           assert.equal(receivedBytes, 0) | 
					
						
							|  |  |  |           assert.equal(totalBytes, mockPDF.length) | 
					
						
							|  |  |  |           assert.equal(disposition, contentDisposition) | 
					
						
							|  |  |  |           done() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-08-25 14:04:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-10 17:43:27 +05:30
										 |  |  |     it('can generate a default filename', function (done) { | 
					
						
							| 
									
										
										
										
											2016-09-19 17:11:42 +09:00
										 |  |  |       // Somehow this test always fail on appveyor.
 | 
					
						
							|  |  |  |       if (process.env.APPVEYOR === 'True') return done() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-10 17:43:27 +05:30
										 |  |  |       downloadServer.listen(0, '127.0.0.1', function () { | 
					
						
							|  |  |  |         var port = downloadServer.address().port | 
					
						
							|  |  |  |         ipcRenderer.sendSync('set-download-option', true, false) | 
					
						
							| 
									
										
										
										
											2017-01-23 19:11:09 +05:30
										 |  |  |         w.webContents.downloadURL(url + ':' + port + '/?testFilename') | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:39 +05:30
										 |  |  |         ipcRenderer.once('download-done', function (event, state, url, | 
					
						
							|  |  |  |                                                     mimeType, receivedBytes, | 
					
						
							|  |  |  |                                                     totalBytes, disposition, | 
					
						
							|  |  |  |                                                     filename) { | 
					
						
							| 
									
										
										
										
											2016-09-10 17:43:27 +05:30
										 |  |  |           assert.equal(state, 'cancelled') | 
					
						
							|  |  |  |           assert.equal(filename, 'download.pdf') | 
					
						
							|  |  |  |           assert.equal(mimeType, 'application/pdf') | 
					
						
							|  |  |  |           assert.equal(receivedBytes, 0) | 
					
						
							|  |  |  |           assert.equal(totalBytes, mockPDF.length) | 
					
						
							|  |  |  |           assert.equal(disposition, contentDisposition) | 
					
						
							|  |  |  |           done() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-25 14:04:26 -07:00
										 |  |  |     describe('when a save path is specified and the URL is unavailable', function () { | 
					
						
							|  |  |  |       it('does not display a save dialog and reports the done state as interrupted', function (done) { | 
					
						
							|  |  |  |         ipcRenderer.sendSync('set-download-option', false, false) | 
					
						
							|  |  |  |         ipcRenderer.once('download-done', (event, state) => { | 
					
						
							|  |  |  |           assert.equal(state, 'interrupted') | 
					
						
							|  |  |  |           done() | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2016-08-25 14:40:15 -07:00
										 |  |  |         w.webContents.downloadURL('file://' + path.join(__dirname, 'does-not-exist.txt')) | 
					
						
							| 
									
										
										
										
											2016-08-25 14:04:26 -07:00
										 |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-06-08 20:01:27 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-12 22:25:09 +09:00
										 |  |  |   describe('ses.protocol', function () { | 
					
						
							| 
									
										
										
										
											2016-06-23 13:11:19 +09:00
										 |  |  |     const partitionName = 'temp' | 
					
						
							|  |  |  |     const protocolName = 'sp' | 
					
						
							|  |  |  |     const partitionProtocol = session.fromPartition(partitionName).protocol | 
					
						
							|  |  |  |     const protocol = session.defaultSession.protocol | 
					
						
							| 
									
										
										
										
											2016-06-29 09:42:12 -07:00
										 |  |  |     const handler = function (ignoredError, callback) { | 
					
						
							| 
									
										
										
										
											2016-06-23 13:29:18 +09:00
										 |  |  |       callback({data: 'test', mimeType: 'text/html'}) | 
					
						
							| 
									
										
										
										
											2016-06-23 13:11:19 +09:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(function (done) { | 
					
						
							|  |  |  |       if (w != null) w.destroy() | 
					
						
							| 
									
										
										
										
											2016-06-08 20:01:27 +05:30
										 |  |  |       w = new BrowserWindow({ | 
					
						
							|  |  |  |         show: false, | 
					
						
							|  |  |  |         webPreferences: { | 
					
						
							|  |  |  |           partition: partitionName | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }) | 
					
						
							| 
									
										
										
										
											2016-06-23 13:11:19 +09:00
										 |  |  |       partitionProtocol.registerStringProtocol(protocolName, handler, function (error) { | 
					
						
							| 
									
										
										
										
											2016-06-29 09:37:10 -07:00
										 |  |  |         done(error != null ? error : undefined) | 
					
						
							| 
									
										
										
										
											2016-06-23 13:11:19 +09:00
										 |  |  |       }) | 
					
						
							| 
									
										
										
										
											2016-06-08 20:01:27 +05:30
										 |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-15 21:12:06 +09:00
										 |  |  |     afterEach(function (done) { | 
					
						
							|  |  |  |       partitionProtocol.unregisterProtocol(protocolName, () => done()) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-23 13:14:33 +09:00
										 |  |  |     it('does not affect defaultSession', function (done) { | 
					
						
							| 
									
										
										
										
											2016-06-23 13:11:19 +09:00
										 |  |  |       protocol.isProtocolHandled(protocolName, function (result) { | 
					
						
							|  |  |  |         assert.equal(result, false) | 
					
						
							|  |  |  |         partitionProtocol.isProtocolHandled(protocolName, function (result) { | 
					
						
							|  |  |  |           assert.equal(result, true) | 
					
						
							| 
									
										
										
										
											2016-06-23 13:14:33 +09:00
										 |  |  |           done() | 
					
						
							| 
									
										
										
										
											2016-06-08 20:01:27 +05:30
										 |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-06-23 13:14:33 +09:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-01 22:24:30 +09:00
										 |  |  |     xit('handles requests from partition', function (done) { | 
					
						
							| 
									
										
										
										
											2016-06-23 13:14:33 +09:00
										 |  |  |       w.webContents.on('did-finish-load', function () { | 
					
						
							|  |  |  |         done() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       w.loadURL(`${protocolName}://fake-host`) | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2016-06-08 20:01:27 +05:30
										 |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-07-21 10:01:08 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('ses.setProxy(options, callback)', function () { | 
					
						
							|  |  |  |     it('allows configuring proxy settings', function (done) { | 
					
						
							|  |  |  |       const config = { | 
					
						
							|  |  |  |         proxyRules: 'http=myproxy:80' | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       session.defaultSession.setProxy(config, function () { | 
					
						
							|  |  |  |         session.defaultSession.resolveProxy('http://localhost', function (proxy) { | 
					
						
							|  |  |  |           assert.equal(proxy, 'PROXY myproxy:80') | 
					
						
							|  |  |  |           done() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('allows bypassing proxy settings', function (done) { | 
					
						
							|  |  |  |       const config = { | 
					
						
							|  |  |  |         proxyRules: 'http=myproxy:80', | 
					
						
							|  |  |  |         proxyBypassRules: '<local>' | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       session.defaultSession.setProxy(config, function () { | 
					
						
							|  |  |  |         session.defaultSession.resolveProxy('http://localhost', function (proxy) { | 
					
						
							|  |  |  |           assert.equal(proxy, 'DIRECT') | 
					
						
							|  |  |  |           done() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-08-23 11:29:21 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-24 16:12:49 +09:00
										 |  |  |   describe('ses.getBlobData(identifier, callback)', function () { | 
					
						
							| 
									
										
										
										
											2016-08-23 11:29:21 +05:30
										 |  |  |     it('returns blob data for uuid', function (done) { | 
					
						
							|  |  |  |       const scheme = 'temp' | 
					
						
							|  |  |  |       const protocol = session.defaultSession.protocol | 
					
						
							|  |  |  |       const url = scheme + '://host' | 
					
						
							|  |  |  |       before(function () { | 
					
						
							|  |  |  |         if (w != null) w.destroy() | 
					
						
							|  |  |  |         w = new BrowserWindow({show: false}) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       after(function (done) { | 
					
						
							|  |  |  |         protocol.unregisterProtocol(scheme, () => { | 
					
						
							|  |  |  |           closeWindow(w).then(() => { | 
					
						
							|  |  |  |             w = null | 
					
						
							|  |  |  |             done() | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const postData = JSON.stringify({ | 
					
						
							|  |  |  |         type: 'blob', | 
					
						
							|  |  |  |         value: 'hello' | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       const content = `<html>
 | 
					
						
							|  |  |  |                        <script> | 
					
						
							|  |  |  |                        const {webFrame} = require('electron') | 
					
						
							|  |  |  |                        webFrame.registerURLSchemeAsPrivileged('${scheme}') | 
					
						
							|  |  |  |                        let fd = new FormData(); | 
					
						
							|  |  |  |                        fd.append('file', new Blob(['${postData}'], {type:'application/json'})); | 
					
						
							|  |  |  |                        fetch('${url}', {method:'POST', body: fd }); | 
					
						
							|  |  |  |                        </script> | 
					
						
							|  |  |  |                        </html>` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       protocol.registerStringProtocol(scheme, function (request, callback) { | 
					
						
							|  |  |  |         if (request.method === 'GET') { | 
					
						
							|  |  |  |           callback({data: content, mimeType: 'text/html'}) | 
					
						
							|  |  |  |         } else if (request.method === 'POST') { | 
					
						
							|  |  |  |           let uuid = request.uploadData[1].blobUUID | 
					
						
							|  |  |  |           assert(uuid) | 
					
						
							|  |  |  |           session.defaultSession.getBlobData(uuid, function (result) { | 
					
						
							|  |  |  |             assert.equal(result.toString(), postData) | 
					
						
							|  |  |  |             done() | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }, function (error) { | 
					
						
							|  |  |  |         if (error) return done(error) | 
					
						
							|  |  |  |         w.loadURL(url) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-10-24 16:12:49 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('ses.setCertificateVerifyProc(callback)', function () { | 
					
						
							|  |  |  |     var server = null | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(function (done) { | 
					
						
							|  |  |  |       var certPath = path.join(__dirname, 'fixtures', 'certificates') | 
					
						
							|  |  |  |       var options = { | 
					
						
							|  |  |  |         key: fs.readFileSync(path.join(certPath, 'server.key')), | 
					
						
							|  |  |  |         cert: fs.readFileSync(path.join(certPath, 'server.pem')), | 
					
						
							|  |  |  |         ca: [ | 
					
						
							|  |  |  |           fs.readFileSync(path.join(certPath, 'rootCA.pem')), | 
					
						
							|  |  |  |           fs.readFileSync(path.join(certPath, 'intermediateCA.pem')) | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |         requestCert: true, | 
					
						
							|  |  |  |         rejectUnauthorized: false | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       server = https.createServer(options, function (req, res) { | 
					
						
							|  |  |  |         res.writeHead(200) | 
					
						
							|  |  |  |         res.end('<title>hello</title>') | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       server.listen(0, '127.0.0.1', done) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     afterEach(function () { | 
					
						
							|  |  |  |       session.defaultSession.setCertificateVerifyProc(null) | 
					
						
							|  |  |  |       server.close() | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 13:33:07 -08:00
										 |  |  |     it('accepts the request when the callback is called with 0', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-07 16:35:37 -08:00
										 |  |  |       session.defaultSession.setCertificateVerifyProc(function ({hostname, certificate, verificationResult}, callback) { | 
					
						
							| 
									
										
										
										
											2017-02-08 13:33:07 -08:00
										 |  |  |         assert(['net::ERR_CERT_AUTHORITY_INVALID', 'net::ERR_CERT_COMMON_NAME_INVALID'].includes(verificationResult), verificationResult) | 
					
						
							| 
									
										
										
										
											2016-11-09 13:05:46 +00:00
										 |  |  |         callback(0) | 
					
						
							| 
									
										
										
										
											2016-10-24 16:12:49 +09:00
										 |  |  |       }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       w.webContents.once('did-finish-load', function () { | 
					
						
							|  |  |  |         assert.equal(w.webContents.getTitle(), 'hello') | 
					
						
							|  |  |  |         done() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       w.loadURL(`https://127.0.0.1:${server.address().port}`) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 12:51:24 -08:00
										 |  |  |     describe('deprecated function signature', function () { | 
					
						
							|  |  |  |       it('supports accepting the request', function (done) { | 
					
						
							|  |  |  |         session.defaultSession.setCertificateVerifyProc(function (hostname, certificate, callback) { | 
					
						
							|  |  |  |           assert.equal(hostname, '127.0.0.1') | 
					
						
							|  |  |  |           callback(true) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         w.webContents.once('did-finish-load', function () { | 
					
						
							|  |  |  |           assert.equal(w.webContents.getTitle(), 'hello') | 
					
						
							|  |  |  |           done() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         w.loadURL(`https://127.0.0.1:${server.address().port}`) | 
					
						
							| 
									
										
										
										
											2017-02-07 16:35:37 -08:00
										 |  |  |       }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 12:51:24 -08:00
										 |  |  |       it('supports rejecting the request', function (done) { | 
					
						
							|  |  |  |         session.defaultSession.setCertificateVerifyProc(function (hostname, certificate, callback) { | 
					
						
							|  |  |  |           assert.equal(hostname, '127.0.0.1') | 
					
						
							|  |  |  |           callback(false) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         var url = `https://127.0.0.1:${server.address().port}` | 
					
						
							|  |  |  |         w.webContents.once('did-finish-load', function () { | 
					
						
							|  |  |  |           assert.equal(w.webContents.getTitle(), url) | 
					
						
							|  |  |  |           done() | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         w.loadURL(url) | 
					
						
							| 
									
										
										
										
											2017-02-07 16:35:37 -08:00
										 |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 13:33:07 -08:00
										 |  |  |     it('rejects the request when the callback is called with -2', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-07 16:35:37 -08:00
										 |  |  |       session.defaultSession.setCertificateVerifyProc(function ({hostname, certificate, verificationResult}, callback) { | 
					
						
							| 
									
										
										
										
											2016-10-24 16:12:49 +09:00
										 |  |  |         assert.equal(hostname, '127.0.0.1') | 
					
						
							|  |  |  |         assert.equal(certificate.issuerName, 'Intermediate CA') | 
					
						
							| 
									
										
										
										
											2016-11-11 09:55:56 +00:00
										 |  |  |         assert.equal(certificate.subjectName, 'localhost') | 
					
						
							|  |  |  |         assert.equal(certificate.issuer.commonName, 'Intermediate CA') | 
					
						
							|  |  |  |         assert.equal(certificate.subject.commonName, 'localhost') | 
					
						
							| 
									
										
										
										
											2016-11-15 16:32:12 -08:00
										 |  |  |         assert.equal(certificate.issuerCert.issuer.commonName, 'Root CA') | 
					
						
							| 
									
										
										
										
											2016-11-11 09:55:56 +00:00
										 |  |  |         assert.equal(certificate.issuerCert.subject.commonName, 'Intermediate CA') | 
					
						
							| 
									
										
										
										
											2016-11-15 16:32:12 -08:00
										 |  |  |         assert.equal(certificate.issuerCert.issuerCert.issuer.commonName, 'Root CA') | 
					
						
							|  |  |  |         assert.equal(certificate.issuerCert.issuerCert.subject.commonName, 'Root CA') | 
					
						
							|  |  |  |         assert.equal(certificate.issuerCert.issuerCert.issuerCert, undefined) | 
					
						
							| 
									
										
										
										
											2017-02-08 13:33:07 -08:00
										 |  |  |         assert(['net::ERR_CERT_AUTHORITY_INVALID', 'net::ERR_CERT_COMMON_NAME_INVALID'].includes(verificationResult), verificationResult) | 
					
						
							| 
									
										
										
										
											2016-11-09 13:05:46 +00:00
										 |  |  |         callback(-2) | 
					
						
							| 
									
										
										
										
											2016-10-24 16:12:49 +09:00
										 |  |  |       }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       var url = `https://127.0.0.1:${server.address().port}` | 
					
						
							|  |  |  |       w.webContents.once('did-finish-load', function () { | 
					
						
							|  |  |  |         assert.equal(w.webContents.getTitle(), url) | 
					
						
							|  |  |  |         done() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       w.loadURL(url) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:39 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('ses.createInterruptedDownload(options)', function () { | 
					
						
							|  |  |  |     it('can create an interrupted download item', function (done) { | 
					
						
							|  |  |  |       ipcRenderer.sendSync('set-download-option', true, false) | 
					
						
							|  |  |  |       const filePath = path.join(__dirname, 'fixtures', 'mock.pdf') | 
					
						
							|  |  |  |       const options = { | 
					
						
							|  |  |  |         path: filePath, | 
					
						
							| 
									
										
										
										
											2016-12-09 09:36:44 -08:00
										 |  |  |         urlChain: ['http://127.0.0.1/'], | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:39 +05:30
										 |  |  |         mimeType: 'application/pdf', | 
					
						
							|  |  |  |         offset: 0, | 
					
						
							|  |  |  |         length: 5242880 | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       w.webContents.session.createInterruptedDownload(options) | 
					
						
							|  |  |  |       ipcRenderer.once('download-created', function (event, state, urlChain, | 
					
						
							|  |  |  |                                                      mimeType, receivedBytes, | 
					
						
							|  |  |  |                                                      totalBytes, filename, | 
					
						
							|  |  |  |                                                      savePath) { | 
					
						
							|  |  |  |         assert.equal(state, 'interrupted') | 
					
						
							| 
									
										
										
										
											2016-12-09 09:36:44 -08:00
										 |  |  |         assert.deepEqual(urlChain, ['http://127.0.0.1/']) | 
					
						
							| 
									
										
										
										
											2016-11-24 20:46:39 +05:30
										 |  |  |         assert.equal(mimeType, 'application/pdf') | 
					
						
							|  |  |  |         assert.equal(receivedBytes, 0) | 
					
						
							|  |  |  |         assert.equal(totalBytes, 5242880) | 
					
						
							|  |  |  |         assert.equal(savePath, filePath) | 
					
						
							|  |  |  |         done() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('can be resumed', function (done) { | 
					
						
							|  |  |  |       const fixtures = path.join(__dirname, 'fixtures') | 
					
						
							|  |  |  |       const downloadFilePath = path.join(fixtures, 'logo.png') | 
					
						
							|  |  |  |       const rangeServer = http.createServer(function (req, res) { | 
					
						
							|  |  |  |         let options = { | 
					
						
							|  |  |  |           root: fixtures | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         send(req, req.url, options) | 
					
						
							|  |  |  |         .on('error', function (error) { | 
					
						
							|  |  |  |           done(error) | 
					
						
							|  |  |  |         }).pipe(res) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       ipcRenderer.sendSync('set-download-option', true, false, downloadFilePath) | 
					
						
							|  |  |  |       rangeServer.listen(0, '127.0.0.1', function () { | 
					
						
							|  |  |  |         const port = rangeServer.address().port | 
					
						
							|  |  |  |         const downloadUrl = `http://127.0.0.1:${port}/assets/logo.png` | 
					
						
							|  |  |  |         const callback = function (event, state, url, mimeType, | 
					
						
							|  |  |  |                                    receivedBytes, totalBytes, disposition, | 
					
						
							|  |  |  |                                    filename, savePath, urlChain, | 
					
						
							|  |  |  |                                    lastModifiedTime, eTag) { | 
					
						
							|  |  |  |           if (state === 'cancelled') { | 
					
						
							|  |  |  |             const options = { | 
					
						
							|  |  |  |               path: savePath, | 
					
						
							|  |  |  |               urlChain: urlChain, | 
					
						
							|  |  |  |               mimeType: mimeType, | 
					
						
							|  |  |  |               offset: receivedBytes, | 
					
						
							|  |  |  |               length: totalBytes, | 
					
						
							|  |  |  |               lastModified: lastModifiedTime, | 
					
						
							|  |  |  |               eTag: eTag | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             ipcRenderer.sendSync('set-download-option', false, false, downloadFilePath) | 
					
						
							|  |  |  |             w.webContents.session.createInterruptedDownload(options) | 
					
						
							|  |  |  |           } else { | 
					
						
							|  |  |  |             assert.equal(state, 'completed') | 
					
						
							|  |  |  |             assert.equal(filename, 'logo.png') | 
					
						
							|  |  |  |             assert.equal(savePath, downloadFilePath) | 
					
						
							|  |  |  |             assert.equal(url, downloadUrl) | 
					
						
							|  |  |  |             assert.equal(mimeType, 'image/png') | 
					
						
							|  |  |  |             assert.equal(receivedBytes, 14022) | 
					
						
							|  |  |  |             assert.equal(totalBytes, 14022) | 
					
						
							|  |  |  |             assert(fs.existsSync(downloadFilePath)) | 
					
						
							|  |  |  |             fs.unlinkSync(downloadFilePath) | 
					
						
							|  |  |  |             rangeServer.close() | 
					
						
							|  |  |  |             ipcRenderer.removeListener('download-done', callback) | 
					
						
							|  |  |  |             done() | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         ipcRenderer.on('download-done', callback) | 
					
						
							|  |  |  |         w.webContents.downloadURL(downloadUrl) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-12-01 01:32:17 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('ses.clearAuthCache(options[, callback])', function () { | 
					
						
							|  |  |  |     it('can clear http auth info from cache', function (done) { | 
					
						
							|  |  |  |       const ses = session.fromPartition('auth-cache') | 
					
						
							|  |  |  |       const server = http.createServer(function (req, res) { | 
					
						
							|  |  |  |         var credentials = auth(req) | 
					
						
							|  |  |  |         if (!credentials || credentials.name !== 'test' || credentials.pass !== 'test') { | 
					
						
							|  |  |  |           res.statusCode = 401 | 
					
						
							|  |  |  |           res.setHeader('WWW-Authenticate', 'Basic realm="Restricted"') | 
					
						
							|  |  |  |           res.end() | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2016-12-01 18:10:02 +05:30
										 |  |  |           res.end('authenticated') | 
					
						
							| 
									
										
										
										
											2016-12-01 01:32:17 +05:30
										 |  |  |         } | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       server.listen(0, '127.0.0.1', function () { | 
					
						
							|  |  |  |         const port = server.address().port | 
					
						
							|  |  |  |         function issueLoginRequest (attempt = 1) { | 
					
						
							|  |  |  |           if (attempt > 2) { | 
					
						
							|  |  |  |             server.close() | 
					
						
							|  |  |  |             return done() | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           const request = net.request({ | 
					
						
							|  |  |  |             url: `http://127.0.0.1:${port}`, | 
					
						
							|  |  |  |             session: ses | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |           request.on('login', function (info, callback) { | 
					
						
							|  |  |  |             attempt++ | 
					
						
							|  |  |  |             assert.equal(info.scheme, 'basic') | 
					
						
							|  |  |  |             assert.equal(info.realm, 'Restricted') | 
					
						
							|  |  |  |             callback('test', 'test') | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |           request.on('response', function (response) { | 
					
						
							|  |  |  |             let data = '' | 
					
						
							| 
									
										
										
										
											2016-12-01 18:10:02 +05:30
										 |  |  |             response.pause() | 
					
						
							| 
									
										
										
										
											2016-12-01 01:32:17 +05:30
										 |  |  |             response.on('data', function (chunk) { | 
					
						
							|  |  |  |               data += chunk | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             response.on('end', function () { | 
					
						
							|  |  |  |               assert.equal(data, 'authenticated') | 
					
						
							|  |  |  |               ses.clearAuthCache({type: 'password'}, function () { | 
					
						
							|  |  |  |                 issueLoginRequest(attempt) | 
					
						
							|  |  |  |               }) | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             response.on('error', function (error) { | 
					
						
							|  |  |  |               done(error) | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2016-12-01 18:10:02 +05:30
										 |  |  |             response.resume() | 
					
						
							| 
									
										
										
										
											2016-12-01 01:32:17 +05:30
										 |  |  |           }) | 
					
						
							|  |  |  |           // Internal api to bypass cache for testing.
 | 
					
						
							|  |  |  |           request.urlRequest._setLoadFlags(1 << 1) | 
					
						
							|  |  |  |           request.end() | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         issueLoginRequest() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-12-19 15:25:56 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('ses.setPermissionRequestHandler(handler)', () => { | 
					
						
							|  |  |  |     it('cancels any pending requests when cleared', (done) => { | 
					
						
							|  |  |  |       const ses = session.fromPartition('permissionTest') | 
					
						
							|  |  |  |       ses.setPermissionRequestHandler(() => { | 
					
						
							|  |  |  |         ses.setPermissionRequestHandler(null) | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       webview = new WebView() | 
					
						
							|  |  |  |       webview.addEventListener('ipc-message', function (e) { | 
					
						
							|  |  |  |         assert.equal(e.channel, 'message') | 
					
						
							|  |  |  |         assert.deepEqual(e.args, ['SecurityError']) | 
					
						
							|  |  |  |         done() | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       webview.src = 'file://' + fixtures + '/pages/permissions/midi-sysex.html' | 
					
						
							|  |  |  |       webview.partition = 'permissionTest' | 
					
						
							|  |  |  |       webview.setAttribute('nodeintegration', 'on') | 
					
						
							|  |  |  |       document.body.appendChild(webview) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2016-03-25 13:03:49 -07:00
										 |  |  | }) |