mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
fix: implement 'login' event for WebContents (#20954)
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import { expect } from 'chai'
|
||||
import * as cp from 'child_process'
|
||||
import * as https from 'https'
|
||||
import * as http from 'http'
|
||||
import * as net from 'net'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { app, BrowserWindow, Menu } from 'electron'
|
||||
import { emittedOnce } from './events-helpers'
|
||||
import { closeWindow } from './window-helpers'
|
||||
import { closeWindow, closeAllWindows } from './window-helpers'
|
||||
import { ifdescribe } from './spec-helpers'
|
||||
import split = require('split')
|
||||
|
||||
@@ -1415,6 +1416,33 @@ describe('default behavior', () => {
|
||||
expect(output[0]).to.equal(output[1])
|
||||
})
|
||||
})
|
||||
|
||||
describe('login event', () => {
|
||||
afterEach(closeAllWindows)
|
||||
let server: http.Server
|
||||
let serverUrl: string
|
||||
|
||||
before((done) => {
|
||||
server = http.createServer((request, response) => {
|
||||
if (request.headers.authorization) {
|
||||
return response.end('ok')
|
||||
}
|
||||
response
|
||||
.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Foo"' })
|
||||
.end()
|
||||
}).listen(0, '127.0.0.1', () => {
|
||||
serverUrl = 'http://127.0.0.1:' + (server.address() as net.AddressInfo).port
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should emit a login event on app when a WebContents hits a 401', async () => {
|
||||
const w = new BrowserWindow({ show: false })
|
||||
w.loadURL(serverUrl)
|
||||
const [, webContents] = await emittedOnce(app, 'login')
|
||||
expect(webContents).to.equal(w.webContents)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
async function runTestApp (name: string, ...args: any[]) {
|
||||
|
||||
@@ -313,11 +313,6 @@ describe('session module', () => {
|
||||
|
||||
beforeEach(async () => {
|
||||
customSession = session.fromPartition('proxyconfig')
|
||||
// FIXME(deepak1556): This is just a hack to force
|
||||
// creation of request context which in turn initializes
|
||||
// the network context, can be removed with network
|
||||
// service enabled.
|
||||
await customSession.clearHostResolverCache()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
|
||||
@@ -1513,4 +1513,99 @@ describe('webContents module', () => {
|
||||
await devtoolsClosed
|
||||
})
|
||||
})
|
||||
|
||||
describe('login event', () => {
|
||||
afterEach(closeAllWindows)
|
||||
|
||||
let server: http.Server
|
||||
let serverUrl: string
|
||||
let serverPort: number
|
||||
let proxyServer: http.Server
|
||||
let proxyServerPort: number
|
||||
|
||||
before((done) => {
|
||||
server = http.createServer((request, response) => {
|
||||
if (request.url === '/no-auth') {
|
||||
return response.end('ok')
|
||||
}
|
||||
if (request.headers.authorization) {
|
||||
response.writeHead(200, { 'Content-type': 'text/plain' })
|
||||
return response.end(request.headers.authorization)
|
||||
}
|
||||
response
|
||||
.writeHead(401, { 'WWW-Authenticate': 'Basic realm="Foo"' })
|
||||
.end()
|
||||
}).listen(0, '127.0.0.1', () => {
|
||||
serverPort = (server.address() as AddressInfo).port
|
||||
serverUrl = `http://127.0.0.1:${serverPort}`
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
before((done) => {
|
||||
proxyServer = http.createServer((request, response) => {
|
||||
if (request.headers['proxy-authorization']) {
|
||||
response.writeHead(200, { 'Content-type': 'text/plain' })
|
||||
return response.end(request.headers['proxy-authorization'])
|
||||
}
|
||||
response
|
||||
.writeHead(407, { 'Proxy-Authenticate': 'Basic realm="Foo"' })
|
||||
.end()
|
||||
}).listen(0, '127.0.0.1', () => {
|
||||
proxyServerPort = (proxyServer.address() as AddressInfo).port
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
after(() => {
|
||||
server.close()
|
||||
proxyServer.close()
|
||||
})
|
||||
|
||||
it('is emitted when navigating', async () => {
|
||||
const [user, pass] = ['user', 'pass']
|
||||
const w = new BrowserWindow({ show: false })
|
||||
let eventRequest: any
|
||||
let eventAuthInfo: any
|
||||
w.webContents.on('login', (event, request, authInfo, cb) => {
|
||||
eventRequest = request
|
||||
eventAuthInfo = authInfo
|
||||
event.preventDefault()
|
||||
cb(user, pass)
|
||||
})
|
||||
await w.loadURL(serverUrl)
|
||||
const body = await w.webContents.executeJavaScript(`document.documentElement.textContent`)
|
||||
expect(body).to.equal(`Basic ${Buffer.from(`${user}:${pass}`).toString('base64')}`)
|
||||
expect(eventRequest.url).to.equal(serverUrl + '/')
|
||||
expect(eventAuthInfo.isProxy).to.be.false()
|
||||
expect(eventAuthInfo.scheme).to.equal('basic')
|
||||
expect(eventAuthInfo.host).to.equal('127.0.0.1')
|
||||
expect(eventAuthInfo.port).to.equal(serverPort)
|
||||
expect(eventAuthInfo.realm).to.equal('Foo')
|
||||
})
|
||||
|
||||
it('is emitted when a proxy requests authorization', async () => {
|
||||
const customSession = session.fromPartition(`${Math.random()}`)
|
||||
await customSession.setProxy({ proxyRules: `127.0.0.1:${proxyServerPort}`, proxyBypassRules: '<-loopback>' })
|
||||
const [user, pass] = ['user', 'pass']
|
||||
const w = new BrowserWindow({ show: false, webPreferences: { session: customSession } })
|
||||
let eventRequest: any
|
||||
let eventAuthInfo: any
|
||||
w.webContents.on('login', (event, request, authInfo, cb) => {
|
||||
eventRequest = request
|
||||
eventAuthInfo = authInfo
|
||||
event.preventDefault()
|
||||
cb(user, pass)
|
||||
})
|
||||
await w.loadURL(`${serverUrl}/no-auth`)
|
||||
const body = await w.webContents.executeJavaScript(`document.documentElement.textContent`)
|
||||
expect(body).to.equal(`Basic ${Buffer.from(`${user}:${pass}`).toString('base64')}`)
|
||||
expect(eventRequest.url).to.equal(`${serverUrl}/no-auth`)
|
||||
expect(eventAuthInfo.isProxy).to.be.true()
|
||||
expect(eventAuthInfo.scheme).to.equal('basic')
|
||||
expect(eventAuthInfo.host).to.equal('127.0.0.1')
|
||||
expect(eventAuthInfo.port).to.equal(proxyServerPort)
|
||||
expect(eventAuthInfo.realm).to.equal('Foo')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user