This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
HttpServerIntegrationTests.cs
100 lines (89 loc) · 4.09 KB
/
HttpServerIntegrationTests.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
using Flurl;
using Flurl.Http;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Build.Locator;
using Microsoft.Extensions.Configuration;
using Microsoft.Quantum.IQSharp;
using Microsoft.Quantum.IQSharp.Common;
using Microsoft.Quantum.IQSharp.Web.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Tests.IQSharp
{
[TestClass]
public class HttpServerIntegrationTests
{
private string autoLoadPackagesEnvVarName = "IQSHARP_AUTO_LOAD_PACKAGES";
private string originalAutoLoadPackages = string.Empty;
[TestInitialize]
public void SetTestEnvironment()
{
// Avoid loading default packages during this test by setting IQSHARP_AUTO_LOAD_PACKAGES to $null.
// Microsoft.Quantum.Standard and related packages are built from the QuantumLibraries repo,
// which is downstream of this one. So loading those packages could cause problems in E2E builds.
originalAutoLoadPackages = Environment.GetEnvironmentVariable(autoLoadPackagesEnvVarName) ?? string.Empty;
Environment.SetEnvironmentVariable(autoLoadPackagesEnvVarName, "$null");
}
[TestCleanup]
public void RestoreEnvironment()
{
Environment.SetEnvironmentVariable(autoLoadPackagesEnvVarName, originalAutoLoadPackages);
}
[TestMethod]
public async Task CompileAndSimulateViaApi()
{
var vsi = Microsoft.Quantum.IQSharp.Startup.VisualStudioInstance;
var args = new string[] { "server" };
// Set the same configuration that Program.cs is using.
Program.Configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddJsonFile("appsettings.json")
.AddCommandLine(
args,
new Dictionary<string, string>()
{
["--user-agent"] = "IQSHARP_USER_AGENT",
["--hosting-env"] = "IQSHARP_HOSTING_ENV"
}
)
.Add(new NormalizedEnvironmentVariableConfigurationSource
{
Prefix = "IQSHARP_",
Aliases = new Dictionary<string, string>
{
["USER_AGENT"] = "UserAgent",
["HOSTING_ENV"] = "HostingEnvironment",
["LOG_PATH"] = "LogPath",
["AUTO_LOAD_PACKAGES"] = "AutoLoadPackages",
["AUTO_OPEN_NAMESPACES"] = "AutoOpenNamespaces",
["SKIP_AUTO_LOAD_PROJECT"] = "SkipAutoLoadProject",
}
})
.Build();
using (IWebHost server = Program.GetHttpServer(args))
{
server.Start();
// TODO: Use constant strings for these http requests. Port and paths.
// Compile a snippet with the server and get back a list of now executable operations.
var compileResult = await "http://localhost:8008/api"
.AppendPathSegment("Snippets")
.AppendPathSegment("compile")
.PostJsonAsync(new CompileSnippetModel
{
Code = SNIPPETS.HelloQ
})
.ReceiveJson<Response<string[]>>();
Assert.AreEqual(Status.Success, compileResult.Status);
Assert.AreEqual("HelloQ", compileResult.Result.First());
// Now simulate the operation and check the output is as expected.
var simulateResult = await "http://localhost:8008/api"
.AppendPathSegment("Snippets")
.AppendPathSegment("HelloQ")
.AppendPathSegment("simulate")
.GetAsync()
.ReceiveJson<Response<object>>();
Assert.AreEqual(Status.Success, simulateResult.Status);
Assert.AreEqual("Hello from quantum world!", simulateResult.Messages.First());
}
}
}
}