-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
/
Copy pathHttpCommandInfo.cs
134 lines (118 loc) · 5.27 KB
/
HttpCommandInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// <copyright file="HttpCommandInfo.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.Globalization;
namespace OpenQA.Selenium
{
/// <summary>
/// Provides the execution information for a <see cref="DriverCommand"/>.
/// </summary>
public class HttpCommandInfo : CommandInfo
{
/// <summary>
/// POST verb for the command info
/// </summary>
public const string PostCommand = "POST";
/// <summary>
/// GET verb for the command info
/// </summary>
public const string GetCommand = "GET";
/// <summary>
/// DELETE verb for the command info
/// </summary>
public const string DeleteCommand = "DELETE";
private const string SessionIdPropertyName = "sessionId";
/// <summary>
/// Initializes a new instance of the <see cref="HttpCommandInfo"/> class
/// </summary>
/// <param name="method">Method of the Command</param>
/// <param name="resourcePath">Relative URL path to the resource used to execute the command</param>
public HttpCommandInfo(string method, string resourcePath)
{
this.ResourcePath = resourcePath;
this.Method = method;
}
/// <summary>
/// Gets the URL representing the path to the resource.
/// </summary>
public string ResourcePath { get; }
/// <summary>
/// Gets the HTTP method associated with the command.
/// </summary>
public string Method { get; }
/// <summary>
/// Gets the unique identifier for this command within the scope of its protocol definition
/// </summary>
public override string CommandIdentifier
{
get { return string.Format(CultureInfo.InvariantCulture, "{0} {1}", this.Method, this.ResourcePath); }
}
/// <summary>
/// Creates the full URI associated with this command, substituting command
/// parameters for tokens in the URI template.
/// </summary>
/// <param name="baseUri">The base URI associated with the command.</param>
/// <param name="commandToExecute">The command containing the parameters with which
/// to substitute the tokens in the template.</param>
/// <returns>The full URI for the command, with the parameters of the command
/// substituted for the tokens in the template.</returns>
public Uri CreateCommandUri(Uri baseUri, Command commandToExecute)
{
string[] urlParts = this.ResourcePath.Split(["/"], StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < urlParts.Length; i++)
{
string urlPart = urlParts[i];
if (urlPart.StartsWith("{", StringComparison.OrdinalIgnoreCase) && urlPart.EndsWith("}", StringComparison.OrdinalIgnoreCase))
{
urlParts[i] = GetCommandPropertyValue(urlPart, commandToExecute);
}
}
string relativeUrlString = string.Join("/", urlParts);
Uri relativeUri = new Uri(relativeUrlString, UriKind.Relative);
if (!Uri.TryCreate(baseUri, relativeUri, out Uri? fullUri))
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unable to create URI from base {0} and relative path {1}", baseUri?.ToString(), relativeUrlString));
}
return fullUri;
}
private static string GetCommandPropertyValue(string propertyName, Command commandToExecute)
{
string propertyValue = string.Empty;
// Strip the curly braces
propertyName = propertyName.Substring(1, propertyName.Length - 2);
if (propertyName == SessionIdPropertyName)
{
if (commandToExecute.SessionId != null)
{
propertyValue = commandToExecute.SessionId.ToString();
}
}
else if (commandToExecute.HasParameters())
{
// Extract the URL parameter, and remove it from the parameters dictionary
// so it doesn't get transmitted as a JSON parameter.
if (commandToExecute.TryGetValueAndRemoveIfNotNull(propertyName, out var propertyValueObject))
{
propertyValue = propertyValueObject.ToString()!;
}
}
return propertyValue;
}
}
}