Enabling passing relative Url when creating connections
This commit is contained in:
parent
bb79a9760c
commit
67b2b27ac9
|
|
@ -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 = {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue