Function for data parsing based on the data content type
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
1766647700
commit
588a0aa3ac
|
@ -31,6 +31,16 @@ const isBase64 = (value) =>
|
|||
const clone = (o) =>
|
||||
JSON.parse(JSON.stringify(o));
|
||||
|
||||
const isJsonContentType = (contentType) =>
|
||||
contentType && contentType.match(/(json)/i);
|
||||
|
||||
const asData = (data, contentType) =>
|
||||
((typeof data) !== "string"
|
||||
? data
|
||||
: isJsonContentType(contentType)
|
||||
? JSON.parse(data)
|
||||
: data);
|
||||
|
||||
module.exports = {
|
||||
isString,
|
||||
isStringOrThrow,
|
||||
|
@ -42,5 +52,7 @@ module.exports = {
|
|||
|
||||
equalsOrThrow,
|
||||
isBase64,
|
||||
clone
|
||||
clone,
|
||||
|
||||
asData
|
||||
};
|
||||
|
|
|
@ -43,4 +43,67 @@ describe("Functional approach", () => {
|
|||
expect(actual).to.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("asData" , () => {
|
||||
it("should throw error when data is not a valid json", () => {
|
||||
let data = "not a json";
|
||||
|
||||
expect(fun.asData.bind(fun, data, "application/json"))
|
||||
.to
|
||||
.throws();
|
||||
});
|
||||
|
||||
it("should parse string content type as string", () => {
|
||||
let expected = "a string";
|
||||
|
||||
let actual = fun.asData(expected, "text/plain");
|
||||
|
||||
expect((typeof actual)).to.equal("string");
|
||||
expect(actual).to.equal(expected);
|
||||
});
|
||||
|
||||
it("should parse 'application/json' as json object", () => {
|
||||
let expected = {
|
||||
much: "wow",
|
||||
myext : {
|
||||
ext : "x04"
|
||||
}
|
||||
};
|
||||
|
||||
let actual = fun.asData(JSON.stringify(expected), "application/json");
|
||||
|
||||
expect((typeof actual)).to.equal("object");
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
|
||||
it("should parse 'application/cloudevents+json' as json object", () => {
|
||||
let expected = {
|
||||
much: "wow",
|
||||
myext : {
|
||||
ext : "x04"
|
||||
}
|
||||
};
|
||||
|
||||
let actual = fun.asData(JSON.stringify(expected),
|
||||
"application/cloudevents+json");
|
||||
|
||||
expect((typeof actual)).to.equal("object");
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
|
||||
it("should parse 'text/json' as json object", () => {
|
||||
let expected = {
|
||||
much: "wow",
|
||||
myext : {
|
||||
ext : "x04"
|
||||
}
|
||||
};
|
||||
|
||||
let actual = fun.asData(JSON.stringify(expected),
|
||||
"text/json");
|
||||
|
||||
expect((typeof actual)).to.equal("object");
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue