2023-02-04 11:36:32 +01:00
|
|
|
/**
|
|
|
|
* This test file is used to test the text parser.
|
|
|
|
*/
|
|
|
|
|
2023-02-04 15:41:33 +01:00
|
|
|
const parser = require("../src/bruToJson");
|
2023-02-03 16:38:40 +01:00
|
|
|
|
|
|
|
const assertSingleHeader = (input) => {
|
2023-02-04 11:36:32 +01:00
|
|
|
const output = parser(input);
|
2023-02-03 16:38:40 +01:00
|
|
|
|
|
|
|
const expected = {
|
|
|
|
"headers": [{
|
|
|
|
"name": "hello",
|
|
|
|
"value": "world",
|
|
|
|
"enabled": true
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
expect(output).toEqual(expected);
|
|
|
|
};
|
|
|
|
|
|
|
|
describe("headers parser", () => {
|
|
|
|
it("should parse empty header", () => {
|
|
|
|
const input = `
|
|
|
|
headers {
|
|
|
|
}`;
|
|
|
|
|
2023-02-04 11:36:32 +01:00
|
|
|
const output = parser(input);
|
2023-02-03 16:38:40 +01:00
|
|
|
const expected = {
|
|
|
|
"headers": []
|
|
|
|
};
|
|
|
|
expect(output).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse single header", () => {
|
|
|
|
const input = `
|
|
|
|
headers {
|
|
|
|
hello: world
|
|
|
|
}`;
|
|
|
|
|
|
|
|
assertSingleHeader(input);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse single header with spaces", () => {
|
|
|
|
const input = `
|
|
|
|
headers {
|
|
|
|
hello: world
|
|
|
|
}`;
|
|
|
|
|
|
|
|
assertSingleHeader(input);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should parse single header with spaces and newlines", () => {
|
|
|
|
const input = `
|
|
|
|
headers {
|
|
|
|
|
|
|
|
hello: world
|
|
|
|
|
|
|
|
|
|
|
|
}`;
|
|
|
|
|
|
|
|
assertSingleHeader(input);
|
|
|
|
});
|
|
|
|
|
2023-02-03 18:32:16 +01:00
|
|
|
it("should parse single header with empty value", () => {
|
|
|
|
const input = `
|
|
|
|
headers {
|
|
|
|
hello:
|
|
|
|
}`;
|
|
|
|
|
2023-02-04 11:36:32 +01:00
|
|
|
const output = parser(input);
|
2023-02-03 18:32:16 +01:00
|
|
|
const expected = {
|
|
|
|
"headers": [{
|
|
|
|
"name": "hello",
|
|
|
|
"value": "",
|
|
|
|
"enabled": true
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
expect(output).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
2023-02-03 16:38:40 +01:00
|
|
|
it("should parse multi headers", () => {
|
|
|
|
const input = `
|
|
|
|
headers {
|
2023-02-03 17:14:07 +01:00
|
|
|
content-type: application/json
|
2023-02-06 20:48:18 +01:00
|
|
|
|
2023-02-03 17:14:07 +01:00
|
|
|
Authorization: JWT secret
|
2023-02-03 16:38:40 +01:00
|
|
|
}`;
|
|
|
|
|
2023-02-04 11:36:32 +01:00
|
|
|
const output = parser(input);
|
2023-02-03 16:38:40 +01:00
|
|
|
const expected = {
|
|
|
|
"headers": [{
|
2023-02-03 17:14:07 +01:00
|
|
|
"name": "content-type",
|
|
|
|
"value": "application/json",
|
2023-02-03 16:38:40 +01:00
|
|
|
"enabled": true
|
|
|
|
}, {
|
2023-02-03 17:14:07 +01:00
|
|
|
"name": "Authorization",
|
|
|
|
"value": "JWT secret",
|
2023-02-03 16:38:40 +01:00
|
|
|
"enabled": true
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
expect(output).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
2023-02-03 18:57:06 +01:00
|
|
|
it("should parse disabled headers", () => {
|
|
|
|
const input = `
|
2023-02-05 18:43:18 +01:00
|
|
|
headers {
|
|
|
|
~content-type: application/json
|
2023-02-03 18:57:06 +01:00
|
|
|
}`;
|
|
|
|
|
2023-02-04 11:36:32 +01:00
|
|
|
const output = parser(input);
|
2023-02-03 18:57:06 +01:00
|
|
|
const expected = {
|
|
|
|
"headers": [{
|
|
|
|
"name": "content-type",
|
|
|
|
"value": "application/json",
|
|
|
|
"enabled": false
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
expect(output).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
2023-02-03 16:38:40 +01:00
|
|
|
it("should throw error on invalid header", () => {
|
|
|
|
const input = `
|
|
|
|
headers {
|
|
|
|
hello: world
|
|
|
|
foo
|
|
|
|
}`;
|
|
|
|
|
2023-02-04 11:36:32 +01:00
|
|
|
expect(() => parser(input)).toThrow();
|
2023-02-03 16:38:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should throw error on invalid header", () => {
|
|
|
|
const input = `
|
|
|
|
headers {
|
|
|
|
hello: world
|
|
|
|
foo: bar}`;
|
|
|
|
|
2023-02-04 11:36:32 +01:00
|
|
|
expect(() => parser(input)).toThrow();
|
2023-02-03 16:38:40 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|