Skip to content

Commit c5c2481

Browse files
committed
Fixes
1 parent 0e6e34a commit c5c2481

File tree

4 files changed

+80
-71
lines changed

4 files changed

+80
-71
lines changed

BrowserStackLocal/BrowserStackLocal Unit Tests/BrowserStackTunnelTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public void TestInitialState()
3030
public void TestBinaryPathIsSet()
3131
{
3232
tunnel = new TunnelClass();
33-
tunnel.addBinaryPath("dummyPath");
33+
tunnel.addBinaryPath("dummyPath", "");
3434
Assert.AreEqual(tunnel.getBinaryAbsolute(), "dummyPath");
3535
}
3636
[TestMethod]
3737
public void TestBinaryPathOnNull()
3838
{
3939
tunnel = new TunnelClass();
40-
tunnel.addBinaryPath(null);
40+
tunnel.addBinaryPath(null, "");
4141
string expectedPath = Path.Combine(homepath, ".browserstack");
4242
expectedPath = Path.Combine(expectedPath, binaryName);
4343
Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath);
@@ -46,7 +46,7 @@ public void TestBinaryPathOnNull()
4646
public void TestBinaryPathOnEmpty()
4747
{
4848
tunnel = new TunnelClass();
49-
tunnel.addBinaryPath("");
49+
tunnel.addBinaryPath("", "");
5050
string expectedPath = Path.Combine(homepath, ".browserstack");
5151
expectedPath = Path.Combine(expectedPath, binaryName);
5252
Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath);
@@ -56,7 +56,7 @@ public void TestBinaryPathOnFallback()
5656
{
5757
string expectedPath = "dummyPath";
5858
tunnel = new TunnelClass();
59-
tunnel.addBinaryPath("dummyPath");
59+
tunnel.addBinaryPath("dummyPath", "");
6060
Assert.AreEqual(tunnel.getBinaryAbsolute(), expectedPath);
6161

6262
tunnel.fallbackPaths();
@@ -78,7 +78,7 @@ public void TestBinaryPathOnFallback()
7878
public void TestBinaryPathOnNoMoreFallback()
7979
{
8080
tunnel = new TunnelClass();
81-
tunnel.addBinaryPath("dummyPath");
81+
tunnel.addBinaryPath("dummyPath", "");
8282
tunnel.fallbackPaths();
8383
tunnel.fallbackPaths();
8484
tunnel.fallbackPaths();

BrowserStackLocal/BrowserStackLocal Unit Tests/LocalTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void TestWorksForBinaryPath()
107107
tunnelMock.Setup(mock => mock.Run("dummyKey", "", logAbsolute, "start"));
108108
local.setTunnel(tunnelMock.Object);
109109
local.start(options);
110-
tunnelMock.Verify(mock => mock.addBinaryPath("dummyPath"), Times.Once);
110+
tunnelMock.Verify(mock => mock.addBinaryPath("dummyPath", ""), Times.Once);
111111
tunnelMock.Verify(mock => mock.addBinaryArguments(It.IsRegex("-logFile \"" + logAbsolute + "\" .*")), Times.Once());
112112
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute, "start"), Times.Once());
113113
local.stop();
@@ -129,7 +129,7 @@ public void TestWorksWithBooleanOptions()
129129
tunnelMock.Setup(mock => mock.Run("dummyKey", "", logAbsolute, "start"));
130130
local.setTunnel(tunnelMock.Object);
131131
local.start(options);
132-
tunnelMock.Verify(mock => mock.addBinaryPath(""), Times.Once);
132+
tunnelMock.Verify(mock => mock.addBinaryPath("", ""), Times.Once);
133133
tunnelMock.Verify(mock => mock.addBinaryArguments(It.IsRegex("-vvv.*-force.*-forcelocal.*-forceproxy.*-onlyAutomate.*")), Times.Once());
134134
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute, "start"), Times.Once());
135135
local.stop();
@@ -152,7 +152,7 @@ public void TestWorksWithValueOptions()
152152
tunnelMock.Setup(mock =>mock.Run("dummyKey", "", logAbsolute, "start"));
153153
local.setTunnel(tunnelMock.Object);
154154
local.start(options);
155-
tunnelMock.Verify(mock => mock.addBinaryPath(""), Times.Once);
155+
tunnelMock.Verify(mock => mock.addBinaryPath("", ""), Times.Once);
156156
tunnelMock.Verify(mock => mock.addBinaryArguments(
157157
It.IsRegex("-localIdentifier.*dummyIdentifier.*dummyHost.*-proxyHost.*dummyHost.*-proxyPort.*dummyPort.*-proxyUser.*dummyUser.*-proxyPass.*dummyPass.*")
158158
), Times.Once());
@@ -175,7 +175,7 @@ public void TestWorksWithCustomOptions()
175175
tunnelMock.Setup(mock => mock.Run("dummyKey", "", logAbsolute, "start"));
176176
local.setTunnel(tunnelMock.Object);
177177
local.start(options);
178-
tunnelMock.Verify(mock => mock.addBinaryPath(""), Times.Once);
178+
tunnelMock.Verify(mock => mock.addBinaryPath("", ""), Times.Once);
179179
tunnelMock.Verify(mock => mock.addBinaryArguments(
180180
It.IsRegex("-customBoolKey1.*-customBoolKey2.*-customKey1.*customValue1.*-customKey2.*customValue2.*")
181181
), Times.Once());
@@ -200,7 +200,7 @@ public void TestCallsFallbackOnFailure()
200200
});
201201
local.setTunnel(tunnelMock.Object);
202202
local.start(options);
203-
tunnelMock.Verify(mock => mock.addBinaryPath(""), Times.Once);
203+
tunnelMock.Verify(mock => mock.addBinaryPath("", ""), Times.Once);
204204
tunnelMock.Verify(mock => mock.addBinaryArguments(It.IsRegex("-logFile \"" + logAbsolute + "\" .*")), Times.Once());
205205
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute, "start"), Times.Exactly(2));
206206
tunnelMock.Verify(mock => mock.fallbackPaths(), Times.Once());
@@ -219,7 +219,7 @@ public void TestKillsTunnel()
219219
local.setTunnel(tunnelMock.Object);
220220
local.start(options);
221221
local.stop();
222-
tunnelMock.Verify(mock => mock.addBinaryPath(""), Times.Once);
222+
tunnelMock.Verify(mock => mock.addBinaryPath("", ""), Times.Once);
223223
tunnelMock.Verify(mock => mock.addBinaryArguments(It.IsRegex("-logFile \"" + logAbsolute + "\" .*")), Times.Once());
224224
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute, "start"), Times.Once());
225225
}

BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public class BrowserStackTunnel : IDisposable
2323
static readonly string uname = Util.GetUName();
2424
static readonly string binaryName = GetBinaryName();
2525

26-
static readonly string userAgent = "";
27-
static readonly string sourceUrl = null;
26+
private static string userAgent = "";
27+
private string sourceUrl = null;
2828
private bool isFallbackEnabled = false;
2929
private Exception downloadFailureException = null;
3030

@@ -94,8 +94,15 @@ static string GetBinaryName()
9494
return "BrowserStackLocal.exe";
9595
}
9696

97-
public virtual void addBinaryPath(string binaryAbsolute)
97+
public virtual void addBinaryPath(string binaryAbsolute, string accessKey, bool fallbackEnabled = false, Exception failureException = null)
9898
{
99+
if (basePathsIndex == -1)
100+
{
101+
/* Called at most twice (primary & a fallback) */
102+
isFallbackEnabled = fallbackEnabled;
103+
downloadFailureException = failureException;
104+
fetchSourceUrl(accessKey);
105+
}
99106
if (binaryAbsolute == null || binaryAbsolute.Trim().Length == 0)
100107
{
101108
binaryAbsolute = Path.Combine(basePaths[++basePathsIndex], binaryName);
@@ -112,15 +119,19 @@ public virtual void addBinaryArguments(string binaryArguments)
112119
this.binaryArguments = binaryArguments;
113120
}
114121

115-
public BrowserStackTunnel(string userAgent)
122+
public BrowserStackTunnel(string userAgentParam)
116123
{
117-
userAgent = userAgent;
124+
userAgent = userAgentParam;
118125
localState = LocalState.Idle;
119126
output = new StringBuilder();
120127
}
121128

122129
public virtual void fallbackPaths()
123130
{
131+
if (File.Exists(binaryAbsolute))
132+
{
133+
File.Delete(binaryAbsolute);
134+
}
124135
if (basePathsIndex >= basePaths.Length - 1)
125136
{
126137
throw new Exception("Binary not found or failed to launch. Make sure that BrowserStackLocal is not already running.");
@@ -164,49 +175,48 @@ private string fetchSourceUrl(string accessKey)
164175
{
165176
{ "auth_token", accessKey }
166177
};
167-
client.DefaultRequestHeaders.Add("Content-Type", "application/json");
168-
client.DefaultRequestHeaders.Add("user-agent", userAgent);
169178

170179
if (isFallbackEnabled)
171180
{
172181
data["error_message"] = downloadFailureException.Message;
173-
client.DefaultRequestHeaders.Add("X-Local-Fallback-Cloudflare", "true");
174182
}
175183

176-
string jsonData = JsonConvert.SerializeObject(data);
184+
var jsonData = JsonConvert.SerializeObject(data);
177185
var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
178186

179-
HttpResponseMessage response = await client.PostAsync(url, content);
187+
client.DefaultRequestHeaders.Add("User-Agent", userAgent);
188+
if (isFallbackEnabled)
189+
{
190+
client.DefaultRequestHeaders.Add("X-Local-Fallback-Cloudflare", "true");
191+
}
192+
193+
var response = client.PostAsync(url, content).Result;
180194

181195
response.EnsureSuccessStatusCode();
182196

183-
string responseString = await response.Content.ReadAsStringAsync();
184-
185-
dynamic jsonResponse = JsonConvert.DeserializeObject(responseString);
197+
var responseString = response.Content.ReadAsStringAsync().Result;
186198

187-
Console.WriteLine("Response JSON:");
188-
Console.WriteLine(jsonResponse);
199+
var jsonResponse = JObject.Parse(responseString);
189200

190-
if (jsonResponse.error != null)
201+
if (jsonResponse["error"] != null)
191202
{
192-
throw new Exception(jsonResponse.error);
203+
throw new Exception((string)jsonResponse["error"]);
193204
}
194-
return jsonResponse.data.endpoint;
205+
206+
sourceUrl = jsonResponse["data"]?["endpoint"]?.ToString();
207+
return sourceUrl;
195208
}
196209
}
197210

198211
public void downloadBinary(string accessKey)
199212
{
200213
string binaryDirectory = Path.Combine(this.binaryAbsolute, "..");
201-
//string binaryAbsolute = Path.Combine(binaryDirectory, binaryName);
202-
203-
string sourceDownloadUrl = fetchSourceUrl(accessKey);
204214

205215
Directory.CreateDirectory(binaryDirectory);
206216

207217
using (var client = new WebClient())
208218
{
209-
client.DownloadFile(sourceDownloadUrl + "/" + binaryName, this.binaryAbsolute);
219+
client.DownloadFile(sourceUrl + "/" + binaryName, this.binaryAbsolute);
210220
}
211221

212222
if (!File.Exists(binaryAbsolute))
@@ -217,7 +227,7 @@ public void downloadBinary(string accessKey)
217227
modifyBinaryPermission();
218228
}
219229

220-
public virtual void Run(string accessKey, string folder, string logFilePath, string processType, bool fallbackEnabled, Exception failureException)
230+
public virtual void Run(string accessKey, string folder, string logFilePath, string processType)
221231
{
222232
string arguments = "-d " + processType + " ";
223233
if (folder != null && folder.Trim().Length != 0)
@@ -230,8 +240,6 @@ public virtual void Run(string accessKey, string folder, string logFilePath, str
230240
}
231241
if (!File.Exists(binaryAbsolute))
232242
{
233-
isFallbackEnabled = fallbackEnabled;
234-
downloadFailureException = failureException;
235243
downloadBinary(accessKey);
236244
}
237245

BrowserStackLocal/BrowserStackLocal/Local.cs

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -150,50 +150,19 @@ public Local()
150150
userAgent = userAgent + "/" + bindingVersion;
151151
tunnel = new BrowserStackTunnel(userAgent);
152152
}
153-
public void start(List<KeyValuePair<string, string>> options)
154-
{
155-
foreach (KeyValuePair<string, string> pair in options)
156-
{
157-
string key = pair.Key;
158-
string value = pair.Value;
159-
addArgs(key, value);
160-
}
161-
162-
if (accessKey == null || accessKey.Trim().Length == 0)
163-
{
164-
accessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
165-
if (accessKey == null || accessKey.Trim().Length == 0)
166-
{
167-
throw new Exception("BROWSERSTACK_ACCESS_KEY cannot be empty. " +
168-
"Specify one by adding key to options or adding to the environment variable BROWSERSTACK_ACCESS_KEY.");
169-
}
170-
Regex.Replace(this.accessKey, @"\s+", "");
171-
}
172-
173-
if (customLogPath == null || customLogPath.Trim().Length == 0)
174-
{
175-
customLogPath = Path.Combine(BrowserStackTunnel.basePaths[1], "local.log");
176-
}
177-
178-
argumentString += "-logFile \"" + customLogPath + "\" ";
179-
argumentString += "--source \"c-sharp:" + bindingVersion + "\" ";
180-
tunnel.addBinaryArguments(argumentString);
181-
182-
DownloadVerifyAndRunBinary();
183-
}
184153

185154
private void DownloadVerifyAndRunBinary()
186155
{
187156
tunnel.basePathsIndex = -1;
188-
tunnel.addBinaryPath(customBinaryPath);
157+
tunnel.addBinaryPath(customBinaryPath, accessKey, isFallbackEnabled, downloadFailureException);
189158
try
190159
{
191160
while (true)
192161
{
193162
bool except = false;
194163
try
195164
{
196-
tunnel.Run(accessKey, folder, customLogPath, "start", isFallbackEnabled, downloadFailureException);
165+
tunnel.Run(accessKey, folder, customLogPath, "start");
197166
}
198167
catch (System.ComponentModel.Win32Exception)
199168
{
@@ -220,7 +189,7 @@ private void DownloadVerifyAndRunBinary()
220189
{
221190
isFallbackEnabled = true;
222191
downloadFailureException = err;
223-
downloadVerifyAndRunBinary();
192+
DownloadVerifyAndRunBinary();
224193
}
225194
else
226195
{
@@ -229,9 +198,41 @@ private void DownloadVerifyAndRunBinary()
229198
}
230199
}
231200

201+
public void start(List<KeyValuePair<string, string>> options)
202+
{
203+
foreach (KeyValuePair<string, string> pair in options)
204+
{
205+
string key = pair.Key;
206+
string value = pair.Value;
207+
addArgs(key, value);
208+
}
209+
210+
if (accessKey == null || accessKey.Trim().Length == 0)
211+
{
212+
accessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");
213+
if (accessKey == null || accessKey.Trim().Length == 0)
214+
{
215+
throw new Exception("BROWSERSTACK_ACCESS_KEY cannot be empty. " +
216+
"Specify one by adding key to options or adding to the environment variable BROWSERSTACK_ACCESS_KEY.");
217+
}
218+
Regex.Replace(this.accessKey, @"\s+", "");
219+
}
220+
221+
if (customLogPath == null || customLogPath.Trim().Length == 0)
222+
{
223+
customLogPath = Path.Combine(BrowserStackTunnel.basePaths[1], "local.log");
224+
}
225+
226+
argumentString += "-logFile \"" + customLogPath + "\" ";
227+
argumentString += "--source \"c-sharp:" + bindingVersion + "\" ";
228+
tunnel.addBinaryArguments(argumentString);
229+
230+
DownloadVerifyAndRunBinary();
231+
}
232+
232233
public void stop()
233234
{
234-
tunnel.Run(accessKey, folder, customLogPath, "stop", isFallbackEnabled, downloadFailureException);
235+
tunnel.Run(accessKey, folder, customLogPath, "stop");
235236
tunnel.Kill();
236237
}
237238
}

0 commit comments

Comments
 (0)