Files
bruno/packages/bruno-lang/v2/tests/examples/examples.spec.js
sanish chirayath 68cbb7d9df Feat/add import export support for examples (#5936)
* feat: enhance Bru grammar to support response blocks and examples

- Added new grammar rules for response headers, status, and body types (JSON, XML, text).
- Introduced parsing logic for example blocks, allowing multiple examples with various body types.
- Implemented tests for example parsing, including edge cases and complex examples with authentication.
- Created fixture files for simple and complex examples to validate parsing functionality.

feat: extend jsonToBru functionality to support response handling and examples

- Updated jsonToBru to include parsing for response headers, status, and body types (JSON, XML, text).
- Enhanced example handling to support multiple examples with various body types.
- Added comprehensive tests for example parsing, including edge cases and complex scenarios with authentication.
- Created fixture files for testing the new features and validating parsing functionality.

move: files to fixtures folder

refactor: simplify response body handling in Bru grammar and JSON conversion

- Removed specific body type handling (JSON, XML, text) from grammar and semantics.
- Updated response body parsing in jsonToBru to handle a unified response body format.
- Adjusted tests and fixtures to reflect changes in response body structure, ensuring compatibility with the new format.

feat: add response bookmarking functionality to ResponsePane

- Introduced ResponseBookmark component to allow users to save responses as examples.
- Added NameExampleModal for naming saved examples.
- Updated ResponsePane to include the new bookmarking feature.
- Implemented Redux actions to manage response examples in the collections state.
- Enhanced CollectionItem to display saved examples and allow for expansion.

fix: remove unnecessary padding from ExampleItem component

feat: implement delete and rename functionality for examples in ExampleItem component

- Added DeleteExampleModal for confirming deletion of examples.
- Integrated modal for renaming examples with state management.
- Enhanced ExampleItem to handle example deletion and renaming through modals.
- Updated Redux actions to support example updates and deletions in the collections state.

fix: example writing to  disc properly

fix: example parsing errors

fix: request with example parsing error

fix: handle examples in collections and requests

feat: implement response example functionality in the application

- Added ResponseExample component to handle displaying and editing response examples.
- Integrated ResponseExampleRequestPane and ResponseExampleResponsePane for structured request and response handling.
- Enhanced RequestTabPanel and RequestTab components to support response-example tabs.
- Introduced new styled components for better UI/UX in response examples.
- Updated theme files to include styles for response examples.
- Implemented URL bar for editing request URLs in response examples.
- Added functionality for managing headers and parameters in response examples.
- Improved overall structure and organization of response example components.

add styles for example url bar

feat: add Checkbox component and Table-v2 for enhanced UI

- Introduced a new Checkbox component for better user interaction in forms.
- Added Table-v2 component to improve table rendering and resizing functionality.
- Updated existing components to utilize the new Checkbox and Table-v2 for managing headers and parameters in response examples.
- Enhanced styling for better visual consistency across components.
- Updated theme files to include styles for the new components.

feat: implement custom scrollbar styles for response example components

fix: features

add actions , view more

feat: enhance response example functionality

- Added GenerateCodeItem component for generating code snippets from response examples.
- Integrated modal for code generation within ResponseExample component.
- Updated ResponseExampleTopBar to handle example name and description editing.
- Improved state management for response examples, including new actions for updating names and descriptions.
- Enhanced ResponseExampleRequestPane to support editing and saving request details.
- Refactored URL handling in ResponseExampleUrlBar to utilize example-specific data.
- Improved overall user experience with better UI elements and state management.

feat: enhance response example management and UI components

feat: enhance editing capabilities in response example components

feat: update multipart form parameter handling in response examples

feat: refactor response example parameter handling and enhance UI interactions

feat: introduce RadioButton component and update Checkbox usage in response examples

fix: styles

fix radio button styling

fixed radio button styles

feat: add create example from sidebar

feat: enhance ResponseExample components with layout adjustments and new HeightBoundContainer

feat: add Checkbox and RadioButton components with comprehensive tests for rendering, user interactions, and accessibility

feat: playwright test csaes

rm: comments

fix: linting

fix: tests

refactor: update response example tests and enhance functionality

fix: tests

fix: e2e-tests

refactor: implement hasRequestChanges utility for better change detection

rm: console

rm: consoles

fix: lint

fix: tests

fix: response header disabled by default issue

Feat/with bru example parser (#5892)

* fix: response header disabled by default issue

feat: new parsing logic

fix: change test cases to accomodate new brulang

add: path params features

rm:consoles

six: make tab permanent on double click

fix width

feat: add status editing

feat: review fixes

review fixes

fix: review fixes

fix: post review

mv: test files

fix: review

* fix: lint

* fix: review comments

* fix: icons folder strcuture

fix: tests

fix: lint

fix: unit tests

feat: body mode selector

fix: close all collections

rm: example

feat added tests. lang change

feat: add custom status text

fix: status update

feat: add body mode, update tests

add default name prefilled for example

fix: active tab styles, prefilled name, text fixes

fix : pkg lock

fix: review

fix: review comments

fix: hide cursor when readonly

fix: height

fix: null body

fix: response body parsing

fix: test cases

feat: add method support for examples

fix: reponse parsing

fix: update response body type when content type is updated

rm : commented code

feat: update parser logic

fix: organize files

feat: enhance examples handling in collection export and import

feat: postman imports fro examples

feat: enhance OpenAPI import functionality to support examples

feat: support postman export

fix: postman export import

fix: open api tests, remove requestbody related logic

rm: examples

fix:  move common attributes files

ui fixes

fix: clone issue

fix: create example from request menu

review fixes

more review fixes

mv: files, fix mode req error

organize files

fix:tests

fix: save dot issue

fix: bugs

fix: postman export

fix: import path params

* chore:improve modal handling in environment and response example tests

fix: test issues resolved

* chore: update response example tests to use new fixture files and improve cleanup logic

---------

Co-authored-by: Abhishek S Lal <abhishek@usebruno.com>
Co-authored-by: Bijin Bruno <bijin@usebruno.com>
2025-11-01 05:56:11 +05:30

310 lines
11 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const bruToJson = require('../../src/bruToJson');
const jsonToBru = require('../../src/jsonToBru');
describe('Examples functionality', () => {
describe('Fixture-based tests', () => {
it('should parse examples-simple.bru correctly', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'examples-simple.bru'), 'utf8');
const expected = require('./fixtures/json/examples-simple.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
it('should parse examples-complex.bru correctly', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'examples-complex.bru'), 'utf8');
const output = bruToJson(input);
// Basic structure validation
expect(output.meta).toBeDefined();
expect(output.http).toBeDefined();
expect(output.examples).toBeDefined();
expect(Array.isArray(output.examples)).toBe(true);
expect(output.examples).toHaveLength(3);
// Check each example has the expected structure
output.examples.forEach((example, index) => {
expect(example.name).toBeDefined();
expect(example.description).toBeDefined();
expect(example.request).toBeDefined();
expect(example.request.url).toBeDefined();
if (example.response) {
expect(example.response.status).toBeDefined();
expect(example.response.body).toBeDefined();
}
});
// Check specific examples
const jsonExample = output.examples[0];
expect(jsonExample.name).toBe('JSON API Example');
expect(jsonExample.request.url).toBeDefined();
if (jsonExample.request.body && jsonExample.request.body.json) {
expect(jsonExample.request.body.json).toContain('"format": "json"');
}
const xmlExample = output.examples[1];
expect(xmlExample.name).toBe('XML API Example');
if (xmlExample.request.body && xmlExample.request.body.xml) {
expect(xmlExample.request.body.xml).toContain('<format>xml</format>');
}
const textExample = output.examples[2];
expect(textExample.name).toBe('Text API Example');
});
});
describe('Basic examples parsing', () => {
it('should parse a single example block', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'bruToJson-single-example.bru'), 'utf8');
const expected = require('./fixtures/json/bruToJson-single-example.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
it('should parse multiple example blocks', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'bruToJson-multiple-examples.bru'), 'utf8');
const expected = require('./fixtures/json/bruToJson-multiple-examples.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
it('should handle examples with response blocks', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'bruToJson-response-example.bru'), 'utf8');
const expected = require('./fixtures/json/bruToJson-response-example.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
});
describe('Examples with different body types', () => {
it('should handle examples with JSON body', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'bruToJson-json-body.bru'), 'utf8');
const expected = require('./fixtures/json/bruToJson-json-body.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
it('should handle examples with XML body', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'bruToJson-xml-body.bru'), 'utf8');
const expected = require('./fixtures/json/bruToJson-xml-body.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
it('should handle examples with text body', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'bruToJson-text-body.bru'), 'utf8');
const expected = require('./fixtures/json/bruToJson-text-body.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
});
describe('Edge cases', () => {
it('should handle empty example blocks', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'bruToJson-empty-example.bru'), 'utf8');
const expected = require('./fixtures/json/bruToJson-empty-example.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
it('should work without any examples', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'bruToJson-no-examples.bru'), 'utf8');
const expected = require('./fixtures/json/bruToJson-no-examples.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
});
describe('jsonToBru conversion', () => {
it('should convert JSON with examples to BRU format', () => {
const jsonInput = require('./fixtures/json/jsonToBru-simple.json');
const expected = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'jsonToBru-simple.bru'), 'utf8');
const output = jsonToBru(jsonInput);
expect(output).toEqual(expected);
});
it('should handle multiple examples correctly', () => {
const jsonInput = require('./fixtures/json/jsonToBru-multiple.json');
const expected = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'jsonToBru-multiple.bru'), 'utf8');
const output = jsonToBru(jsonInput);
expect(output).toEqual(expected);
});
it('should handle examples with response blocks', () => {
const jsonInput = require('./fixtures/json/jsonToBru-response.json');
const expected = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'jsonToBru-response.bru'), 'utf8');
const output = jsonToBru(jsonInput);
expect(output).toEqual(expected);
});
it('should handle examples with different body types', () => {
const jsonInput = require('./fixtures/json/jsonToBru-bodytypes.json');
const expected = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'jsonToBru-bodytypes.bru'), 'utf8');
const output = jsonToBru(jsonInput);
expect(output).toEqual(expected);
});
it('should handle round-trip conversion correctly', () => {
const originalBru = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'examples-simple.bru'), 'utf8');
const jsonFromBru = bruToJson(originalBru);
const bruFromJson = jsonToBru(jsonFromBru);
const jsonFromBruAgain = bruToJson(bruFromJson);
// The examples should be preserved through the round-trip
expect(jsonFromBruAgain.examples).toBeDefined();
expect(Array.isArray(jsonFromBruAgain.examples)).toBe(true);
expect(jsonFromBruAgain.examples).toHaveLength(2);
expect(jsonFromBruAgain.examples[0].name).toBe('Get User by ID');
expect(jsonFromBruAgain.examples[1].name).toBe('Create New User');
});
it('should handle empty examples array', () => {
const jsonInput = {
meta: {
name: 'No Examples API',
type: 'http'
},
http: {
method: 'get',
url: 'https://api.example.com/test'
},
examples: []
};
const expected = `meta {
name: No Examples API
type: http
}
get {
url: https://api.example.com/test
}
`;
const output = jsonToBru(jsonInput);
expect(output).toEqual(expected);
});
it('should handle examples with minimal structure', () => {
const jsonInput = {
meta: {
name: 'Test API',
type: 'http'
},
http: {
method: 'get',
url: 'https://api.example.com/test'
},
examples: [
{
name: 'Example Request',
description: 'A simple example',
request: {
url: 'https://api.example.com/example'
}
}
]
};
const expected = `meta {
name: Test API
type: http
}
get {
url: https://api.example.com/test
}
example {
name: Example Request
description: A simple example
request: {
url: https://api.example.com/example
}
}
`;
const output = jsonToBru(jsonInput);
expect(output).toEqual(expected);
});
});
describe('Complex examples with auth', () => {
it('should parse complex-with-auth.bru correctly', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'complex-with-auth.bru'), 'utf8');
const expected = require('./fixtures/json/complex-with-auth.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
it('should parse form-data-complex.bru correctly', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'form-data-complex.bru'), 'utf8');
const expected = require('./fixtures/json/form-data-complex.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
it('should parse multiple-examples-variations.bru correctly', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'multiple-examples-variations.bru'), 'utf8');
const expected = require('./fixtures/json/multiple-examples-variations.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
it('should parse oauth2-examples.bru correctly', () => {
const input = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'oauth2-examples.bru'), 'utf8');
const expected = require('./fixtures/json/oauth2-examples.json');
const output = bruToJson(input);
expect(output).toEqual(expected);
});
describe('jsonToBru conversion for complex fixtures', () => {
it('should convert complex-with-auth.json to BRU format and preserve examples', () => {
const jsonInput = require('./fixtures/json/complex-with-auth.json');
const expected = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'complex-with-auth.bru'), 'utf8');
const output = jsonToBru(jsonInput);
expect(output).toEqual(expected);
});
it('should convert form-data-complex.json to BRU format and preserve examples', () => {
const jsonInput = require('./fixtures/json/form-data-complex.json');
const expected = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'form-data-complex.bru'), 'utf8');
const output = jsonToBru(jsonInput);
expect(output).toEqual(expected);
});
it('should convert multiple-examples-variations.json to BRU format and preserve examples', () => {
const jsonInput = require('./fixtures/json/multiple-examples-variations.json');
const expected = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'multiple-examples-variations.bru'), 'utf8');
const output = jsonToBru(jsonInput);
expect(output).toEqual(expected);
});
it('should convert oauth2-examples.json to BRU format and preserve examples', () => {
const jsonInput = require('./fixtures/json/oauth2-examples.json');
const expected = fs.readFileSync(path.join(__dirname, 'fixtures', 'bru', 'oauth2-examples.bru'), 'utf8');
const output = jsonToBru(jsonInput);
expect(output).toEqual(expected);
});
});
});
});