Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
FriggaHel committed Mar 28, 2024
1 parent 9c22be7 commit 1e916a6
Showing 1 changed file with 97 additions and 97 deletions.
194 changes: 97 additions & 97 deletions visual-dotnet/SauceLabs.Visual.Tests/BuildFactoryTest.cs
Original file line number Diff line number Diff line change
@@ -1,98 +1,98 @@
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NUnit.Framework;
using RichardSzalay.MockHttp;
using SauceLabs.Visual.GraphQL;
using SauceLabs.Visual.Models;

namespace SauceLabs.Visual.Tests;

public class BuildFactoryTest
{
MockHttpMessageHandler MockedHandler;
private const string _username = "dummy-username";
private const string _accessKey = "dummy-key";

[SetUp]
public void Setup()
{
MockedHandler = new MockHttpMessageHandler();

var createHandler = () =>
{
var id = RandomString(32);
var content = new VisualBuild(id, $"http://dummy/test/{id}", BuildMode.Running);
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new ReadOnlyMemoryContent(ToResult(content));
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return Task.FromResult(resp);
};

var base64EncodedAuthenticationString =
Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_username}:{_accessKey}"));
var regions = new[] { Region.Staging, Region.UsEast4, Region.EuCentral1, Region.UsWest1 };
foreach (var r in regions)
{
MockedHandler
.When(r.Value.ToString())
.WithHeaders($"Authorization: Basic {base64EncodedAuthenticationString}")
.WithPartialContent($"\"operationName\":\"{CreateBuildMutation.OperationName}\"")
.Respond(createHandler);
MockedHandler
.When(r.Value.ToString())
.WithHeaders($"Authorization: Basic {base64EncodedAuthenticationString}")
.WithPartialContent($"\"operationName\":\"{FinishBuildMutation.OperationName}\"")
.Respond(createHandler);
}
MockedHandler.Fallback.Throw(new InvalidOperationException("No matching mock handler"));
}

[TearDown]
public void Cleanup()
{
MockedHandler.Dispose();
}

private string RandomString(int length)
{
const string chars = "abcdef0123456789";
return new string(Enumerable.Repeat(chars, length).Select(s => s[Random.Shared.Next(s.Length)]).ToArray());
}

private byte[] ToResult(object o)
{
var nestedContent = new
{
Data = new
{
Result = o
},
};
return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(nestedContent));
}

[Test]
public async Task BuildFactory_ReturnSameBuildWhenSameRegion()
{
var api = new VisualApi(Region.Staging, _username, _accessKey, MockedHandler.ToHttpClient());
var build1 = await BuildFactory.Get(api, new CreateBuildOptions());
var build2 = await BuildFactory.Get(api, new CreateBuildOptions());
Assert.AreEqual(build1, build2);
}

[Test]
public async Task BuildFactory_ReturnDifferentBuildWhenDifferentRegion()
{
var api1 = new VisualApi(Region.Staging, _username, _accessKey, MockedHandler.ToHttpClient());
var api2 = new VisualApi(Region.UsEast4, _username, _accessKey, MockedHandler.ToHttpClient());
var build1 = await BuildFactory.Get(api1, new CreateBuildOptions());
var build2 = await BuildFactory.Get(api2, new CreateBuildOptions());
Assert.AreNotEqual(build1, build2);
}
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NUnit.Framework;
using RichardSzalay.MockHttp;
using SauceLabs.Visual.GraphQL;
using SauceLabs.Visual.Models;

namespace SauceLabs.Visual.Tests;

public class BuildFactoryTest
{
MockHttpMessageHandler MockedHandler;
private const string _username = "dummy-username";
private const string _accessKey = "dummy-key";

[SetUp]
public void Setup()
{
MockedHandler = new MockHttpMessageHandler();

var createHandler = () =>
{
var id = RandomString(32);
var content = new VisualBuild(id, $"http://dummy/test/{id}", BuildMode.Running);
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new ReadOnlyMemoryContent(ToResult(content));
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return Task.FromResult(resp);
};

var base64EncodedAuthenticationString =
Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_username}:{_accessKey}"));
var regions = new[] { Region.Staging, Region.UsEast4, Region.EuCentral1, Region.UsWest1 };
foreach (var r in regions)
{
MockedHandler
.When(r.Value.ToString())
.WithHeaders($"Authorization: Basic {base64EncodedAuthenticationString}")
.WithPartialContent($"\"operationName\":\"{CreateBuildMutation.OperationName}\"")
.Respond(createHandler);
MockedHandler
.When(r.Value.ToString())
.WithHeaders($"Authorization: Basic {base64EncodedAuthenticationString}")
.WithPartialContent($"\"operationName\":\"{FinishBuildMutation.OperationName}\"")
.Respond(createHandler);
}
MockedHandler.Fallback.Throw(new InvalidOperationException("No matching mock handler"));
}

[TearDown]
public void Cleanup()
{
MockedHandler.Dispose();
}

private string RandomString(int length)
{
const string chars = "abcdef0123456789";
return new string(Enumerable.Repeat(chars, length).Select(s => s[Random.Shared.Next(s.Length)]).ToArray());
}

private byte[] ToResult(object o)
{
var nestedContent = new
{
Data = new
{
Result = o
},
};
return Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(nestedContent));
}

[Test]
public async Task BuildFactory_ReturnSameBuildWhenSameRegion()
{
var api = new VisualApi(Region.Staging, _username, _accessKey, MockedHandler.ToHttpClient());
var build1 = await BuildFactory.Get(api, new CreateBuildOptions());
var build2 = await BuildFactory.Get(api, new CreateBuildOptions());
Assert.AreEqual(build1, build2);
}

[Test]
public async Task BuildFactory_ReturnDifferentBuildWhenDifferentRegion()
{
var api1 = new VisualApi(Region.Staging, _username, _accessKey, MockedHandler.ToHttpClient());
var api2 = new VisualApi(Region.UsEast4, _username, _accessKey, MockedHandler.ToHttpClient());
var build1 = await BuildFactory.Get(api1, new CreateBuildOptions());
var build2 = await BuildFactory.Get(api2, new CreateBuildOptions());
Assert.AreNotEqual(build1, build2);
}
}

0 comments on commit 1e916a6

Please sign in to comment.