mirror of
https://github.com/sasjs/server.git
synced 2026-04-10 07:33:13 +00:00
feat: set response headers provded by SAS Code execution
This commit is contained in:
25
api/src/utils/extractHeaders.ts
Normal file
25
api/src/utils/extractHeaders.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
const headerUtils = require('http-headers-validation')
|
||||
|
||||
export interface HTTPHeaders {
|
||||
[key: string]: string | undefined
|
||||
}
|
||||
|
||||
export const extractHeaders = (content: string): HTTPHeaders => {
|
||||
const headersObj: HTTPHeaders = {}
|
||||
const headersArr = content
|
||||
.split('\n')
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => !!line)
|
||||
|
||||
headersArr.forEach((headerStr) => {
|
||||
const [key, value] = headerStr.split(':').map((data) => data.trim())
|
||||
|
||||
if (value && headerUtils.validateHeader(key, value)) {
|
||||
headersObj[key] = value
|
||||
} else {
|
||||
delete headersObj[key]
|
||||
}
|
||||
})
|
||||
|
||||
return headersObj
|
||||
}
|
||||
@@ -7,6 +7,9 @@ export const getCertificates = async () => {
|
||||
const keyPath = PRIVATE_KEY ?? (await getFileInput('Private Key (PEM)'))
|
||||
const certPath = FULL_CHAIN ?? (await getFileInput('Full Chain (PEM)'))
|
||||
|
||||
console.log('keyPath: ', keyPath)
|
||||
console.log('certPath: ', certPath)
|
||||
|
||||
const key = await readFile(keyPath)
|
||||
const cert = await readFile(certPath)
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from './connectDB'
|
||||
export * from './extractHeaders'
|
||||
export * from './file'
|
||||
export * from './generateAccessToken'
|
||||
export * from './generateAuthCode'
|
||||
|
||||
40
api/src/utils/specs/extractHeaders.spec.ts
Normal file
40
api/src/utils/specs/extractHeaders.spec.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { extractHeaders } from '..'
|
||||
|
||||
describe('extractHeaders', () => {
|
||||
it('should return valid http headers', () => {
|
||||
const headers = extractHeaders(`
|
||||
Content-type: application/csv
|
||||
Cache-Control: public, max-age=2000
|
||||
Content-type: application/text
|
||||
Cache-Control: public, max-age=1500
|
||||
Content-type: application/zip
|
||||
Cache-Control: public, max-age=1000
|
||||
`)
|
||||
|
||||
expect(headers).toEqual({
|
||||
'Content-type': 'application/zip',
|
||||
'Cache-Control': 'public, max-age=1000'
|
||||
})
|
||||
})
|
||||
|
||||
it('should not return http headers if last occurrence is blank', () => {
|
||||
const headers = extractHeaders(`
|
||||
Content-type: application/csv
|
||||
Cache-Control: public, max-age=1000
|
||||
Content-type: application/text
|
||||
Content-type:
|
||||
`)
|
||||
|
||||
expect(headers).toEqual({ 'Cache-Control': 'public, max-age=1000' })
|
||||
})
|
||||
|
||||
it('should return only valid http headers', () => {
|
||||
const headers = extractHeaders(`
|
||||
Content-type[]: application/csv
|
||||
Content//-type: application/text
|
||||
Content()-type: application/zip
|
||||
`)
|
||||
|
||||
expect(headers).toEqual({})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user