| 
									
										
										
										
											2015-06-16 10:37:39 +08:00
										 |  |  | assert  = require 'assert' | 
					
						
							|  |  |  | remote  = require 'remote' | 
					
						
							| 
									
										
										
										
											2015-06-16 17:23:29 +08:00
										 |  |  | http    = require 'http' | 
					
						
							|  |  |  | path    = require 'path' | 
					
						
							| 
									
										
										
										
											2015-06-16 10:37:39 +08:00
										 |  |  | BrowserWindow = remote.require 'browser-window' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe 'cookies module', -> | 
					
						
							| 
									
										
										
										
											2015-06-16 17:23:29 +08:00
										 |  |  |   fixtures = path.resolve __dirname, 'fixtures' | 
					
						
							| 
									
										
										
										
											2015-06-16 10:37:39 +08:00
										 |  |  |   w = null | 
					
						
							| 
									
										
										
										
											2015-06-20 10:42:18 +08:00
										 |  |  |   url = "http://127.0.0.1" | 
					
						
							| 
									
										
										
										
											2015-06-16 17:23:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-16 10:37:39 +08:00
										 |  |  |   beforeEach -> w = new BrowserWindow(show: true) | 
					
						
							|  |  |  |   afterEach -> w.destroy() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it 'should get cookies', (done) -> | 
					
						
							|  |  |  |     server = http.createServer (req, res) -> | 
					
						
							|  |  |  |       console.log req | 
					
						
							|  |  |  |       res.setHeader('Set-Cookie', ['type=dummy']) | 
					
						
							|  |  |  |       res.end('finished') | 
					
						
							| 
									
										
										
										
											2015-06-20 10:42:18 +08:00
										 |  |  |       server.close() | 
					
						
							| 
									
										
										
										
											2015-06-16 10:37:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-20 10:42:18 +08:00
										 |  |  |     port = remote.process.port | 
					
						
							|  |  |  |     server.listen port, '127.0.0.1', -> | 
					
						
							| 
									
										
										
										
											2015-06-16 10:37:39 +08:00
										 |  |  |       {port} = server.address() | 
					
						
							| 
									
										
										
										
											2015-06-20 10:42:18 +08:00
										 |  |  |       w.loadUrl "#{url}:#{port}" | 
					
						
							| 
									
										
										
										
											2015-06-16 10:37:39 +08:00
										 |  |  |       w.webContents.on 'did-finish-load', ()-> | 
					
						
							| 
									
										
										
										
											2015-06-20 10:42:18 +08:00
										 |  |  |         w.webContents.session.cookies.get {url: url}, (error, cookies) -> | 
					
						
							| 
									
										
										
										
											2015-06-16 10:37:39 +08:00
										 |  |  |           throw error if error | 
					
						
							|  |  |  |           assert.equal 1, cookies.length | 
					
						
							|  |  |  |           assert.equal 'type', cookies[0].name | 
					
						
							|  |  |  |           assert.equal 'dummy', cookies[0].value | 
					
						
							|  |  |  |           done() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it 'should overwrite the existent cookie', (done) -> | 
					
						
							| 
									
										
										
										
											2015-06-16 17:23:29 +08:00
										 |  |  |     w.loadUrl 'file://' + path.join(fixtures, 'page', 'a.html') | 
					
						
							|  |  |  |     w.webContents.on 'did-finish-load', ()-> | 
					
						
							| 
									
										
										
										
											2015-06-20 10:42:18 +08:00
										 |  |  |       w.webContents.session.cookies.set {url: url, name: 'type', value: 'dummy2'}, (error) -> | 
					
						
							| 
									
										
										
										
											2015-06-16 10:37:39 +08:00
										 |  |  |         throw error if error | 
					
						
							| 
									
										
										
										
											2015-06-20 10:42:18 +08:00
										 |  |  |         w.webContents.session.cookies.get {url: url}, (error, cookies_list) -> | 
					
						
							| 
									
										
										
										
											2015-06-16 17:23:29 +08:00
										 |  |  |           throw error if error | 
					
						
							|  |  |  |           assert.equal 1, cookies_list.length | 
					
						
							|  |  |  |           assert.equal 'type', cookies_list[0].name | 
					
						
							|  |  |  |           assert.equal 'dummy2', cookies_list[0].value | 
					
						
							|  |  |  |           done() | 
					
						
							| 
									
										
										
										
											2015-06-16 10:37:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it 'should set new cookie', (done) -> | 
					
						
							| 
									
										
										
										
											2015-06-16 17:23:29 +08:00
										 |  |  |     w.loadUrl 'file://' + path.join(fixtures, 'page', 'a.html') | 
					
						
							|  |  |  |     w.webContents.on 'did-finish-load', ()-> | 
					
						
							| 
									
										
										
										
											2015-06-20 10:42:18 +08:00
										 |  |  |       w.webContents.session.cookies.set {url: url, name: 'key', value: 'dummy2'}, (error) -> | 
					
						
							| 
									
										
										
										
											2015-06-16 10:37:39 +08:00
										 |  |  |         throw error if error | 
					
						
							| 
									
										
										
										
											2015-06-20 10:42:18 +08:00
										 |  |  |         w.webContents.session.cookies.get {url: url}, (error, cookies_list) -> | 
					
						
							| 
									
										
										
										
											2015-06-16 17:23:29 +08:00
										 |  |  |           throw error if error | 
					
						
							|  |  |  |           assert.equal 2, cookies_list.length | 
					
						
							|  |  |  |           for cookie in cookies_list | 
					
						
							|  |  |  |             if cookie.name is 'key' | 
					
						
							|  |  |  |                assert.equal 'dummy2', cookie.value | 
					
						
							|  |  |  |                done(); | 
					
						
							| 
									
										
										
										
											2015-06-16 10:37:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   it 'should remove cookies', (done) -> | 
					
						
							| 
									
										
										
										
											2015-06-16 17:23:29 +08:00
										 |  |  |     w.loadUrl 'file://' + path.join(fixtures, 'page', 'a.html') | 
					
						
							|  |  |  |     w.webContents.on 'did-finish-load', ()-> | 
					
						
							| 
									
										
										
										
											2015-06-20 10:42:18 +08:00
										 |  |  |       w.webContents.session.cookies.get {url: url}, (error, cookies_list) -> | 
					
						
							| 
									
										
										
										
											2015-06-16 17:23:29 +08:00
										 |  |  |         count = 0 | 
					
						
							|  |  |  |         for cookie in cookies_list | 
					
						
							| 
									
										
										
										
											2015-06-20 10:42:18 +08:00
										 |  |  |           w.webContents.session.cookies.remove {url: url, name: cookie.name}, (error) -> | 
					
						
							| 
									
										
										
										
											2015-06-16 17:23:29 +08:00
										 |  |  |             throw error if error | 
					
						
							|  |  |  |             ++count | 
					
						
							|  |  |  |             if count == cookies_list.length | 
					
						
							| 
									
										
										
										
											2015-06-20 10:42:18 +08:00
										 |  |  |               w.webContents.session.cookies.get {url: url}, (error, cookies_list) -> | 
					
						
							| 
									
										
										
										
											2015-06-16 17:23:29 +08:00
										 |  |  |                 throw error if error | 
					
						
							|  |  |  |                 assert.equal 0, cookies_list.length | 
					
						
							|  |  |  |                 done() |