2023-02-04 11:36:32 +01:00
|
|
|
/**
|
2023-02-09 13:01:37 +01:00
|
|
|
* This test file is used to test the dictionary parser.
|
2023-02-04 11:36:32 +01:00
|
|
|
*/
|
|
|
|
|
2023-09-21 21:12:48 +02: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 = {
|
2023-09-21 21:12:48 +02:00
|
|
|
headers: [
|
|
|
|
{
|
|
|
|
name: 'hello',
|
|
|
|
value: 'world',
|
|
|
|
enabled: true
|
|
|
|
}
|
|
|
|
]
|
2023-02-03 16:38:40 +01:00
|
|
|
};
|
|
|
|
expect(output).toEqual(expected);
|
|
|
|
};
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
describe('headers parser', () => {
|
|
|
|
it('should parse empty header', () => {
|
2023-02-03 16:38:40 +01:00
|
|
|
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 = {
|
2023-09-21 21:12:48 +02:00
|
|
|
headers: []
|
2023-02-03 16:38:40 +01:00
|
|
|
};
|
|
|
|
expect(output).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
it('should parse single header', () => {
|
2023-02-03 16:38:40 +01:00
|
|
|
const input = `
|
|
|
|
headers {
|
|
|
|
hello: world
|
|
|
|
}`;
|
|
|
|
|
|
|
|
assertSingleHeader(input);
|
|
|
|
});
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
it('should parse single header with spaces', () => {
|
2023-02-03 16:38:40 +01:00
|
|
|
const input = `
|
|
|
|
headers {
|
|
|
|
hello: world
|
|
|
|
}`;
|
|
|
|
|
|
|
|
assertSingleHeader(input);
|
|
|
|
});
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
it('should parse single header with spaces and newlines', () => {
|
2023-02-03 16:38:40 +01:00
|
|
|
const input = `
|
|
|
|
headers {
|
|
|
|
|
|
|
|
hello: world
|
|
|
|
|
|
|
|
|
|
|
|
}`;
|
|
|
|
|
|
|
|
assertSingleHeader(input);
|
|
|
|
});
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
it('should parse single header with empty value', () => {
|
2023-02-03 18:32:16 +01:00
|
|
|
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 = {
|
2023-09-21 21:12:48 +02:00
|
|
|
headers: [
|
|
|
|
{
|
|
|
|
name: 'hello',
|
|
|
|
value: '',
|
|
|
|
enabled: true
|
|
|
|
}
|
|
|
|
]
|
2023-02-03 18:32:16 +01:00
|
|
|
};
|
|
|
|
expect(output).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
it('should parse multi headers', () => {
|
2023-02-03 16:38:40 +01:00
|
|
|
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 = {
|
2023-09-21 21:12:48 +02:00
|
|
|
headers: [
|
|
|
|
{
|
|
|
|
name: 'content-type',
|
|
|
|
value: 'application/json',
|
|
|
|
enabled: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Authorization',
|
|
|
|
value: 'JWT secret',
|
|
|
|
enabled: true
|
|
|
|
}
|
|
|
|
]
|
2023-02-03 16:38:40 +01:00
|
|
|
};
|
|
|
|
expect(output).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
it('should parse disabled headers', () => {
|
2023-02-03 18:57:06 +01:00
|
|
|
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 = {
|
2023-09-21 21:12:48 +02:00
|
|
|
headers: [
|
|
|
|
{
|
|
|
|
name: 'content-type',
|
|
|
|
value: 'application/json',
|
|
|
|
enabled: false
|
|
|
|
}
|
|
|
|
]
|
2023-02-03 18:57:06 +01:00
|
|
|
};
|
|
|
|
expect(output).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
it('should parse empty url', () => {
|
2023-02-09 13:01:37 +01:00
|
|
|
const input = `
|
|
|
|
get {
|
|
|
|
url:
|
|
|
|
body: json
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const output = parser(input);
|
|
|
|
const expected = {
|
2023-09-21 21:12:48 +02:00
|
|
|
http: {
|
|
|
|
url: '',
|
|
|
|
method: 'get',
|
|
|
|
body: 'json'
|
2023-02-09 13:01:37 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
expect(output).toEqual(expected);
|
|
|
|
});
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
it('should throw error on invalid header', () => {
|
2023-02-03 16:38:40 +01:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2023-09-21 21:12:48 +02:00
|
|
|
it('should throw error on invalid header', () => {
|
2023-02-03 16:38:40 +01:00
|
|
|
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
|
|
|
});
|
|
|
|
});
|