public async Task Request_for_non_existent_markdown_file_returns_404() { using (var agent = new AgentService(new StartupOptions(rootDirectory: new FileSystemDirectoryAccessor(TestAssets.SampleConsole)))) { var response = await agent.GetAsync(@"/DOESNOTEXIST"); response.Should().BeNotFound(); } }
public async Task Returns_200_if_the_package_exists() { var packageVersion = "1.0.0"; using (var agent = new AgentService()) { var response = await agent.GetAsync($@"/packages/console/{packageVersion}"); response.StatusCode.Should().Be(HttpStatusCode.OK); } }
public async Task Can_serve_blazor_console_code_runner() { using (var agent = new AgentService()) { var response = await agent.GetAsync(@"/LocalCodeRunner/blazor-console"); response.EnsureSuccess(); var result = await response.Content.ReadAsStringAsync(); result.Should().Contain("Loading..."); } }
public async Task Returns_404_if_the_package_does_not_exist() { var packageName = Guid.NewGuid().ToString(); var packageVersion = "1.0.0"; using (var agent = new AgentService()) { var response = await agent.GetAsync($@"/packages/{packageName}/{packageVersion}"); response.StatusCode.Should().Be(HttpStatusCode.NotFound); } }
public async Task Can_serve_nodatime_code_runner() { using (var agent = new AgentService(StartupOptions.FromCommandLine("hosted"))) { var response = await agent.GetAsync(@"/LocalCodeRunner/blazor-nodatime.api"); response.Should().BeSuccessful(); var result = await response.Content.ReadAsStringAsync(); result.Should().Contain("Loading..."); } }
public async Task Return_html_for_existing_markdown_files_in_a_subdirectory() { using (var agent = new AgentService(new StartupOptions(dir: TestAssets.SampleConsole))) { var response = await agent.GetAsync(@"Subdirectory/Tutorial.md"); response.Should().BeSuccessful(); var result = await response.Content.ReadAsStringAsync(); result.Should().Contain("<em>tutorial file</em>"); } }
public async Task Return_html_for_an_existing_markdown_file() { using (var agent = new AgentService(new StartupOptions(dir: TestAssets.SampleConsole))) { var response = await agent.GetAsync(@"Readme.md"); response.Should().BeSuccessful(); var result = await response.Content.ReadAsStringAsync(); response.Content.Headers.ContentType.MediaType.Should().Be("text/html"); result.Should().Contain("<em>markdown file</em>"); } }
public static Task <HttpResponseMessage> GetAsync( this AgentService service, string uri, string referer = null) { var message = new HttpRequestMessage(HttpMethod.Get, uri); if (referer != null) { message.Headers.Add("referer", referer); } return(service.SendAsync(message)); }
public async Task Can_serve_nodatime_code_runner() { var registry = await Default.PackageRegistry.ValueAsync(); var nodatime = await registry.Get <WorkspaceServer.Packaging.Package2>("blazor-nodatime.api"); using (var agent = new AgentService(StartupOptions.FromCommandLine("hosted"))) { var response = await agent.GetAsync(@"/LocalCodeRunner/blazor-nodatime.api"); response.Should().BeSuccessful(); var result = await response.Content.ReadAsStringAsync(); result.Should().Contain("Loading..."); } }
public async Task Returns_IsWasmSupported_false_if_the_package_does_not_contain_wasm_runner() { var packageVersion = "1.0.0"; using (var agent = new AgentService()) { var response = await agent.GetAsync($@"/packages/console/{packageVersion}"); response.Should().BeSuccessful(); var result = await response.Content.ReadAsStringAsync(); result.FromJsonTo <Package>() .IsWasmSupported .Should() .BeFalse(); } }
public async Task When_relative_uri_is_specified_then_it_opens_to_that_page() { var launchUri = new Uri("something.md", UriKind.Relative); using (var clock = VirtualClock.Start()) using (var agent = new AgentService(new StartupOptions( dir: TestAssets.SampleConsole, uri: launchUri))) { await clock.Wait(5.Seconds()); agent.BrowserLauncher .LaunchedUri .ToString() .Should() .Match("http://localhost:*/something.md"); } }
public async Task Returns_IsWasmSupported_true_if_the_package_contains_wasm_runner() { var package = await Create.InstalledPackageWithBlazorEnabled(); var packageVersion = "1.0.0"; using (var agent = new AgentService()) { var response = await agent.GetAsync($"/packages/{package.Name}/{packageVersion}"); response.Should().BeSuccessful(); var result = await response.Content.ReadAsStringAsync(); result.FromJsonTo <Package>() .IsWasmSupported .Should() .BeTrue(); } }
public async Task Is_able_to_serve_static_files() { using (var disposableDirectory = DisposableDirectory.Create()) { System.IO.File.WriteAllText(Path.Combine(disposableDirectory.Directory.FullName, "a.js"), "alert('This is an alert from javascript');"); var options = new StartupOptions(dir: disposableDirectory.Directory); using (var agent = new AgentService(options: options)) { var response = await agent.GetAsync(@"/a.js"); response.Should().BeSuccessful(); response.Content.Headers.ContentType.MediaType.Should().Be("application/javascript"); var html = await response.Content.ReadAsStringAsync(); html.Should().Be("alert('This is an alert from javascript');"); } } }
public async Task Returns_blazor_false_if_the_package_does_not_contain_blazor_runner() { var package = await Create.ConsoleWorkspaceCopy(); var packageVersion = "1.0.0"; using (var agent = new AgentService()) { var response = await agent.GetAsync($@"/packages/{package.Name}/{packageVersion}"); response.Should().BeSuccessful(); var result = await response.Content.ReadAsStringAsync(); result.FromJsonTo <Package>() .IsBlazorSupported .Should() .BeFalse(); } }
public async Task When_they_run_a_snippet_then_they_get_diagnostics_for_the_first_line() { await Default.ConsoleWorkspace(); var output = Guid.NewGuid().ToString(); using (var agent = new AgentService()) { var json = new WorkspaceRequest( Workspace.FromSource( $@"Console.WriteLine(""{output}""".EnforceLF(), workspaceType: "script"), requestId: "TestRun") .ToJson(); var request = new HttpRequestMessage( HttpMethod.Post, @"/workspace/run") { Content = new StringContent( json, Encoding.UTF8, "application/json") }; var response = await agent.SendAsync(request); var result = await response .EnsureSuccess() .DeserializeAs <RunResult>(); var diagnostics = result.GetFeature <Diagnostics>(); diagnostics.Should().Contain(d => d.Start == 56 && d.End == 56 && d.Message == "(1,57): error CS1026: ) expected" && d.Id == "CS1026"); } }
public async Task Can_serve_from_webassembly_controller() { var(name, addSource) = await Create.NupkgWithBlazorEnabled(); using (var agent = new AgentService(new StartupOptions(addPackageSource: new WorkspaceServer.PackageSource(addSource.FullName)))) { var response = await agent.GetAsync($@"/LocalCodeRunner/{name}"); response.EnsureSuccess(); var result = await response.Content.ReadAsStringAsync(); result.Should().Contain("Loading..."); response = await agent.GetAsync($@"/LocalCodeRunner/{name}/interop.js"); response.EnsureSuccess(); result = await response.Content.ReadAsStringAsync(); result.Should().Contain("DotNet.invokeMethodAsync"); } }
public async Task Scaffolding_HTML_includes_trydotnet_js_autoEnable_invocation() { using (var agent = new AgentService(new StartupOptions(dir: TestAssets.SampleConsole))) { var response = await agent.GetAsync(@"Subdirectory/Tutorial.md"); response.Should().BeSuccessful(); var html = await response.Content.ReadAsStringAsync(); var document = new HtmlDocument(); document.LoadHtml(html); var script = document.DocumentNode .Descendants("body") .Single() .Descendants("script") .FirstOrDefault(s => s.InnerHtml.Contains(@"trydotnet.autoEnable({ apiBaseAddress: new URL(""http://localhost""), useBlazor:false });")); script.Should().NotBeNull(); } }
public async Task Can_serve_from_webassembly_controller() { var(name, addSource) = await Create.NupkgWithBlazorEnabled(); using (var agent = new AgentService(new StartupOptions(addPackageSource: new PackageSource(addSource.FullName)))) { var response = await agent.GetAsync($@"/LocalCodeRunner/{name}"); response.EnsureSuccess(); var result = await response.Content.ReadAsStringAsync(); result.Should().Contain("Loading..."); response = await agent.GetAsync($@"/LocalCodeRunner/{name}/interop.js"); response.EnsureSuccess(); result = await response.Content.ReadAsStringAsync(); result.Should().Contain("invokeMethodAsync"); } // Now do the same thing in hosted mode using the already installed package using (var agent = new AgentService(StartupOptions.FromCommandLine("hosted"))) { var response = await agent.GetAsync($@"/LocalCodeRunner/{name}"); response.EnsureSuccess(); var result = await response.Content.ReadAsStringAsync(); result.Should().Contain("Loading..."); response = await agent.GetAsync($@"/LocalCodeRunner/{name}/interop.js"); response.EnsureSuccess(); result = await response.Content.ReadAsStringAsync(); result.Should().Contain("invokeMethodAsync"); } }
public async Task Scaffolding_HTML_trydotnet_js_autoEnable_useBlazor_is_true_when_package_is_not_specified_and_supports_wasmrunner() { var(name, addSource) = await Create.NupkgWithBlazorEnabled("packageName"); using (var dir = DisposableDirectory.Create()) { var text = $@" ```cs --package {name} ```"; var path = Path.Combine(dir.Directory.FullName, "BlazorTutorial.md"); File.WriteAllText(path, text); var startupOptions = new StartupOptions( dir: dir.Directory, addPackageSource: new WorkspaceServer.PackageSource(addSource.FullName)); using (var agent = new AgentService(startupOptions)) { var response = await agent.GetAsync(@"/BlazorTutorial.md"); response.Should().BeSuccessful(); var html = await response.Content.ReadAsStringAsync(); var document = new HtmlDocument(); document.LoadHtml(html); var scripts = document.DocumentNode .Descendants("body") .Single() .Descendants("script") .Select(s => s.InnerHtml); scripts.Should() .Contain(s => s.Contains(@"trydotnet.autoEnable({ apiBaseAddress: new URL(""http://localhost""), useWasmRunner: true });")); } } }
public async Task Scaffolding_HTML_includes_trydotnet_js_script_link() { using (var agent = new AgentService(new StartupOptions(dir: TestAssets.SampleConsole))) { var response = await agent.GetAsync(@"Subdirectory/Tutorial.md"); response.Should().BeSuccessful(); var html = await response.Content.ReadAsStringAsync(); var document = new HtmlDocument(); document.LoadHtml(html); var script = document.DocumentNode .Descendants("head") .Single() .Descendants("script") .FirstOrDefault(); script.Attributes["src"].Value.Should().StartWith("/api/trydotnet.min.js?v="); } }
public async Task Lists_markdown_files_when_a_folder_is_requested() { using (var agent = new AgentService(new StartupOptions(dir: TestAssets.SampleConsole))) { var response = await agent.GetAsync(@"/"); response.Should().BeSuccessful(); var html = await response.Content.ReadAsStringAsync(); var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(html); var links = htmlDoc.DocumentNode .SelectNodes("//a") .Select(a => a.Attributes["href"].Value) .ToArray(); links.Should().Contain("./Readme.md"); links.Should().Contain("./Subdirectory/Tutorial.md"); } }
public async Task A_script_snippet_workspace_can_be_used_to_get_diagnostics() { await Default.ConsoleWorkspace(); var log = new LogEntryList(); var(processed, position) = CodeManipulation.ProcessMarkup("adddd"); using (LogEvents.Subscribe(log.Add)) using (var agent = new AgentService()) { var json = new WorkspaceRequest( requestId: "TestRun", activeBufferId: "default.cs", workspace: Workspace.FromSource( processed, "script", id: "default.cs", position: position)) .ToJson(); var request = new HttpRequestMessage(HttpMethod.Post, @"/workspace/diagnostics") { Content = new StringContent( json, Encoding.UTF8, "application/json") }; var response = await agent.SendAsync(request); var result = await response .EnsureSuccess() .DeserializeAs <DiagnosticResult>(); result.Diagnostics.Should().NotBeNullOrEmpty(); result.Diagnostics.Should().Contain(signature => signature.Message == "default.cs(1,1): error CS0103: The name \'adddd\' does not exist in the current context"); } }
public async Task A_script_snippet_workspace_can_be_used_to_get_signature_help() { await Default.ConsoleWorkspace(); var log = new LogEntryList(); var(processed, position) = CodeManipulation.ProcessMarkup("Console.WriteLine($$)"); using (LogEvents.Subscribe(log.Add)) using (var agent = new AgentService()) { var json = new WorkspaceRequest( requestId: "TestRun", activeBufferId: "default.cs", workspace: Workspace.FromSource( processed, "script", id: "default.cs", position: position)) .ToJson(); var request = new HttpRequestMessage(HttpMethod.Post, @"/workspace/signaturehelp") { Content = new StringContent( json, Encoding.UTF8, "application/json") }; var response = await agent.SendAsync(request); var result = await response .EnsureSuccess() .DeserializeAs <SignatureHelpResult>(); result.Signatures.Should().NotBeNullOrEmpty(); result.Signatures.Should().Contain(signature => signature.Label == "void Console.WriteLine(string format, params object[] arg)"); } }
public async Task Scaffolding_HTML_includes_trydotnet_js_autoEnable_invocation_with_useBlazor_defaulting_to_false() { using (var agent = new AgentService(new StartupOptions(rootDirectory: new FileSystemDirectoryAccessor(TestAssets.SampleConsole)))) { var response = await agent.GetAsync(@"Subdirectory/Tutorial.md"); response.Should().BeSuccessful(); var html = await response.Content.ReadAsStringAsync(); var document = new HtmlDocument(); document.LoadHtml(html); var scripts = document.DocumentNode .Descendants("body") .Single() .Descendants("script") .Select(s => s.InnerHtml); scripts.Should() .Contain(s => s.Contains(@"trydotnet.autoEnable({ apiBaseAddress: new URL(""http://localhost""), useWasmRunner: false });")); } }
public async Task A_script_snippet_workspace_can_be_used_to_get_completions() { await Default.ConsoleWorkspace(); var(processed, position) = CodeManipulation.ProcessMarkup("Console.$$"); using (var agent = new AgentService(StartupOptions.FromCommandLine("hosted"))) { var json = new WorkspaceRequest( requestId: "TestRun", activeBufferId: "default.cs", workspace: Workspace.FromSource( processed, "script", id: "default.cs", position: position)) .ToJson(); var request = new HttpRequestMessage( HttpMethod.Post, @"/workspace/completion") { Content = new StringContent( json, Encoding.UTF8, "application/json") }; var response = await agent.SendAsync(request); var result = await response .EnsureSuccess() .DeserializeAs <CompletionResult>(); result.Items.Should().ContainSingle(item => item.DisplayText == "WriteLine"); } }
public async Task A_console_project_can_be_used_to_get_diagnostics() { await Default.ConsoleWorkspace(); #region bufferSources var program = @"using System; using System.Linq; namespace FibonacciTest { public class Program { public static void Main() { foreach (var i in FibonacciGenerator.Fibonacci().Take(20)) { Console.WriteLine(i); } } } }".EnforceLF(); var generator = @"using System.Collections.Generic; using System; namespace FibonacciTest { public static class FibonacciGenerator { public static IEnumerable<int> Fibonacci() { int current = 1, next = 1; while (true) { adddd yield return current; next = current + (current = next); Cons$$ } } } }".EnforceLF(); #endregion var(processed, position) = CodeManipulation.ProcessMarkup(generator); var log = new LogEntryList(); using (LogEvents.Subscribe(log.Add)) using (var agent = new AgentService()) { var json = new WorkspaceRequest(activeBufferId: "generators/FibonacciGenerator.cs", requestId: "TestRun", workspace: Workspace.FromSources( "console", language: "csharp", ("Program.cs", program, 0), ("generators/FibonacciGenerator.cs", processed, position) )).ToJson(); var request = new HttpRequestMessage( HttpMethod.Post, @"/workspace/diagnostics") { Content = new StringContent( json, Encoding.UTF8, "application/json") }; var response = await agent.SendAsync(request); var result = await response .EnsureSuccess() .DeserializeAs <DiagnosticResult>(); result.Diagnostics.Should().NotBeNullOrEmpty(); result.Diagnostics.Should().Contain(diagnostic => diagnostic.Message == "generators/FibonacciGenerator.cs(12,17): error CS0246: The type or namespace name \'adddd\' could not be found (are you missing a using directive or an assembly reference?)"); } }
public async Task A_console_project_can_be_used_to_get_type_completion_with_a_space_in_the_name() { await Default.ConsoleWorkspace(); #region bufferSources var program = @"using System; using System.Linq; namespace FibonacciTest { public class Program { public static void Main() { foreach (var i in FibonacciGenerator.Fibonacci().Take(20)) { Console.WriteLine(i); } } } }".EnforceLF(); var generator = @"using System.Collections.Generic; using System; namespace FibonacciTest { public static class FibonacciGenerator { public static IEnumerable<int> Fibonacci() { int current = 1, next = 1; while (true) { yield return current; next = current + (current = next); Cons$$ } } } }".EnforceLF(); #endregion var package = await PackageUtilities.Copy(await Default.ConsoleWorkspace(), "a space"); var(processed, position) = CodeManipulation.ProcessMarkup(generator); var log = new LogEntryList(); using (LogEvents.Subscribe(log.Add)) using (var agent = new AgentService()) { var json = new WorkspaceRequest(activeBufferId: "generators/FibonacciGenerator.cs", requestId: "TestRun", workspace: Workspace.FromSources( package.Name, language: "csharp", ("Program.cs", program, 0), ("generators/FibonacciGenerator.cs", processed, position) )).ToJson(); var request = new HttpRequestMessage( HttpMethod.Post, @"/workspace/completion") { Content = new StringContent( json, Encoding.UTF8, "application/json") }; var response = await agent.SendAsync(request); var result = await response .EnsureSuccess() .DeserializeAs <CompletionResult>(); result.Items.Should().NotBeNullOrEmpty(); result.Items.Should().Contain(completion => completion.SortText == "Console"); } }
public async Task A_console_workspace_can_be_used_to_get_signature_help() { await Default.ConsoleWorkspace(); #region bufferSources var program = @"using System; using System.Linq; namespace FibonacciTest { public class Program { public static void Main() { foreach (var i in FibonacciGenerator.Fibonacci().Take(20)) { Console.WriteLine(i); } } } }".EnforceLF(); var generator = @"using System.Collections.Generic; using System; namespace FibonacciTest { public static class FibonacciGenerator { public static IEnumerable<int> Fibonacci() { int current = 1, next = 1; while (true) { yield return current; next = current + (current = next); Console.WriteLine($$); } } } }".EnforceLF(); #endregion var(processed, position) = CodeManipulation.ProcessMarkup(generator); var log = new LogEntryList(); using (LogEvents.Subscribe(log.Add)) using (var agent = new AgentService()) { var json = new WorkspaceRequest(activeBufferId: "generators/FibonacciGenerator.cs", requestId: "TestRun", workspace: Workspace.FromSources( workspaceType: "console", language: "csharp", ("Program.cs", program, 0), ("generators/FibonacciGenerator.cs", processed, position) )).ToJson(); var request = new HttpRequestMessage( HttpMethod.Post, @"/workspace/signaturehelp") { Content = new StringContent( json, Encoding.UTF8, "application/json") }; var response = await agent.SendAsync(request); var result = await response .EnsureSuccess() .DeserializeAs <SignatureHelpResult>(); result.Signatures.Should().NotBeNullOrEmpty(); result.Signatures.Should().Contain(diagnostic => diagnostic.Label == "void Console.WriteLine(string format, params object[] arg)"); } }