зеркало из https://github.com/Azure/ms-rest-js.git
Add base64UrlSpec and fix indentation
This commit is contained in:
Родитель
56c7abe59e
Коммит
8a56c7a450
|
@ -0,0 +1,27 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
import { TypeSpec, createValidationErrorMessage } from "./typeSpec";
|
||||
|
||||
/**
|
||||
* A type specification that describes how to validate and serialize a Base64Url encoded ByteArray.
|
||||
*/
|
||||
const byteArraySpec: TypeSpec<string> = {
|
||||
typeName: "Base64Url",
|
||||
|
||||
serialize(propertyPath: string[], value: any): string {
|
||||
if (!value || typeof value.constructor.isBuffer !== "function" || !value.constructor.isBuffer(value)) {
|
||||
throw new Error(createValidationErrorMessage(propertyPath, value, "a Buffer"));
|
||||
}
|
||||
|
||||
const result: string = value.toString("base64");
|
||||
|
||||
let trimmedResultLength = result.length;
|
||||
while ((trimmedResultLength - 1) >= 0 && result[trimmedResultLength - 1] === "=") {
|
||||
--trimmedResultLength;
|
||||
}
|
||||
|
||||
return result.substr(0, trimmedResultLength).replace(/\+/g, "-").replace(/\//g, "_");
|
||||
}
|
||||
};
|
||||
|
||||
export default byteArraySpec;
|
|
@ -6,11 +6,11 @@ import { TypeSpec, createValidationErrorMessage } from "./typeSpec";
|
|||
* A type specification that describes how to validate and serialize a ByteArray.
|
||||
*/
|
||||
const byteArraySpec: TypeSpec<string> = {
|
||||
typeName: "ByteArray(Buffer)",
|
||||
typeName: "ByteArray",
|
||||
|
||||
serialize(propertyPath: string[], value: any): string {
|
||||
if (!value || typeof value.constructor.isBuffer !== "function" || !value.constructor.isBuffer(value)) {
|
||||
throw new Error(createValidationErrorMessage(propertyPath, value, "a ByteArray(Buffer)"));
|
||||
throw new Error(createValidationErrorMessage(propertyPath, value, "a Buffer"));
|
||||
}
|
||||
return value.toString("base64");
|
||||
}
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
import * as assert from "assert";
|
||||
import base64UrlSpec from "../../lib/serialization/base64UrlSpec";
|
||||
|
||||
describe("base64UrlSpec", () => {
|
||||
it("should have \"Base64Url\" for its typeName property", () => {
|
||||
assert.strictEqual("Base64Url", base64UrlSpec.typeName);
|
||||
});
|
||||
|
||||
describe("serialize()", () => {
|
||||
it("should throw an error when given undefined", () => {
|
||||
try {
|
||||
base64UrlSpec.serialize(["a", "property", "path"], undefined);
|
||||
assert.fail("Expected an error to be thrown.");
|
||||
} catch (error) {
|
||||
assert.strictEqual(error.message, "Property a.property.path with value undefined must be a Buffer.");
|
||||
}
|
||||
});
|
||||
|
||||
it("should throw an error when given 5", () => {
|
||||
try {
|
||||
base64UrlSpec.serialize(["another", "property", "path"], 5);
|
||||
assert.fail("Expected an error to be thrown.");
|
||||
} catch (error) {
|
||||
assert.strictEqual(error.message, "Property another.property.path with value 5 must be a Buffer.");
|
||||
}
|
||||
});
|
||||
|
||||
it("should throw an error when given {}", () => {
|
||||
try {
|
||||
base64UrlSpec.serialize(["another", "property", "path"], {});
|
||||
assert.fail("Expected an error to be thrown.");
|
||||
} catch (error) {
|
||||
assert.strictEqual(error.message, "Property another.property.path with value {} must be a Buffer.");
|
||||
}
|
||||
});
|
||||
|
||||
it("should return a base64 encoded string with no error when given a Buffer", () => {
|
||||
assert.strictEqual(base64UrlSpec.serialize(["this", "one", "works"], new Buffer([0, 1, 2, 3, 4])), "AAECAwQ");
|
||||
});
|
||||
});
|
||||
});
|
|
@ -4,8 +4,8 @@ import * as assert from "assert";
|
|||
import byteArraySpec from "../../lib/serialization/byteArraySpec";
|
||||
|
||||
describe("byteArraySpec", () => {
|
||||
it("should have \"ByteArray(Buffer)\" for its typeName property", () => {
|
||||
assert.strictEqual("ByteArray(Buffer)", byteArraySpec.typeName);
|
||||
it("should have \"ByteArray\" for its typeName property", () => {
|
||||
assert.strictEqual("ByteArray", byteArraySpec.typeName);
|
||||
});
|
||||
|
||||
describe("serialize()", () => {
|
||||
|
@ -14,7 +14,7 @@ describe("byteArraySpec", () => {
|
|||
byteArraySpec.serialize(["a", "property", "path"], undefined);
|
||||
assert.fail("Expected an error to be thrown.");
|
||||
} catch (error) {
|
||||
assert.strictEqual(error.message, "Property a.property.path with value undefined must be a ByteArray(Buffer).");
|
||||
assert.strictEqual(error.message, "Property a.property.path with value undefined must be a Buffer.");
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -23,7 +23,7 @@ describe("byteArraySpec", () => {
|
|||
byteArraySpec.serialize(["another", "property", "path"], 5);
|
||||
assert.fail("Expected an error to be thrown.");
|
||||
} catch (error) {
|
||||
assert.strictEqual(error.message, "Property another.property.path with value 5 must be a ByteArray(Buffer).");
|
||||
assert.strictEqual(error.message, "Property another.property.path with value 5 must be a Buffer.");
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -32,7 +32,7 @@ describe("byteArraySpec", () => {
|
|||
byteArraySpec.serialize(["another", "property", "path"], {});
|
||||
assert.fail("Expected an error to be thrown.");
|
||||
} catch (error) {
|
||||
assert.strictEqual(error.message, "Property another.property.path with value {} must be a ByteArray(Buffer).");
|
||||
assert.strictEqual(error.message, "Property another.property.path with value {} must be a Buffer.");
|
||||
}
|
||||
});
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче