bruno/packages/bruno-lang/v2/tests/dictionary.spec.js

140 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-02-04 11:36:32 +01:00
/**
* This test file is used to test the text parser.
*/
const parser = require("../src/bruToJson");
const assertSingleHeader = (input) => {
2023-02-04 11:36:32 +01:00
const output = parser(input);
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);
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);
});
it("should parse single header with empty value", () => {
const input = `
headers {
hello:
}`;
2023-02-04 11:36:32 +01:00
const output = parser(input);
const expected = {
"headers": [{
"name": "hello",
"value": "",
"enabled": true
}]
};
expect(output).toEqual(expected);
});
it("should parse multi headers", () => {
const input = `
headers {
content-type: application/json
Authorization: JWT secret
}`;
2023-02-04 11:36:32 +01:00
const output = parser(input);
const expected = {
"headers": [{
"name": "content-type",
"value": "application/json",
"enabled": true
}, {
"name": "Authorization",
"value": "JWT secret",
"enabled": true
}]
};
expect(output).toEqual(expected);
});
it("should parse disabled headers", () => {
const input = `
headers {
~content-type: application/json
}`;
2023-02-04 11:36:32 +01:00
const output = parser(input);
const expected = {
"headers": [{
"name": "content-type",
"value": "application/json",
"enabled": false
}]
};
expect(output).toEqual(expected);
});
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();
});
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();
});
});