From 60d7ec5647cbc6e46440f94b34a06685e8c05d1b Mon Sep 17 00:00:00 2001 From: BrennanConroy Date: Tue, 22 May 2018 10:31:31 -0700 Subject: [PATCH] Add endpoint to samples for automated deployment usage --- build/publish-apps.ps1 | 2 +- clients/ts/FunctionalTests/Startup.cs | 34 ++++++++++++++++ clients/ts/FunctionalTests/ts/Common.ts | 3 +- .../FunctionalTests/ts/HubConnectionTests.ts | 15 +++++-- samples/SignalRSamples/Startup.cs | 39 +++++++++++++++++++ .../SkipIfDockerNotPresentAttribute.cs | 17 +------- 6 files changed, 88 insertions(+), 22 deletions(-) diff --git a/build/publish-apps.ps1 b/build/publish-apps.ps1 index 09531e26b3..a2d0062e79 100644 --- a/build/publish-apps.ps1 +++ b/build/publish-apps.ps1 @@ -1,4 +1,4 @@ -param($RootDirectory = (Get-Location), $Framework = "netcoreapp2.1", $Runtime = "win7-x64", $CommitHash, $BranchName, $BuildNumber) +param($RootDirectory = (Get-Location), $Framework = "netcoreapp2.2", $Runtime = "win7-x64", $CommitHash, $BranchName, $BuildNumber) # De-Powershell the path $RootDirectory = (Convert-Path $RootDirectory) diff --git a/clients/ts/FunctionalTests/Startup.cs b/clients/ts/FunctionalTests/Startup.cs index 23915e9ff3..ce37395f5d 100644 --- a/clients/ts/FunctionalTests/Startup.cs +++ b/clients/ts/FunctionalTests/Startup.cs @@ -3,6 +3,8 @@ using System; using System.IdentityModel.Tokens.Jwt; +using System.IO; +using System.Reflection; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication.JwtBearer; @@ -12,6 +14,8 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Connections; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; namespace FunctionalTests @@ -103,6 +107,36 @@ namespace FunctionalTests await context.Response.WriteAsync(GenerateJwtToken()); return; } + + if (context.Request.Path.StartsWithSegments("/deployment")) + { + var attributes = Assembly.GetAssembly(typeof(Startup)).GetCustomAttributes(); + + context.Response.ContentType = "application/json"; + using (var textWriter = new StreamWriter(context.Response.Body)) + using (var writer = new JsonTextWriter(textWriter)) + { + var json = new JObject(); + var commitHash = string.Empty; + + foreach (var attribute in attributes) + { + json.Add(attribute.Key, attribute.Value); + + if (string.Equals(attribute.Key, "CommitHash")) + { + commitHash = attribute.Value; + } + } + + if (!string.IsNullOrEmpty(commitHash)) + { + json.Add("GitHubUrl", $"https://github.com/aspnet/SignalR/commit/{commitHash}"); + } + + json.WriteTo(writer); + } + } }); } diff --git a/clients/ts/FunctionalTests/ts/Common.ts b/clients/ts/FunctionalTests/ts/Common.ts index 376c3733bd..54c4ff437d 100644 --- a/clients/ts/FunctionalTests/ts/Common.ts +++ b/clients/ts/FunctionalTests/ts/Common.ts @@ -4,7 +4,8 @@ import { HttpTransportType, IHubProtocol, JsonHubProtocol } from "@aspnet/signalr"; import { MessagePackHubProtocol } from "@aspnet/signalr-protocol-msgpack"; -export const ECHOENDPOINT_URL = "http://" + document.location.host + "/echo"; +export const ENDPOINT_BASE_URL = document.location.protocol + "//" + document.location.host; +export const ECHOENDPOINT_URL = ENDPOINT_BASE_URL + "/echo"; export function getHttpTransportTypes(): HttpTransportType[] { const transportTypes = []; diff --git a/clients/ts/FunctionalTests/ts/HubConnectionTests.ts b/clients/ts/FunctionalTests/ts/HubConnectionTests.ts index dabf807aeb..01ebafd9ad 100644 --- a/clients/ts/FunctionalTests/ts/HubConnectionTests.ts +++ b/clients/ts/FunctionalTests/ts/HubConnectionTests.ts @@ -4,7 +4,7 @@ import { AbortError, DefaultHttpClient, HttpClient, HttpRequest, HttpResponse, HttpTransportType, HubConnection, HubConnectionBuilder, IHttpConnectionOptions, IStreamSubscriber, JsonHubProtocol, LogLevel } from "@aspnet/signalr"; import { MessagePackHubProtocol } from "@aspnet/signalr-protocol-msgpack"; -import { eachTransport, eachTransportAndProtocol } from "./Common"; +import { eachTransport, eachTransportAndProtocol, ENDPOINT_BASE_URL } from "./Common"; import { TestLogger } from "./TestLogger"; const TESTHUBENDPOINT_URL = "/testhub"; @@ -345,7 +345,7 @@ describe("hubConnection", () => { }); it("closed with error if hub cannot be created", (done) => { - const hubConnection = getConnectionBuilder(transportType, "http://" + document.location.host + "/uncreatable") + const hubConnection = getConnectionBuilder(transportType, ENDPOINT_BASE_URL + "/uncreatable") .withHubProtocol(protocol) .build(); @@ -496,7 +496,7 @@ describe("hubConnection", () => { const message = "你好,世界!"; try { - const jwtToken = await getJwtToken("http://" + document.location.host + "/generateJwtToken"); + const jwtToken = await getJwtToken(ENDPOINT_BASE_URL + "/generateJwtToken"); const hubConnection = getConnectionBuilder(transportType, "/authorizedhub", { accessTokenFactory: () => jwtToken, @@ -525,7 +525,7 @@ describe("hubConnection", () => { try { const hubConnection = getConnectionBuilder(transportType, "/authorizedhub", { - accessTokenFactory: () => getJwtToken("http://" + document.location.host + "/generateJwtToken"), + accessTokenFactory: () => getJwtToken(ENDPOINT_BASE_URL + "/generateJwtToken"), }).build(); hubConnection.onclose((error) => { @@ -573,6 +573,8 @@ describe("hubConnection", () => { // Check what transport was used by asking the server to tell us. expect(await hubConnection.invoke("GetActiveTransportName")).toEqual("ServerSentEvents"); + + await hubConnection.stop(); done(); } catch (e) { fail(e); @@ -590,6 +592,8 @@ describe("hubConnection", () => { // Check what transport was used by asking the server to tell us. expect(await hubConnection.invoke("GetActiveTransportName")).toEqual("LongPolling"); + + await hubConnection.stop(); done(); } catch (e) { fail(e); @@ -614,6 +618,7 @@ describe("hubConnection", () => { // Make sure that we connect with SSE or LongPolling after Websockets fail const transportName = await hubConnection.invoke("GetActiveTransportName"); expect(transportName === "ServerSentEvents" || transportName === "LongPolling").toBe(true); + await hubConnection.stop(); } catch (e) { fail(e); } finally { @@ -681,6 +686,8 @@ describe("hubConnection", () => { expect(await hubConnection.invoke("GetActiveTransportName")).toEqual("LongPolling"); // Check to see that the Content-Type header is set the expected value expect(await hubConnection.invoke("GetContentTypeHeader")).toEqual("text/plain;charset=UTF-8"); + + await hubConnection.stop(); done(); } catch (e) { fail(e); diff --git a/samples/SignalRSamples/Startup.cs b/samples/SignalRSamples/Startup.cs index 2b7305f3c9..e2cbd56b6a 100644 --- a/samples/SignalRSamples/Startup.cs +++ b/samples/SignalRSamples/Startup.cs @@ -2,9 +2,14 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.IO; +using System.Reflection; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using SignalRSamples.ConnectionHandlers; using SignalRSamples.Hubs; @@ -62,6 +67,40 @@ namespace SignalRSamples { routes.MapConnectionHandler("/chat"); }); + + app.Use(next => (context) => + { + if (context.Request.Path.StartsWithSegments("/deployment")) + { + var attributes = Assembly.GetAssembly(typeof(Startup)).GetCustomAttributes(); + + context.Response.ContentType = "application/json"; + using (var textWriter = new StreamWriter(context.Response.Body)) + using (var writer = new JsonTextWriter(textWriter)) + { + var json = new JObject(); + var commitHash = string.Empty; + + foreach (var attribute in attributes) + { + json.Add(attribute.Key, attribute.Value); + + if (string.Equals(attribute.Key, "CommitHash")) + { + commitHash = attribute.Value; + } + } + + if (!string.IsNullOrEmpty(commitHash)) + { + json.Add("GitHubUrl", $"https://github.com/aspnet/SignalR/commit/{commitHash}"); + } + + json.WriteTo(writer); + } + } + return Task.CompletedTask; + }); } } } diff --git a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/SkipIfDockerNotPresentAttribute.cs b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/SkipIfDockerNotPresentAttribute.cs index b42e3f0778..3df0d769a7 100644 --- a/test/Microsoft.AspNetCore.SignalR.Redis.Tests/SkipIfDockerNotPresentAttribute.cs +++ b/test/Microsoft.AspNetCore.SignalR.Redis.Tests/SkipIfDockerNotPresentAttribute.cs @@ -11,31 +11,16 @@ namespace Microsoft.AspNetCore.SignalR.Redis.Tests { public bool IsMet => CheckDocker(); public string SkipReason { get; private set; } = "Docker is not available"; - public string RequiredOsType { get; } - - public SkipIfDockerNotPresentAttribute() : this("linux") - { - - } - - public SkipIfDockerNotPresentAttribute(string requiredOSType) - { - RequiredOsType = requiredOSType; - } private bool CheckDocker() { if(Docker.Default != null) { // Docker is present, but is it working? - if (Docker.Default.RunCommand("info -f {{.OSType}}", out var output) != 0) + if (Docker.Default.RunCommand("ps", out var output) != 0) { SkipReason = $"Failed to invoke test command 'docker ps'. Output: {output}"; } - else if (!string.Equals(output.Trim(), RequiredOsType, StringComparison.Ordinal)) - { - SkipReason = $"Docker tests do not support the OS type '{output.Trim()}', they require '{RequiredOsType}'."; - } else { // We have a docker