Files
bruno/packages/bruno-converters/tests/openapi/openapi-with-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

305 lines
10 KiB
JavaScript

import { describe, it, expect } from '@jest/globals';
import openApiToBruno from '../../src/openapi/openapi-to-bruno';
import * as fs from 'fs';
import * as path from 'path';
describe('OpenAPI with Examples', () => {
const openApiWithExamples = fs.readFileSync(path.resolve(__dirname, '../../../../tests/import/openapi/fixtures/openapi-with-examples.yaml'),
'utf8');
it('should import OpenAPI collection with response examples', () => {
const brunoCollection = openApiToBruno(openApiWithExamples);
expect(brunoCollection).toBeDefined();
expect(brunoCollection.name).toBe('API with Examples');
expect(brunoCollection.items).toHaveLength(3); // Three separate requests
// Test GET /users endpoint
const getUsersRequest = brunoCollection.items.find((item) => item.name === 'Get all users');
expect(getUsersRequest).toBeDefined();
expect(getUsersRequest.examples).toBeDefined();
expect(getUsersRequest.examples).toHaveLength(4);
// Check specific examples
const successExample = getUsersRequest.examples.find((ex) => ex.name === 'Success Response');
expect(successExample).toBeDefined();
expect(successExample.response.status).toBe('200');
expect(successExample.response.statusText).toBe('OK');
expect(successExample.response.headers).toHaveLength(1);
expect(successExample.response.headers[0].name).toBe('Content-Type');
expect(successExample.response.headers[0].value).toBe('application/json');
expect(JSON.parse(successExample.response.body.content)).toEqual({
users: [
{ id: 1, name: 'John Doe', email: 'john@example.com' },
{ id: 2, name: 'Jane Smith', email: 'jane@example.com' }
]
});
const emptyExample = getUsersRequest.examples.find((ex) => ex.name === 'Empty Response');
expect(emptyExample.response.status).toBe('200');
expect(JSON.parse(emptyExample.response.body.content)).toEqual({ users: [] });
const validationErrorExample = getUsersRequest.examples.find((ex) => ex.name === 'Validation Error');
expect(validationErrorExample).toBeDefined();
expect(validationErrorExample.response.status).toBe('400');
expect(validationErrorExample.response.statusText).toBe('Bad Request');
const serverErrorExample = getUsersRequest.examples.find((ex) => ex.name === 'Server Error');
expect(serverErrorExample).toBeDefined();
expect(serverErrorExample.response.status).toBe('500');
expect(serverErrorExample.response.statusText).toBe('Internal Server Error');
// Test POST /users endpoint
const createUserRequest = brunoCollection.items.find((item) => item.name === 'Create a new user');
expect(createUserRequest).toBeDefined();
expect(createUserRequest.examples).toBeDefined();
expect(createUserRequest.examples).toHaveLength(2);
// Check response examples
const createdExample = createUserRequest.examples.find((ex) => ex.name === 'User Created');
expect(createdExample).toBeDefined();
expect(createdExample.response.status).toBe('201');
expect(createdExample.response.statusText).toBe('Created');
expect(JSON.parse(createdExample.response.body.content)).toEqual({
id: 123,
name: 'John Doe',
email: 'john@example.com',
created_at: '2023-01-01T00:00:00Z'
});
});
it('should handle OpenAPI examples with different content types', () => {
const openApiWithDifferentContentTypes = `
openapi: '3.0.0'
info:
version: '1.0.0'
title: 'API with Different Content Types'
paths:
/data:
get:
summary: 'Get data'
operationId: 'getData'
responses:
'200':
description: 'Successful response'
content:
application/json:
examples:
json_response:
summary: 'JSON Response'
value:
message: 'Hello World'
text/plain:
examples:
text_response:
summary: 'Text Response'
value: 'Hello World'
servers:
- url: 'https://api.example.com'
`;
const brunoCollection = openApiToBruno(openApiWithDifferentContentTypes);
const request = brunoCollection.items[0];
expect(request.examples).toHaveLength(2);
const jsonExample = request.examples.find((ex) => ex.name === 'JSON Response');
expect(jsonExample).toBeDefined();
expect(jsonExample.response.headers[0].value).toBe('application/json');
const textExample = request.examples.find((ex) => ex.name === 'Text Response');
expect(textExample).toBeDefined();
expect(textExample.response.headers[0].value).toBe('text/plain');
expect(textExample.response.body.content).toBe('Hello World');
expect(textExample.response.body.type).toBe('text');
});
it('should handle OpenAPI examples without summary or description', () => {
const openApiWithMinimalExamples = `
openapi: '3.0.0'
info:
version: '1.0.0'
title: 'API with Minimal Examples'
paths:
/test:
get:
summary: 'Test endpoint'
operationId: 'test'
responses:
'200':
description: 'OK'
content:
application/json:
examples:
example1:
value:
message: 'test'
servers:
- url: 'https://api.example.com'
`;
const brunoCollection = openApiToBruno(openApiWithMinimalExamples);
const request = brunoCollection.items[0];
expect(request.examples).toHaveLength(1);
const example = request.examples[0];
expect(example.name).toBe('example1');
expect(example.description).toBe('');
expect(example.response.body.type).toBe('json');
expect(JSON.parse(example.response.body.content)).toEqual({ message: 'test' });
});
it('should not create examples array if no examples are present', () => {
const openApiWithoutExamples = `
openapi: '3.0.0'
info:
version: '1.0.0'
title: 'API without Examples'
paths:
/test:
get:
summary: 'Test endpoint'
operationId: 'test'
responses:
'200':
description: 'OK'
content:
application/json:
schema:
type: object
servers:
- url: 'https://api.example.com'
`;
const brunoCollection = openApiToBruno(openApiWithoutExamples);
const request = brunoCollection.items[0];
expect(request.examples).toBeUndefined();
});
it('should support path-based grouping when specified', () => {
const openApiWithPathGrouping = `
openapi: '3.0.0'
info:
version: '1.0.0'
title: 'API with Path Grouping'
paths:
/users:
get:
summary: 'Get all users'
operationId: 'getUsers'
responses:
'200':
description: 'OK'
content:
application/json:
examples:
success:
summary: 'Success Response'
value:
users: []
post:
summary: 'Create user'
operationId: 'createUser'
responses:
'201':
description: 'Created'
content:
application/json:
examples:
created:
summary: 'User Created'
value:
id: 123
/products:
get:
summary: 'Get all products'
operationId: 'getProducts'
responses:
'200':
description: 'OK'
content:
application/json:
examples:
success:
summary: 'Products Response'
value:
products: []
servers:
- url: 'https://api.example.com'
`;
// Test with path-based grouping
const brunoCollection = openApiToBruno(openApiWithPathGrouping, { groupBy: 'path' });
expect(brunoCollection).toBeDefined();
expect(brunoCollection.name).toBe('API with Path Grouping');
// Should have 2 folders: users and products (without leading slash)
expect(brunoCollection.items).toHaveLength(2);
const usersFolder = brunoCollection.items.find((item) => item.name === 'users');
expect(usersFolder).toBeDefined();
expect(usersFolder.type).toBe('folder');
expect(usersFolder.items).toHaveLength(2); // GET and POST /users
const productsFolder = brunoCollection.items.find((item) => item.name === 'products');
expect(productsFolder).toBeDefined();
expect(productsFolder.type).toBe('folder');
expect(productsFolder.items).toHaveLength(1); // GET /products
// Verify examples are preserved in path-based grouping
const getUsersRequest = usersFolder.items.find((item) => item.name === 'Get all users');
expect(getUsersRequest.examples).toBeDefined();
expect(getUsersRequest.examples).toHaveLength(1);
expect(getUsersRequest.examples[0].name).toBe('Success Response');
});
it('should default to tag-based grouping when no groupBy option is specified', () => {
const openApiWithTags = `
openapi: '3.0.0'
info:
version: '1.0.0'
title: 'API with Tags'
paths:
/users:
get:
summary: 'Get all users'
operationId: 'getUsers'
tags: ['Users']
responses:
'200':
description: 'OK'
/products:
get:
summary: 'Get all products'
operationId: 'getProducts'
tags: ['Products']
responses:
'200':
description: 'OK'
servers:
- url: 'https://api.example.com'
`;
// Test with default grouping (tags)
const brunoCollection = openApiToBruno(openApiWithTags);
expect(brunoCollection).toBeDefined();
expect(brunoCollection.name).toBe('API with Tags');
// Should have 2 folders based on tags: Users and Products
expect(brunoCollection.items).toHaveLength(2);
const usersFolder = brunoCollection.items.find((item) => item.name === 'Users');
expect(usersFolder).toBeDefined();
expect(usersFolder.type).toBe('folder');
expect(usersFolder.items).toHaveLength(1); // GET /users
const productsFolder = brunoCollection.items.find((item) => item.name === 'Products');
expect(productsFolder).toBeDefined();
expect(productsFolder.type).toBe('folder');
expect(productsFolder.items).toHaveLength(1); // GET /products
});
});