public static async Task StartApplicationAsync(short port, CancellationToken ct) { Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "test", EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("ASPNETCORE_URLS", $"http://::{port}", EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable($"{ConfigKeys.Gitlab}:Token", IntegrationUtils.gitlabToken, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable($"{ConfigKeys.Telegram}:Token", IntegrationUtils.telegramToken, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable($"{ConfigKeys.Telegram}:Channel", IntegrationUtils.telegramChannel, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable($"{ConfigKeys.Telegram}:Endpoint", IntegrationUtils.telegramEndpoint, EnvironmentVariableTarget.Process); await IntegrationUtils.WaitForThePortReleased(port); await Task.Run(() => Program.RunApplication(new string[0], ct), ct); }
public async Task Test_Get_Index_Returns_Ok() { using (this.output.UseAsSharedSingleton()) using (var cts = new CancellationTokenSource()) { Task start = IntegrationUtils.StartApplicationAsync(5000, cts.Token); await IntegrationUtils.WaitForThePortAcquired(5000); HttpResponseMessage message = await IntegrationUtils.HttpGetAsync(new Uri("http://localhost:5000")); Assert.Equal(HttpStatusCode.OK, message.StatusCode); cts.Cancel(); await start; } }
public async Task Test_Get_Index_Processes_Requests() { using (this.output.UseAsSharedSingleton()) using (var cts = new CancellationTokenSource()) using (IWebHost listener = IntegrationUtils.CreateListener()) { //arrange Task start = IntegrationUtils.StartApplicationAsync(5000, cts.Token); Task listen = listener.StartListenerAsync(cts.Token); //act await IntegrationUtils.WaitForThePortAcquired(5000); var uri = new Uri("http://localhost:5000/gitlab_hook"); object request = GitlabControllerTest.CreatePipelineFailedRequest(); HttpResponseMessage message = await IntegrationUtils.HttpPostAsync(uri, request); //assert - OK returned Assert.Equal(HttpStatusCode.OK, message.StatusCode); //assert - Telegram message was sent HttpRequest tgRequest = await listener.GetCapturedRequestAsync(); Assert.Equal("/bota-telegram-token/sendMessage", tgRequest.Path); using (var reader = new StreamReader(tgRequest.Body)) { string body = reader.ReadToEnd(); const string expected = "{\"chat_id\":\"a-telegram-channel\",\"text\":\"[A sample project](https://gitlab.com/group/project). The pipeline [10](https://gitlab.com/group/project/pipelines/10) has failed for the branch [master](https://gitlab.com/group/project/tree/master)!" + "\\r\\n\\r\\nThe last commit [567890j](https://gitlab.com/group/project/tree/567890jlkhgtfauygsih) " + "by *user_name*\\r\\nA simple commit message with \'quotes\' and \\\"Quotes\\\"\",\"parse_mode\":\"Markdown\"}"; Assert.Equal(expected, body); } //shutdown the servers cts.Cancel(); await Task.WhenAll(start, listen); } }