Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/bidi/intercepted_response_spec.rb
4079 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
require File.expand_path('../spec_helper', __dir__)
21
require File.expand_path('../../../../../lib/selenium/webdriver/bidi/network/cookies', __dir__)
22
require File.expand_path('../../../../../lib/selenium/webdriver/bidi/network/headers', __dir__)
23
require File.expand_path('../../../../../lib/selenium/webdriver/bidi/network/credentials', __dir__)
24
require File.expand_path('../../../../../lib/selenium/webdriver/bidi/network/intercepted_response', __dir__)
25
26
module Selenium
27
module WebDriver
28
class BiDi
29
describe InterceptedResponse do
30
let(:mock_network) { instance_double(Network) }
31
let(:response_id) { 'resp-456' }
32
let(:intercepted_response) { described_class.new(mock_network, {'request' => response_id}) }
33
let(:mock_headers_data) { [{name: 'Content-Type', value: 'application/json'}] }
34
let(:mock_cookies_data) { [{name: 'session', value: {type: 'string', value: 'abc'}}] }
35
let(:mock_body_string) { '{"message": "Access Denied"}' }
36
37
before do
38
allow(intercepted_response).to receive(:id).and_return(response_id)
39
end
40
41
describe 'Initialization and Default State' do
42
it 'has an empty hash as headers by default' do
43
expect(intercepted_response.headers).to be_empty
44
end
45
46
it 'has an empty hash as cookies by default' do
47
expect(intercepted_response.cookies).to be_empty
48
end
49
end
50
51
describe '#continue' do
52
before do
53
allow(mock_network).to receive(:continue_response)
54
end
55
56
it 'sends nil headers and cookies when not explicitly set' do
57
expected_payload = {id: response_id, cookies: nil, headers: nil, credentials: nil, reason: nil, status: nil}
58
intercepted_response.continue
59
60
expect(mock_network).to have_received(:continue_response).with(expected_payload)
61
end
62
63
it 'sends headers payload and uses default nil cookies/credentials' do
64
intercepted_response.headers = mock_headers_data
65
reason = 'Custom Reason'
66
status = 201
67
68
expected_payload = {
69
id: response_id,
70
cookies: nil,
71
headers: Headers.new(mock_headers_data).as_json,
72
credentials: nil,
73
reason: reason,
74
status: status
75
}
76
intercepted_response.reason = reason
77
intercepted_response.status = status
78
intercepted_response.continue
79
80
expect(mock_network).to have_received(:continue_response).with(expected_payload)
81
end
82
83
it 'sends full custom payload when all fields are set' do
84
credentials = Credentials.new(username: 'user', password: 'pass')
85
86
intercepted_response.headers = mock_headers_data
87
intercepted_response.cookies = mock_cookies_data
88
intercepted_response.credentials(username: 'user', password: 'pass')
89
intercepted_response.reason = 'Test Reason'
90
intercepted_response.status = 404
91
92
expected_payload = {
93
id: response_id,
94
cookies: Cookies.new(mock_cookies_data).as_json,
95
headers: Headers.new(mock_headers_data).as_json,
96
credentials: credentials.as_json,
97
reason: 'Test Reason',
98
status: 404
99
}
100
101
intercepted_response.continue
102
103
expect(mock_network).to have_received(:continue_response).with(expected_payload)
104
end
105
end
106
107
describe '#provide_response' do
108
before do
109
allow(mock_network).to receive(:provide_response)
110
end
111
112
it 'sends nil headers, cookies, and body when not explicitly set' do
113
expected_payload = {id: response_id, cookies: nil, headers: nil, body: nil, reason: nil, status: nil}
114
intercepted_response.provide_response
115
116
expect(mock_network).to have_received(:provide_response).with(expected_payload)
117
end
118
119
it 'sends body payload and uses default [] cookies/headers' do
120
intercepted_response.body = mock_body_string
121
reason = 'Provided Success'
122
status = 200
123
124
expected_payload = {
125
id: response_id,
126
cookies: nil,
127
headers: nil,
128
body: {type: 'string', value: mock_body_string.to_json},
129
reason: reason,
130
status: status
131
}
132
intercepted_response.reason = reason
133
intercepted_response.status = status
134
intercepted_response.provide_response
135
136
expect(mock_network).to have_received(:provide_response).with(expected_payload)
137
end
138
139
it 'sends full custom payload when all fields are set' do
140
intercepted_response.headers = mock_headers_data
141
intercepted_response.cookies = mock_cookies_data
142
intercepted_response.body = mock_body_string
143
intercepted_response.reason = 'Forbidden'
144
intercepted_response.status = 403
145
146
expected_payload = {
147
id: response_id,
148
cookies: Cookies.new(mock_cookies_data).as_json,
149
headers: Headers.new(mock_headers_data).as_json,
150
body: {type: 'string', value: mock_body_string.to_json},
151
reason: 'Forbidden',
152
status: 403
153
}
154
155
intercepted_response.provide_response
156
157
expect(mock_network).to have_received(:provide_response).with(expected_payload)
158
end
159
end
160
161
describe 'setters/getters' do
162
it '#credentials' do
163
credentials = intercepted_response.credentials(username: 'u', password: 'p')
164
expect(credentials).to be_a(Credentials)
165
expect(intercepted_response.credentials).to be(credentials)
166
end
167
168
it '#headers=' do
169
intercepted_response.headers = mock_headers_data
170
expect(intercepted_response.headers).to be_a(Headers)
171
end
172
173
it '#cookies=' do
174
intercepted_response.cookies = mock_cookies_data
175
first_cookies = intercepted_response.cookies
176
intercepted_response.cookies = [{name: 'c2', value: 'v2'}]
177
second_cookies = intercepted_response.cookies
178
expect(first_cookies).to be(second_cookies)
179
end
180
181
it '#body=' do
182
data = {key: 'value', number: 123}
183
intercepted_response.body = data
184
expected_body = {
185
type: 'string',
186
value: data.to_json
187
}
188
expect(intercepted_response.body).to eq(expected_body)
189
end
190
end
191
end
192
end
193
end
194
end
195
196