Skip to content

Commit 1ef54a6

Browse files
committed
Fixing .NET HttpCommandExecutor event for RemoteWebDriver
This commit moves the firing of the event raised before sending a remote HTTP request to before the setting of the request body. This allows the request settings to be properly set without throwing an exeception. This commit also renames the event and event args object to `SendingReomteHttpRequest` and `SendingRemoteHttpRequestEventArgs`, respectively. The previous event and event args are marked as deprecated for removal in a future release, and the previously named event is now a no-op, and will never be fired, even if a handler is attached.
1 parent fe48fc4 commit 1ef54a6

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

dotnet/src/webdriver/Remote/BeforeRemoteHttpRequestEventArgs.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace OpenQA.Selenium.Remote
2323
{
24+
[Obsolete("Replaced by the SendingRemoteHttpRequestEventArgs class")]
2425
public class BeforeRemoteHttpRequestEventArgs : EventArgs
2526
{
2627
private HttpWebRequest request;

dotnet/src/webdriver/Remote/HttpCommandExecutor.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,11 @@ public HttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout, bool ena
9191

9292
}
9393

94+
[Obsolete("Replaced by the SendingRemoteHttpRequest event. This event is a no-op, and event handlers for it will no longer be called.")]
9495
public event EventHandler<BeforeRemoteHttpRequestEventArgs> BeforeRemoteHttpRequest;
9596

97+
public event EventHandler<SendingRemoteHttpRequestEventArgs> SendingRemoteHttpRequest;
98+
9699
/// <summary>
97100
/// Gets the repository of objects containin information about commands.
98101
/// </summary>
@@ -136,19 +139,19 @@ public virtual Response Execute(Command commandToExecute)
136139
}
137140

138141
/// <summary>
139-
/// Raises the <see cref="BeforeRemoteHttpRequest"/> event.
142+
/// Raises the <see cref="SendingRemoteHttpRequest"/> event.
140143
/// </summary>
141-
/// <param name="eventArgs">A <see cref="BeforeRemoteHttpRequestEventArgs"/> that contains the event data.</param>
142-
protected virtual void OnBeforeRemoteHttpRequest(BeforeRemoteHttpRequestEventArgs eventArgs)
144+
/// <param name="eventArgs">A <see cref="SendingRemoteHttpRequestEventArgs"/> that contains the event data.</param>
145+
protected virtual void OnSendingRemoteHttpRequest(SendingRemoteHttpRequestEventArgs eventArgs)
143146
{
144147
if (eventArgs == null)
145148
{
146149
throw new ArgumentNullException("eventArgs", "eventArgs must not be null");
147150
}
148151

149-
if (this.BeforeRemoteHttpRequest != null)
152+
if (this.SendingRemoteHttpRequest != null)
150153
{
151-
this.BeforeRemoteHttpRequest(this, eventArgs);
154+
this.SendingRemoteHttpRequest(this, eventArgs);
152155
}
153156
}
154157

@@ -188,21 +191,23 @@ private HttpResponseInfo MakeHttpRequest(HttpRequestInfo requestInfo)
188191
request.KeepAlive = this.enableKeepAlive;
189192
request.Proxy = null;
190193
request.ServicePoint.ConnectionLimit = 2000;
194+
if (request.Method == CommandInfo.GetCommand)
195+
{
196+
request.Headers.Add("Cache-Control", "no-cache");
197+
}
198+
199+
SendingRemoteHttpRequestEventArgs eventArgs = new SendingRemoteHttpRequestEventArgs(request, requestInfo.RequestBody);
200+
this.OnSendingRemoteHttpRequest(eventArgs);
201+
191202
if (request.Method == CommandInfo.PostCommand)
192203
{
193-
string payload = requestInfo.RequestBody;
204+
string payload = eventArgs.RequestBody;
194205
byte[] data = Encoding.UTF8.GetBytes(payload);
195206
request.ContentType = ContentTypeHeader;
196207
Stream requestStream = request.GetRequestStream();
197208
requestStream.Write(data, 0, data.Length);
198209
requestStream.Close();
199210
}
200-
else if (request.Method == CommandInfo.GetCommand)
201-
{
202-
request.Headers.Add("Cache-Control", "no-cache");
203-
}
204-
205-
this.OnBeforeRemoteHttpRequest(new BeforeRemoteHttpRequestEventArgs(request));
206211

207212
HttpResponseInfo responseInfo = new HttpResponseInfo();
208213
HttpWebResponse webResponse = null;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// <copyright file="SendingRemoteHttpRequestEventArgs.cs" company="WebDriver 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 "License");
7+
// you may not use this file except in compliance with the License.
8+
// 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, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// </copyright>
18+
19+
using System;
20+
using System.Net;
21+
22+
namespace OpenQA.Selenium.Remote
23+
{
24+
public class SendingRemoteHttpRequestEventArgs : EventArgs
25+
{
26+
private HttpWebRequest request;
27+
private string requestBody;
28+
29+
public SendingRemoteHttpRequestEventArgs(HttpWebRequest request, string requestBody)
30+
{
31+
this.request = request;
32+
this.requestBody = requestBody;
33+
}
34+
35+
public HttpWebRequest Request
36+
{
37+
get { return this.request; }
38+
}
39+
40+
public string RequestBody
41+
{
42+
get { return this.requestBody; }
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)