Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/devtools/networkinterceptor.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
class HttpResponse {
19
/**
20
* Creates a HTTP Response that will be used to
21
* mock out network interceptions.
22
* @param {*} urlToIntercept
23
*/
24
constructor(urlToIntercept = '') {
25
this.returnBody = ''
26
this.returnHeaders = []
27
this.returnMethod = 'GET'
28
this.returnStatus = 200
29
this.urlToIntercept = urlToIntercept
30
}
31
32
/**
33
* Add headers that will be returned when we intercept
34
* a HTTP Request
35
* @param {*} header
36
* @param {*} value
37
*/
38
addHeaders(header, value) {
39
this.returnHeaders.push({ name: header, value: value })
40
}
41
42
get headers() {
43
return this.returnHeaders
44
}
45
46
/**
47
* Set the STATUS value of the returned HTTP Request
48
* @param {*} value
49
*/
50
set status(value) {
51
// Add in check that his should be a number
52
this.returnStatus = value
53
}
54
55
get status() {
56
return this.returnStatus
57
}
58
59
/**
60
* Sets the value of the body of the HTTP Request that
61
* will be returned.
62
* @param {*} value
63
*/
64
set body(value) {
65
this.returnBody = value
66
}
67
68
get body() {
69
let buff = Buffer.from(this.returnBody, 'utf-8')
70
return buff.toString('base64')
71
}
72
73
/**
74
* Sets the method of the HTTP Request
75
* @param {*} value the method of the request.
76
*/
77
set method(value) {
78
this.returnMethod = value
79
}
80
81
/**
82
* Returns the Method to be used in the intercept
83
*/
84
get method() {
85
return this.returnMethod
86
}
87
}
88
89
exports.HttpResponse = HttpResponse
90
91