|
| 1 | +import { describe, expect, it } from 'bun:test'; |
| 2 | +import { getSecurityHeaders } from './OpenAPICodeSample'; |
| 3 | +import type { OpenAPIOperationData } from './types'; |
| 4 | + |
| 5 | +describe('getSecurityHeaders', () => { |
| 6 | + it('should handle custom HTTP scheme with x-gitbook-prefix', () => { |
| 7 | + const securities: OpenAPIOperationData['securities'] = [ |
| 8 | + [ |
| 9 | + 'customScheme', |
| 10 | + { |
| 11 | + type: 'apiKey', |
| 12 | + in: 'header', |
| 13 | + name: 'Authorization', |
| 14 | + 'x-gitbook-prefix': 'CustomScheme', |
| 15 | + }, |
| 16 | + ], |
| 17 | + ]; |
| 18 | + |
| 19 | + const result = getSecurityHeaders({ |
| 20 | + securityRequirement: [{ customScheme: [] }], |
| 21 | + securities, |
| 22 | + }); |
| 23 | + |
| 24 | + expect(result).toEqual({ |
| 25 | + Authorization: 'CustomScheme YOUR_API_KEY', |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should use x-gitbook-prefix with x-gitbook-token-placeholder together', () => { |
| 30 | + const securities: OpenAPIOperationData['securities'] = [ |
| 31 | + [ |
| 32 | + 'customAuth', |
| 33 | + { |
| 34 | + type: 'apiKey', |
| 35 | + in: 'header', |
| 36 | + name: 'Authorization', |
| 37 | + 'x-gitbook-prefix': 'Token', |
| 38 | + 'x-gitbook-token-placeholder': 'MY_CUSTOM_TOKEN', |
| 39 | + }, |
| 40 | + ], |
| 41 | + ]; |
| 42 | + |
| 43 | + const result = getSecurityHeaders({ |
| 44 | + securityRequirement: [{ customAuth: [] }], |
| 45 | + securities, |
| 46 | + }); |
| 47 | + |
| 48 | + expect(result).toEqual({ |
| 49 | + Authorization: 'Token MY_CUSTOM_TOKEN', |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should not use x-gitbook-prefix for http scheme', () => { |
| 54 | + const securities: OpenAPIOperationData['securities'] = [ |
| 55 | + [ |
| 56 | + 'customAuth', |
| 57 | + { |
| 58 | + type: 'http', |
| 59 | + in: 'header', |
| 60 | + name: 'Authorization', |
| 61 | + scheme: 'bearer', |
| 62 | + 'x-gitbook-prefix': 'Token', |
| 63 | + }, |
| 64 | + ], |
| 65 | + ]; |
| 66 | + |
| 67 | + const result = getSecurityHeaders({ |
| 68 | + securityRequirement: [{ customAuth: [] }], |
| 69 | + securities, |
| 70 | + }); |
| 71 | + |
| 72 | + expect(result).toEqual({ |
| 73 | + Authorization: 'Bearer YOUR_SECRET_TOKEN', |
| 74 | + }); |
| 75 | + }); |
| 76 | +}); |
0 commit comments