Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dotnet/src/webdriver/BiDi/Communication/Broker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal Broker(BiDi bidi, Uri url)
new BrowserClientWindowConverter(),
new NavigationConverter(),
new InterceptConverter(_bidi),
new RequestConverter(_bidi),
new RequestConverter(),
new ChannelConverter(),
new HandleConverter(_bidi),
new InternalIdConverter(_bidi),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,11 @@ namespace OpenQA.Selenium.BiDi.Communication.Json.Converters;

internal class RequestConverter : JsonConverter<Request>
{
private readonly BiDi _bidi;

public RequestConverter(BiDi bidi)
{
_bidi = bidi;
}

public override Request? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var id = reader.GetString();

return new Request(_bidi, id!);
return new Request(id!);
}

public override void Write(Utf8JsonWriter writer, Request value, JsonSerializerOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,44 +25,50 @@ namespace OpenQA.Selenium.BiDi.Modules.BrowsingContext;

public class BrowsingContextNetworkModule(BrowsingContext context, NetworkModule networkModule)
{
public async Task<Intercept> InterceptRequestAsync(Func<BeforeRequestSentEventArgs, Task> handler, BrowsingContextAddInterceptOptions? interceptOptions = null, SubscriptionOptions? options = null)
public async Task<Intercept> InterceptRequestAsync(Func<InterceptedRequest, Task> handler, InterceptRequestOptions? options = null)
{
AddInterceptOptions addInterceptOptions = new(interceptOptions)
AddInterceptOptions addInterceptOptions = new(options)
{
Contexts = [context]
};

var intercept = await networkModule.AddInterceptAsync([InterceptPhase.BeforeRequestSent], addInterceptOptions).ConfigureAwait(false);

await intercept.OnBeforeRequestSentAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] }).ConfigureAwait(false);
await intercept.OnBeforeRequestSentAsync(
async req => await handler(new(req.BiDi, req.Context, req.IsBlocked, req.Navigation, req.RedirectCount, req.Request, req.Timestamp, req.Initiator)),
new BrowsingContextsSubscriptionOptions(null) { Contexts = [context] }).ConfigureAwait(false);

return intercept;
}

public async Task<Intercept> InterceptResponseAsync(Func<ResponseStartedEventArgs, Task> handler, BrowsingContextAddInterceptOptions? interceptOptions = null, SubscriptionOptions? options = null)
public async Task<Intercept> InterceptResponseAsync(Func<InterceptedResponse, Task> handler, InterceptResponseOptions? options = null)
{
AddInterceptOptions addInterceptOptions = new(interceptOptions)
AddInterceptOptions addInterceptOptions = new(options)
{
Contexts = [context]
};

var intercept = await networkModule.AddInterceptAsync([InterceptPhase.ResponseStarted], addInterceptOptions).ConfigureAwait(false);

await intercept.OnResponseStartedAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] }).ConfigureAwait(false);
await intercept.OnResponseStartedAsync(
async res => await handler(new(res.BiDi, res.Context, res.IsBlocked, res.Navigation, res.RedirectCount, res.Request, res.Timestamp, res.Response)),
new BrowsingContextsSubscriptionOptions(null) { Contexts = [context] }).ConfigureAwait(false);

return intercept;
}

public async Task<Intercept> InterceptAuthAsync(Func<AuthRequiredEventArgs, Task> handler, BrowsingContextAddInterceptOptions? interceptOptions = null, SubscriptionOptions? options = null)
public async Task<Intercept> InterceptAuthAsync(Func<InterceptedAuth, Task> handler, InterceptAuthOptions? options = null)
{
AddInterceptOptions addInterceptOptions = new(interceptOptions)
AddInterceptOptions addInterceptOptions = new(options)
{
Contexts = [context]
};

var intercept = await networkModule.AddInterceptAsync([InterceptPhase.AuthRequired], addInterceptOptions).ConfigureAwait(false);

await intercept.OnAuthRequiredAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] }).ConfigureAwait(false);
await intercept.OnAuthRequiredAsync(
async auth => await handler(new(auth.BiDi, auth.Context, auth.IsBlocked, auth.Navigation, auth.RedirectCount, auth.Request, auth.Timestamp, auth.Response)),
new BrowsingContextsSubscriptionOptions(null) { Contexts = [context] }).ConfigureAwait(false);

return intercept;
}
Expand Down Expand Up @@ -127,3 +133,9 @@ public Task<Subscription> OnAuthRequiredAsync(Action<AuthRequiredEventArgs> hand
return networkModule.OnAuthRequiredAsync(handler, new BrowsingContextsSubscriptionOptions(options) { Contexts = [context] });
}
}

public record InterceptRequestOptions : BrowsingContextAddInterceptOptions;

public record InterceptResponseOptions : BrowsingContextAddInterceptOptions;

public record InterceptAuthOptions : BrowsingContextAddInterceptOptions;
106 changes: 106 additions & 0 deletions dotnet/src/webdriver/BiDi/Modules/Network/NetworkModule.HighLevel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// <copyright file="NetworkModule.HighLevel.cs" company="Selenium Committers">
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// </copyright>

using System;
using System.Threading.Tasks;

namespace OpenQA.Selenium.BiDi.Modules.Network;

public partial class NetworkModule
{
public async Task<Intercept> InterceptRequestAsync(Func<InterceptedRequest, Task> handler, InterceptRequestOptions? options = null)
{
var intercept = await AddInterceptAsync([InterceptPhase.BeforeRequestSent], options).ConfigureAwait(false);

await intercept.OnBeforeRequestSentAsync(async req => await handler(new(req.BiDi, req.Context, req.IsBlocked, req.Navigation, req.RedirectCount, req.Request, req.Timestamp, req.Initiator))).ConfigureAwait(false);

return intercept;
}

public async Task<Intercept> InterceptResponseAsync(Func<InterceptedResponse, Task> handler, InterceptResponseOptions? options = null)
{
var intercept = await AddInterceptAsync([InterceptPhase.ResponseStarted], options).ConfigureAwait(false);

await intercept.OnResponseStartedAsync(async res => await handler(new(res.BiDi, res.Context, res.IsBlocked, res.Navigation, res.RedirectCount, res.Request, res.Timestamp, res.Response))).ConfigureAwait(false);

return intercept;
}

public async Task<Intercept> InterceptAuthAsync(Func<InterceptedAuth, Task> handler, InterceptAuthOptions? options = null)
{
var intercept = await AddInterceptAsync([InterceptPhase.AuthRequired], options).ConfigureAwait(false);

await intercept.OnAuthRequiredAsync(async auth => await handler(new(auth.BiDi, auth.Context, auth.IsBlocked, auth.Navigation, auth.RedirectCount, auth.Request, auth.Timestamp, auth.Response))).ConfigureAwait(false);

return intercept;
}
}

public record InterceptRequestOptions : AddInterceptOptions;

public record InterceptResponseOptions : AddInterceptOptions;

public record InterceptAuthOptions : AddInterceptOptions;

public record InterceptedRequest(BiDi BiDi, BrowsingContext.BrowsingContext Context, bool IsBlocked, BrowsingContext.Navigation Navigation, long RedirectCount, RequestData Request, DateTimeOffset Timestamp, Initiator Initiator)
: BeforeRequestSentEventArgs(BiDi, Context, IsBlocked, Navigation, RedirectCount, Request, Timestamp, Initiator)
{
public Task ContinueAsync(ContinueRequestOptions? options = null)
{
return BiDi.Network.ContinueRequestAsync(Request.Request, options);
}

public Task FailAsync()
{
return BiDi.Network.FailRequestAsync(Request.Request);
}

public Task ProvideResponseAsync(ProvideResponseOptions? options = null)
{
return BiDi.Network.ProvideResponseAsync(Request.Request, options);
}
}

public record InterceptedResponse(BiDi BiDi, BrowsingContext.BrowsingContext Context, bool IsBlocked, BrowsingContext.Navigation Navigation, long RedirectCount, RequestData Request, DateTimeOffset Timestamp, ResponseData Response)
: ResponseStartedEventArgs(BiDi, Context, IsBlocked, Navigation, RedirectCount, Request, Timestamp, Response)
{
public Task ContinueResponseAsync(ContinueResponseOptions? options = null)
{
return BiDi.Network.ContinueResponseAsync(Request.Request, options);
}
}

public record InterceptedAuth(BiDi BiDi, BrowsingContext.BrowsingContext Context, bool IsBlocked, BrowsingContext.Navigation Navigation, long RedirectCount, RequestData Request, DateTimeOffset Timestamp, ResponseData Response)
: AuthRequiredEventArgs(BiDi, Context, IsBlocked, Navigation, RedirectCount, Request, Timestamp, Response)
{
public Task ContinueWithAuthAsync(AuthCredentials credentials, ContinueWithAuthCredentialsOptions? options = null)
{
return BiDi.Network.ContinueWithAuthAsync(Request.Request, credentials, options);
}

public Task ContinueWithAuthAsync(ContinueWithAuthDefaultCredentialsOptions? options = null)
{
return BiDi.Network.ContinueWithAuthAsync(Request.Request, options);
}

public Task ContinueWithAuthAsync(ContinueWithAuthCancelCredentialsOptions? options = null)
{
return BiDi.Network.ContinueWithAuthAsync(Request.Request, options);
}
}
29 changes: 1 addition & 28 deletions dotnet/src/webdriver/BiDi/Modules/Network/NetworkModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace OpenQA.Selenium.BiDi.Modules.Network;

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

public async Task<Intercept> InterceptRequestAsync(Func<BeforeRequestSentEventArgs, Task> handler, AddInterceptOptions? interceptOptions = null, SubscriptionOptions? options = null)
{
var intercept = await AddInterceptAsync([InterceptPhase.BeforeRequestSent], interceptOptions).ConfigureAwait(false);

await intercept.OnBeforeRequestSentAsync(handler, options).ConfigureAwait(false);

return intercept;
}

public async Task<Intercept> InterceptResponseAsync(Func<ResponseStartedEventArgs, Task> handler, AddInterceptOptions? interceptOptions = null, SubscriptionOptions? options = null)
{
var intercept = await AddInterceptAsync([InterceptPhase.ResponseStarted], interceptOptions).ConfigureAwait(false);

await intercept.OnResponseStartedAsync(handler, options).ConfigureAwait(false);

return intercept;
}

public async Task SetCacheBehaviorAsync(CacheBehavior behavior, SetCacheBehaviorOptions? options = null)
{
var @params = new SetCacheBehaviorCommandParameters(behavior, options?.Contexts);

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

public async Task<Intercept> InterceptAuthAsync(Func<AuthRequiredEventArgs, Task> handler, AddInterceptOptions? interceptOptions = null, SubscriptionOptions? options = null)
{
var intercept = await AddInterceptAsync([InterceptPhase.AuthRequired], interceptOptions).ConfigureAwait(false);

await intercept.OnAuthRequiredAsync(handler, options).ConfigureAwait(false);

return intercept;
}

internal async Task ContinueRequestAsync(Request request, ContinueRequestOptions? options = null)
{
var @params = new ContinueRequestCommandParameters(request, options?.Body, options?.Cookies, options?.Headers, options?.Method, options?.Url);
Expand Down
42 changes: 1 addition & 41 deletions dotnet/src/webdriver/BiDi/Modules/Network/Request.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,14 @@
// under the License.
// </copyright>

using System.Threading.Tasks;

namespace OpenQA.Selenium.BiDi.Modules.Network;

public class Request
{
private readonly BiDi _bidi;

internal Request(BiDi bidi, string id)
internal Request(string id)
{
_bidi = bidi;
Id = id;
}

public string Id { get; private set; }

public Task ContinueAsync(ContinueRequestOptions? options = null)
{
return _bidi.Network.ContinueRequestAsync(this, options);
}

public Task FailAsync()
{
return _bidi.Network.FailRequestAsync(this);
}

public Task ProvideResponseAsync(ProvideResponseOptions? options = null)
{
return _bidi.Network.ProvideResponseAsync(this, options);
}

public Task ContinueResponseAsync(ContinueResponseOptions? options = null)
{
return _bidi.Network.ContinueResponseAsync(this, options);
}

public Task ContinueWithAuthAsync(AuthCredentials credentials, ContinueWithAuthCredentialsOptions? options = null)
{
return _bidi.Network.ContinueWithAuthAsync(this, credentials, options);
}

public Task ContinueWithAuthAsync(ContinueWithAuthDefaultCredentialsOptions? options = null)
{
return _bidi.Network.ContinueWithAuthAsync(this, options);
}

public Task ContinueWithAuthAsync(ContinueWithAuthCancelCredentialsOptions? options = null)
{
return _bidi.Network.ContinueWithAuthAsync(this, options);
}
}
Loading