Skip to content

Commit

Permalink
Download ChromeDriver MatchingBrowser version network error (#332)
Browse files Browse the repository at this point in the history
* Added json file download timeout
  • Loading branch information
RayLei2333 authored Jun 22, 2024
1 parent a0ac88e commit f7a517f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
9 changes: 9 additions & 0 deletions WebDriverManager.Tests/ChromeConfigTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.RegularExpressions;
using WebDriverManager.DriverConfigs.Impl;
using WebDriverManager.Helpers;
using Xunit;

namespace WebDriverManager.Tests
Expand All @@ -21,5 +22,13 @@ public void DriverDownloadLatestTest()
new DriverManager().SetUpDriver(new ChromeConfig());
Assert.NotEmpty(WebDriverFinder.FindFile(GetBinaryName()));
}

[Fact]
public void DriverDownloadTest()
{
new DriverManager().SetUpDriver(new ChromeConfig(), VersionResolveStrategy.MatchingBrowser);

Assert.NotEmpty(WebDriverFinder.FindFile(GetBinaryName()));
}
}
}
25 changes: 23 additions & 2 deletions WebDriverManager/Clients/ChromeForTestingClient.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using WebDriverManager.Helpers;
using WebDriverManager.Models.Chrome;

namespace WebDriverManager.Clients
{
public static class ChromeForTestingClient
{
private static readonly string BaseUrl = "https://googlechromelabs.github.io/chrome-for-testing/";

private static HttpClient _httpClient;

private static HttpClient HttpClient
Expand All @@ -28,6 +31,9 @@ private static HttpClient HttpClient
BaseAddress = new Uri(BaseUrl)
};

_httpClient.DefaultRequestHeaders.Add("User-Agent", "WebDriverManager.NET");
_httpClient.DefaultRequestHeaders.Add("Accept", "*/*");
_httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "br, deflate, gzip, x-gzip");
return _httpClient;
}
}
Expand Down Expand Up @@ -59,11 +65,26 @@ private static TResult GetResultFromHttpTask<TResult>(Task<HttpResponseMessage>
{
var httpTask = Task.Run(() => taskToRun);
httpTask.Wait();
var response = httpTask.Result;
if (response.Content.Headers.Contains("Content-Encoding"))
{
string encoding = response.Content.Headers.GetValues("Content-Encoding").FirstOrDefault();
if (string.Equals(encoding, "gzip", StringComparison.OrdinalIgnoreCase))
{
var readBytesTask = Task.Run(() => httpTask.Result.Content.ReadAsByteArrayAsync());
readBytesTask.Wait();

byte[] decompressionData = GzipDecompression.DecompressGzip(readBytesTask.Result);
return JsonConvert.DeserializeObject<TResult>(Encoding.UTF8.GetString(decompressionData));
}
}


var readStringTask = Task.Run(() => httpTask.Result.Content.ReadAsStringAsync());
readStringTask.Wait();

return JsonConvert.DeserializeObject<TResult>(readStringTask.Result);


}
}
}
27 changes: 27 additions & 0 deletions WebDriverManager/Helpers/GzipDecompression.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebDriverManager.Helpers
{
internal class GzipDecompression
{
public static byte[] DecompressGzip(byte[] compressedData)
{
using (MemoryStream compressedStream = new MemoryStream(compressedData))
using (MemoryStream decompressedStream = new MemoryStream())
{
using (GZipStream decompressionStream = new GZipStream(compressedStream, CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedStream);
}

return decompressedStream.ToArray();
}
}
}
}

0 comments on commit f7a517f

Please sign in to comment.