Skip to content

Commit 02ecaed

Browse files
committed
[dotnet] [bidi] Combine network interception to apply rules
1 parent ecb0dbf commit 02ecaed

File tree

3 files changed

+61
-29
lines changed

3 files changed

+61
-29
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// <copyright file="NetworkModule.HighLevel.cs" company="Selenium Committers">
2+
// Licensed to the Software Freedom Conservancy (SFC) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The SFC licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
// </copyright>
19+
20+
using System;
21+
using System.Threading.Tasks;
22+
23+
namespace OpenQA.Selenium.BiDi.Modules.Network;
24+
25+
public partial class NetworkModule
26+
{
27+
public async Task<Intercept> InterceptRequestAsync(Func<BeforeRequestSentEventArgs, Task> handler, InterceptRequestOptions? options = null)
28+
{
29+
var intercept = await AddInterceptAsync([InterceptPhase.BeforeRequestSent], options).ConfigureAwait(false);
30+
31+
await intercept.OnBeforeRequestSentAsync(handler).ConfigureAwait(false);
32+
33+
return intercept;
34+
}
35+
36+
public async Task<Intercept> InterceptResponseAsync(Func<ResponseStartedEventArgs, Task> handler, InterceptResponseOptions? options = null)
37+
{
38+
var intercept = await AddInterceptAsync([InterceptPhase.ResponseStarted], options).ConfigureAwait(false);
39+
40+
await intercept.OnResponseStartedAsync(handler).ConfigureAwait(false);
41+
42+
return intercept;
43+
}
44+
45+
public async Task<Intercept> InterceptAuthAsync(Func<AuthRequiredEventArgs, Task> handler, InterceptAuthOptions? options = null)
46+
{
47+
var intercept = await AddInterceptAsync([InterceptPhase.AuthRequired], options).ConfigureAwait(false);
48+
49+
await intercept.OnAuthRequiredAsync(handler).ConfigureAwait(false);
50+
51+
return intercept;
52+
}
53+
}
54+
55+
public record InterceptRequestOptions : AddInterceptOptions;
56+
57+
public record InterceptResponseOptions : AddInterceptOptions;
58+
59+
public record InterceptAuthOptions : AddInterceptOptions;

dotnet/src/webdriver/BiDi/Modules/Network/NetworkModule.cs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
namespace OpenQA.Selenium.BiDi.Modules.Network;
2626

27-
public sealed class NetworkModule(Broker broker) : Module(broker)
27+
public sealed partial class NetworkModule(Broker broker) : Module(broker)
2828
{
2929
internal async Task<Intercept> AddInterceptAsync(IEnumerable<InterceptPhase> phases, AddInterceptOptions? options = null)
3030
{
@@ -42,40 +42,13 @@ internal async Task RemoveInterceptAsync(Intercept intercept, RemoveInterceptOpt
4242
await Broker.ExecuteCommandAsync(new RemoveInterceptCommand(@params), options).ConfigureAwait(false);
4343
}
4444

45-
public async Task<Intercept> InterceptRequestAsync(Func<BeforeRequestSentEventArgs, Task> handler, AddInterceptOptions? interceptOptions = null, SubscriptionOptions? options = null)
46-
{
47-
var intercept = await AddInterceptAsync([InterceptPhase.BeforeRequestSent], interceptOptions).ConfigureAwait(false);
48-
49-
await intercept.OnBeforeRequestSentAsync(handler, options).ConfigureAwait(false);
50-
51-
return intercept;
52-
}
53-
54-
public async Task<Intercept> InterceptResponseAsync(Func<ResponseStartedEventArgs, Task> handler, AddInterceptOptions? interceptOptions = null, SubscriptionOptions? options = null)
55-
{
56-
var intercept = await AddInterceptAsync([InterceptPhase.ResponseStarted], interceptOptions).ConfigureAwait(false);
57-
58-
await intercept.OnResponseStartedAsync(handler, options).ConfigureAwait(false);
59-
60-
return intercept;
61-
}
62-
6345
public async Task SetCacheBehaviorAsync(CacheBehavior behavior, SetCacheBehaviorOptions? options = null)
6446
{
6547
var @params = new SetCacheBehaviorCommandParameters(behavior, options?.Contexts);
6648

6749
await Broker.ExecuteCommandAsync(new SetCacheBehaviorCommand(@params), options).ConfigureAwait(false);
6850
}
6951

70-
public async Task<Intercept> InterceptAuthAsync(Func<AuthRequiredEventArgs, Task> handler, AddInterceptOptions? interceptOptions = null, SubscriptionOptions? options = null)
71-
{
72-
var intercept = await AddInterceptAsync([InterceptPhase.AuthRequired], interceptOptions).ConfigureAwait(false);
73-
74-
await intercept.OnAuthRequiredAsync(handler, options).ConfigureAwait(false);
75-
76-
return intercept;
77-
}
78-
7952
internal async Task ContinueRequestAsync(Request request, ContinueRequestOptions? options = null)
8053
{
8154
var @params = new ContinueRequestCommandParameters(request, options?.Body, options?.Cookies, options?.Headers, options?.Method, options?.Url);

dotnet/test/common/BiDi/Network/NetworkTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public async Task CanAddInterceptStringUrlPattern()
5151
[Test]
5252
public async Task CanAddInterceptUrlPattern()
5353
{
54-
await using var intercept = await bidi.Network.InterceptRequestAsync(e => Task.CompletedTask, interceptOptions: new()
54+
await using var intercept = await bidi.Network.InterceptRequestAsync(e => Task.CompletedTask, options: new()
5555
{
5656
UrlPatterns = [new PatternUrlPattern()
5757
{

0 commit comments

Comments
 (0)