From 67b2b27ac9573d6b453cef1a53efbcc22048c12e Mon Sep 17 00:00:00 2001 From: Pawel Kadluczka Date: Mon, 11 Sep 2017 15:10:26 -0700 Subject: [PATCH] Enabling passing relative Url when creating connections --- .../Connection.spec.ts | 4 +++ .../HttpConnection.ts | 26 +++++++++++++++++-- .../wwwroot/js/hubConnectionTests.js | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/Connection.spec.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/Connection.spec.ts index 00b3e597ee..70120c0790 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/Connection.spec.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS.Tests/Connection.spec.ts @@ -9,6 +9,10 @@ import { ITransport, TransportType, TransferMode } from "../Microsoft.AspNetCore import { eachTransport } from "./Common"; describe("Connection", () => { + it("cannot be created with relative url if document object is not present", () => { + expect(() => new HttpConnection("/test")) + .toThrow(new Error("Cannot resolve '/test'.")); + }); it("starting connection fails if getting id fails", async (done) => { let options: IHttpConnectionOptions = { diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HttpConnection.ts b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HttpConnection.ts index 2549c72495..fcc76ce879 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HttpConnection.ts +++ b/client-ts/Microsoft.AspNetCore.SignalR.Client.TS/HttpConnection.ts @@ -34,10 +34,10 @@ export class HttpConnection implements IConnection { readonly features: any = {}; constructor(url: string, options: IHttpConnectionOptions = {}) { - this.url = url; + this.logger = LoggerFactory.createLogger(options.logging); + this.url = this.resolveUrl(url); options = options || {}; this.httpClient = options.httpClient || new HttpClient(); - this.logger = LoggerFactory.createLogger(options.logging); this.connectionState = ConnectionState.Initial; this.options = options; } @@ -156,6 +156,28 @@ export class HttpConnection implements IConnection { } } + private resolveUrl(url: string) : string { + // startsWith is not supported in IE + if (url.lastIndexOf("https://", 0) === 0 || url.lastIndexOf("http://", 0) === 0) { + return url; + } + + if (typeof window === 'undefined') { + throw new Error(`Cannot resolve '${url}'.`); + } + + let parser = window.document.createElement("a"); + parser.href = url; + + let baseUrl = (!parser.protocol || parser.protocol === ":") + ? `${window.document.location.protocol}//${(parser.host || window.document.location.host)}` + : `${parser.protocol}//${parser.host}`; + + let normalizedUrl = baseUrl + url; + this.logger.log(LogLevel.Information, `Normalizing '${url}' to '${normalizedUrl}'`); + return normalizedUrl; + } + onDataReceived: DataReceived; onClosed: ConnectionClosed; } \ No newline at end of file diff --git a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js index c53ba3d710..39e45f09aa 100644 --- a/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js +++ b/client-ts/Microsoft.AspNetCore.SignalR.Test.Server/wwwroot/js/hubConnectionTests.js @@ -3,7 +3,7 @@ 'use strict'; -var TESTHUBENDPOINT_URL = 'http://' + document.location.host + '/testhub'; +var TESTHUBENDPOINT_URL = '/testhub'; describe('hubConnection', function () { eachTransportAndProtocol(function (transportType, protocol) {