Add ability to change url in TS client (#11102)
This commit is contained in:
parent
14e7aca2ed
commit
79326220b4
File diff suppressed because one or more lines are too long
|
|
@ -750,6 +750,36 @@ describe("hubConnection", () => {
|
|||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("can change url in reconnecting state", async (done) => {
|
||||
try {
|
||||
const reconnectingPromise = new PromiseSource();
|
||||
const hubConnection = getConnectionBuilder(transportType)
|
||||
.withAutomaticReconnect()
|
||||
.build();
|
||||
|
||||
hubConnection.onreconnecting(() => {
|
||||
hubConnection.baseUrl = "http://example123.com";
|
||||
reconnectingPromise.resolve();
|
||||
});
|
||||
|
||||
await hubConnection.start();
|
||||
|
||||
// Induce reconnect
|
||||
(hubConnection as any).serverTimeout();
|
||||
|
||||
await reconnectingPromise;
|
||||
|
||||
expect(hubConnection.baseUrl).toBe("http://example123.com");
|
||||
|
||||
await hubConnection.stop();
|
||||
|
||||
done();
|
||||
} catch (err) {
|
||||
fail(err);
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("can reconnect after negotiate redirect", async (done) => {
|
||||
|
|
|
|||
|
|
@ -53,7 +53,6 @@ export class HttpConnection implements IConnection {
|
|||
// connectionStarted is tracked independently from connectionState, so we can check if the
|
||||
// connection ever did successfully transition from connecting to connected before disconnecting.
|
||||
private connectionStarted: boolean;
|
||||
private readonly baseUrl: string;
|
||||
private readonly httpClient: HttpClient;
|
||||
private readonly logger: ILogger;
|
||||
private readonly options: IHttpConnectionOptions;
|
||||
|
|
@ -66,6 +65,7 @@ export class HttpConnection implements IConnection {
|
|||
private sendQueue?: TransportSendQueue;
|
||||
|
||||
public readonly features: any = {};
|
||||
public baseUrl: string;
|
||||
public connectionId?: string;
|
||||
public onreceive: ((data: string | ArrayBuffer) => void) | null;
|
||||
public onclose: ((e?: Error) => void) | null;
|
||||
|
|
|
|||
|
|
@ -127,6 +127,28 @@ export class HubConnection {
|
|||
return this.connection ? (this.connection.connectionId || null) : null;
|
||||
}
|
||||
|
||||
/** Indicates the url of the {@link HubConnection} to the server. */
|
||||
get baseUrl(): string {
|
||||
return this.connection.baseUrl || "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or
|
||||
* Reconnecting states.
|
||||
* @param {string} url The url to connect to.
|
||||
*/
|
||||
set baseUrl(url: string) {
|
||||
if (this.connectionState !== HubConnectionState.Disconnected && this.connectionState !== HubConnectionState.Reconnecting) {
|
||||
throw new Error("The HubConnection must be in the Disconnected or Reconnecting state to change the url.");
|
||||
}
|
||||
|
||||
if (!url) {
|
||||
throw new Error("The HubConnection url must be a valid url.");
|
||||
}
|
||||
|
||||
this.connection.baseUrl = url;
|
||||
}
|
||||
|
||||
/** Starts the connection.
|
||||
*
|
||||
* @returns {Promise<void>} A Promise that resolves when the connection has been successfully established, or rejects with an error.
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@ export interface IConnection {
|
|||
readonly features: any;
|
||||
readonly connectionId?: string;
|
||||
|
||||
baseUrl: string;
|
||||
|
||||
start(transferFormat: TransferFormat): Promise<void>;
|
||||
send(data: string | ArrayBuffer): Promise<void>;
|
||||
stop(error?: Error): Promise<void>;
|
||||
|
|
|
|||
|
|
@ -41,6 +41,58 @@ describe("HubConnection", () => {
|
|||
});
|
||||
});
|
||||
|
||||
it("can change url", async () => {
|
||||
await VerifyLogger.run(async (logger) => {
|
||||
const connection = new TestConnection();
|
||||
const hubConnection = createHubConnection(connection, logger);
|
||||
try {
|
||||
await hubConnection.start();
|
||||
await hubConnection.stop();
|
||||
hubConnection.baseUrl = "http://newurl.com";
|
||||
expect(hubConnection.baseUrl).toBe("http://newurl.com");
|
||||
} finally {
|
||||
await hubConnection.stop();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("can change url in onclose", async () => {
|
||||
await VerifyLogger.run(async (logger) => {
|
||||
const connection = new TestConnection();
|
||||
const hubConnection = createHubConnection(connection, logger);
|
||||
try {
|
||||
await hubConnection.start();
|
||||
|
||||
expect(hubConnection.baseUrl).toBe("http://example.com");
|
||||
hubConnection.onclose(() => {
|
||||
hubConnection.baseUrl = "http://newurl.com";
|
||||
});
|
||||
|
||||
await hubConnection.stop();
|
||||
expect(hubConnection.baseUrl).toBe("http://newurl.com");
|
||||
} finally {
|
||||
await hubConnection.stop();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("changing url while active throws", async () => {
|
||||
await VerifyLogger.run(async (logger) => {
|
||||
const connection = new TestConnection();
|
||||
const hubConnection = createHubConnection(connection, logger);
|
||||
try {
|
||||
await hubConnection.start();
|
||||
|
||||
expect(() => {
|
||||
hubConnection.baseUrl = "http://newurl.com";
|
||||
}).toThrow("The HubConnection must be in the Disconnected or Reconnecting state to change the url.");
|
||||
|
||||
} finally {
|
||||
await hubConnection.stop();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it("state connected", async () => {
|
||||
await VerifyLogger.run(async (logger) => {
|
||||
const connection = new TestConnection();
|
||||
|
|
@ -1076,7 +1128,7 @@ describe("HubConnection", () => {
|
|||
hubConnection.stream("testMethod").subscribe(NullSubscriber.instance);
|
||||
|
||||
// Send completion to trigger observer.complete()
|
||||
// Expectation is connection.receive will not to throw
|
||||
// Expectation is connection.receive will not throw
|
||||
connection.receive({ type: MessageType.Completion, invocationId: connection.lastInvocationId });
|
||||
} finally {
|
||||
await hubConnection.stop();
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import { IConnection } from "../src/IConnection";
|
|||
import { TextMessageFormat } from "../src/TextMessageFormat";
|
||||
|
||||
export class TestConnection implements IConnection {
|
||||
public baseUrl: string;
|
||||
public readonly features: any = {};
|
||||
public connectionId?: string;
|
||||
|
||||
|
|
@ -22,6 +23,7 @@ export class TestConnection implements IConnection {
|
|||
this.sentData = [];
|
||||
this.lastInvocationId = null;
|
||||
this.autoHandshake = autoHandshake;
|
||||
this.baseUrl = "http://example.com";
|
||||
}
|
||||
|
||||
public start(): Promise<void> {
|
||||
|
|
|
|||
Loading…
Reference in New Issue