Skip to content

Commit 9cd48b7

Browse files
committed
[bidi][js] Allow passing in uri for authentication handlers
1 parent 9bd82f2 commit 9cd48b7

File tree

2 files changed

+71
-22
lines changed

2 files changed

+71
-22
lines changed

javascript/node/selenium-webdriver/lib/network.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ const { InterceptPhase } = require('../bidi/interceptPhase')
2020
const { AddInterceptParameters } = require('../bidi/addInterceptParameters')
2121

2222
class Network {
23+
#callbackId = 0
2324
#driver
2425
#network
25-
#callBackInterceptIdMap = new Map()
26+
#authHandlers = new Map()
2627

2728
constructor(driver) {
2829
this.#driver = driver
@@ -39,39 +40,46 @@ class Network {
3940
return
4041
}
4142
this.#network = await network(this.#driver)
42-
}
4343

44-
async addAuthenticationHandler(username, password) {
45-
await this.#init()
44+
await this.#network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
4645

47-
const interceptId = await this.#network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
46+
await this.#network.authRequired(async (event) => {
47+
const requestId = event.request.request
48+
const uri = event.request.url
49+
const credentials = this.getAuthCredentials(uri)
50+
if (credentials !== null) {
51+
await this.#network.continueWithAuth(requestId, credentials.username, credentials.password)
52+
return
53+
}
4854

49-
const id = await this.#network.authRequired(async (event) => {
50-
await this.#network.continueWithAuth(event.request.request, username, password)
55+
await this.#network.continueWithAuthNoCredentials(requestId)
5156
})
52-
53-
this.#callBackInterceptIdMap.set(id, interceptId)
54-
return id
5557
}
5658

57-
async removeAuthenticationHandler(id) {
59+
getAuthCredentials(uri) {
60+
for (let [, value] of this.#authHandlers) {
61+
if (uri.match(value.uri)) {
62+
return value
63+
}
64+
}
65+
return null
66+
}
67+
async addAuthenticationHandler(username, password, uri = '//') {
5868
await this.#init()
5969

60-
const interceptId = this.#callBackInterceptIdMap.get(id)
70+
const id = this.#callbackId++
6171

62-
await this.#network.removeIntercept(interceptId)
63-
await this.#network.removeCallback(id)
72+
this.#authHandlers.set(id, { username, password, uri })
73+
return id
74+
}
6475

65-
this.#callBackInterceptIdMap.delete(id)
76+
async removeAuthenticationHandler(id) {
77+
await this.#init()
78+
this.#authHandlers.delete(id)
6679
}
6780

6881
async clearAuthenticationHandlers() {
69-
for (const [key, value] of this.#callBackInterceptIdMap.entries()) {
70-
await this.#network.removeIntercept(value)
71-
await this.#network.removeCallback(key)
72-
}
73-
74-
this.#callBackInterceptIdMap.clear()
82+
this.#authHandlers.clear()
7583
}
7684
}
7785

javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,35 @@ suite(
4545
assert.equal(source.includes('Access granted'), true)
4646
})
4747

48+
it('can add authentication handler with filter', async function () {
49+
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
50+
await driver.get(Pages.basicAuth)
51+
52+
await driver.wait(until.elementLocated(By.css('pre')))
53+
let source = await driver.getPageSource()
54+
assert.equal(source.includes('Access granted'), true)
55+
})
56+
57+
it('can add multiple authentication handlers with filter', async function () {
58+
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
59+
await driver.network().addAuthenticationHandler('test', 'test', 'test')
60+
await driver.get(Pages.basicAuth)
61+
62+
await driver.wait(until.elementLocated(By.css('pre')))
63+
let source = await driver.getPageSource()
64+
assert.equal(source.includes('Access granted'), true)
65+
})
66+
67+
it('can add multiple authentication handlers with the same filter', async function () {
68+
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
69+
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
70+
await driver.get(Pages.basicAuth)
71+
72+
await driver.wait(until.elementLocated(By.css('pre')))
73+
let source = await driver.getPageSource()
74+
assert.equal(source.includes('Access granted'), true)
75+
})
76+
4877
it('can remove authentication handler', async function () {
4978
const id = await driver.network().addAuthenticationHandler('genie', 'bottle')
5079

@@ -59,8 +88,20 @@ suite(
5988
}
6089
})
6190

91+
it('can remove authentication handler', async function () {
92+
await driver.network().removeAuthenticationHandler(10)
93+
94+
try {
95+
await driver.get(Pages.basicAuth)
96+
await driver.wait(until.elementLocated(By.css('pre')))
97+
assert.fail('Page should not be loaded')
98+
} catch (e) {
99+
assert.strictEqual(e.name, 'UnexpectedAlertOpenError')
100+
}
101+
})
102+
62103
it('can clear authentication handlers', async function () {
63-
await driver.network().addAuthenticationHandler('genie', 'bottle')
104+
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
64105

65106
await driver.network().addAuthenticationHandler('bottle', 'genie')
66107

0 commit comments

Comments
 (0)