Jfheins/improve error message (#21295)

* Add test for empty URL

* Throw if URL is empty, adjust test
This commit is contained in:
Julius 2020-04-30 21:46:31 +02:00 committed by GitHub
parent 8271204317
commit 63415b7dd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 3 deletions

View File

@ -122,6 +122,7 @@ export class HubConnectionBuilder {
public withUrl(url: string, options: IHttpConnectionOptions): HubConnectionBuilder;
public withUrl(url: string, transportTypeOrOptions?: IHttpConnectionOptions | HttpTransportType): HubConnectionBuilder {
Arg.isRequired(url, "url");
Arg.isNotEmpty(url, "url");
this.url = url;

View File

@ -19,6 +19,11 @@ export class Arg {
throw new Error(`The '${name}' argument is required.`);
}
}
public static isNotEmpty(val: string, name: string): void {
if (!val || val.match(/^\s*$/)) {
throw new Error(`The '${name}' argument should not be empty.`);
}
}
public static isIn(val: any, values: any, name: string): void {
// TypeScript enums have keys for **both** the name and the value of each enum member on the type itself.

View File

@ -73,12 +73,14 @@ class CapturingConsole {
registerUnhandledRejectionHandler();
describe("HubConnectionBuilder", () => {
eachMissingValue((val, name) => {
it(`withUrl throws if url is ${name}`, () => {
for (const val of [undefined, null, ""]) {
it(`withUrl throws if url is ${String(val)}`, () => {
const builder = new HubConnectionBuilder();
expect(() => builder.withUrl(val!)).toThrow("The 'url' argument is required.");
expect(() => builder.withUrl(val!)).toThrow(/The 'url' argument (is required|should not be empty)./);
});
}
eachMissingValue((val, name) => {
it(`withHubProtocol throws if protocol is ${name}`, () => {
const builder = new HubConnectionBuilder();
expect(() => builder.withHubProtocol(val!)).toThrow("The 'protocol' argument is required.");