Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/lib/network.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
const { Network: getNetwork } = require('../bidi/network')
19
const { InterceptPhase } = require('../bidi/interceptPhase')
20
const { AddInterceptParameters } = require('../bidi/addInterceptParameters')
21
22
class Network {
23
#callbackId = 0
24
#driver
25
#network
26
#authHandlers = new Map()
27
28
constructor(driver) {
29
this.#driver = driver
30
}
31
32
// This should be done in the constructor.
33
// But since it needs to call async methods we cannot do that in the constructor.
34
// We can have a separate async method that initialises the Network instance.
35
// However, that pattern does not allow chaining the methods as we would like the user to use it.
36
// Since it involves awaiting to get the instance and then another await to call the method.
37
// Using this allows the user to do this "await driver.network.addAuthenticationHandler(callback)"
38
async #init() {
39
if (this.#network !== undefined) {
40
return
41
}
42
this.#network = await getNetwork(this.#driver)
43
44
await this.#network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
45
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
}
54
55
await this.#network.continueWithAuthNoCredentials(requestId)
56
})
57
}
58
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 = '//') {
68
await this.#init()
69
70
const id = this.#callbackId++
71
72
this.#authHandlers.set(id, { username, password, uri })
73
return id
74
}
75
76
async removeAuthenticationHandler(id) {
77
await this.#init()
78
79
if (this.#authHandlers.has(id)) {
80
this.#authHandlers.delete(id)
81
} else {
82
throw Error(`Callback with id ${id} not found`)
83
}
84
}
85
86
async clearAuthenticationHandlers() {
87
this.#authHandlers.clear()
88
}
89
}
90
91
module.exports = Network
92
93