|
18 | 18 |
|
19 | 19 | using System;
|
20 | 20 | using System.Net;
|
| 21 | +using System.Net.Http; |
21 | 22 | using System.Text;
|
| 23 | +using System.Threading.Tasks; |
22 | 24 | using OpenQA.Selenium.Internal;
|
23 | 25 |
|
24 | 26 | namespace OpenQA.Selenium.Safari
|
@@ -110,47 +112,41 @@ protected override bool IsInitialized
|
110 | 112 | get
|
111 | 113 | {
|
112 | 114 | bool isInitialized = false;
|
113 |
| - try |
114 |
| - { |
115 |
| - // Since Firefox driver won't implement the /session end point (because |
116 |
| - // the W3C spec working group stupidly decided that it isn't necessary), |
117 |
| - // we'll attempt to poll for a different URL which has no side effects. |
118 |
| - // We've chosen to poll on the "quit" URL, passing in a nonexistent |
119 |
| - // session id. |
120 |
| - Uri serviceHealthUri = new Uri(this.ServiceUrl, new Uri("/session/FakeSessionIdForPollingPurposes", UriKind.Relative)); |
121 |
| - HttpWebRequest request = HttpWebRequest.Create(serviceHealthUri) as HttpWebRequest; |
122 |
| - request.KeepAlive = false; |
123 |
| - request.Timeout = 5000; |
124 |
| - request.Method = "DELETE"; |
125 |
| - HttpWebResponse response = request.GetResponse() as HttpWebResponse; |
126 |
| - |
127 |
| - // Checking the response from deleting a nonexistent session. Note that we are simply |
128 |
| - // checking that the HTTP status returned is a 200 status, and that the resposne has |
129 |
| - // the correct Content-Type header. A more sophisticated check would parse the JSON |
130 |
| - // response and validate its values. At the moment we do not do this more sophisticated |
131 |
| - // check. |
132 |
| - isInitialized = response.StatusCode == HttpStatusCode.OK && response.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase); |
133 |
| - response.Close(); |
134 |
| - } |
135 |
| - catch (WebException ex) |
| 115 | + |
| 116 | + Uri serviceHealthUri = new Uri(this.ServiceUrl, new Uri("/session/FakeSessionIdForPollingPurposes", UriKind.Relative)); |
| 117 | + |
| 118 | + // Since Firefox driver won't implement the /session end point (because |
| 119 | + // the W3C spec working group stupidly decided that it isn't necessary), |
| 120 | + // we'll attempt to poll for a different URL which has no side effects. |
| 121 | + // We've chosen to poll on the "quit" URL, passing in a nonexistent |
| 122 | + // session id. |
| 123 | + using (var httpClient = new HttpClient()) |
136 | 124 | {
|
137 |
| - // Because the Firefox driver (incorrectly) does not allow quit on a |
138 |
| - // nonexistent session to succeed, this will throw a WebException, |
139 |
| - // which means we're reduced to using exception handling for flow control. |
140 |
| - // This situation is highly undesirable, and in fact is a horrible code |
141 |
| - // smell, but the implementation leaves us no choice. So we will check for |
142 |
| - // the known response code and content type header, just like we would for |
143 |
| - // the success case. Either way, a valid HTTP response instead of a socket |
144 |
| - // error would tell us that the HTTP server is responding to requests, which |
145 |
| - // is really what we want anyway. |
146 |
| - HttpWebResponse errorResponse = ex.Response as HttpWebResponse; |
147 |
| - if (errorResponse != null) |
148 |
| - { |
149 |
| - isInitialized = (errorResponse.StatusCode == HttpStatusCode.InternalServerError && errorResponse.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase)) || (errorResponse.StatusCode == HttpStatusCode.NotFound); |
150 |
| - } |
151 |
| - else |
| 125 | + httpClient.DefaultRequestHeaders.ConnectionClose = true; |
| 126 | + httpClient.Timeout = TimeSpan.FromSeconds(5); |
| 127 | + |
| 128 | + using (var httpRequest = new HttpRequestMessage(HttpMethod.Delete, serviceHealthUri)) |
152 | 129 | {
|
153 |
| - Console.WriteLine(ex.Message); |
| 130 | + try |
| 131 | + { |
| 132 | + using (var httpResponse = Task.Run(async () => await httpClient.SendAsync(httpRequest)).GetAwaiter().GetResult()) |
| 133 | + { |
| 134 | + isInitialized = (httpResponse.StatusCode == HttpStatusCode.OK |
| 135 | + || httpResponse.StatusCode == HttpStatusCode.InternalServerError |
| 136 | + || httpResponse.StatusCode == HttpStatusCode.NotFound) |
| 137 | + && httpResponse.Content.Headers.ContentType.MediaType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // Checking the response from deleting a nonexistent session. Note that we are simply |
| 142 | + // checking that the HTTP status returned is a 200 status, and that the resposne has |
| 143 | + // the correct Content-Type header. A more sophisticated check would parse the JSON |
| 144 | + // response and validate its values. At the moment we do not do this more sophisticated |
| 145 | + // check. |
| 146 | + catch (Exception ex) when (ex is HttpRequestException || ex is TaskCanceledException) |
| 147 | + { |
| 148 | + Console.WriteLine(ex); |
| 149 | + } |
154 | 150 | }
|
155 | 151 | }
|
156 | 152 |
|
|
0 commit comments