1
0
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:
Saad Jutt
2022-02-18 07:22:11 +05:00
parent cd32912379
commit 2c4aa420b3
10 changed files with 154 additions and 25 deletions

View 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
}

View File

@@ -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)

View File

@@ -1,4 +1,5 @@
export * from './connectDB'
export * from './extractHeaders'
export * from './file'
export * from './generateAccessToken'
export * from './generateAuthCode'

View 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({})
})
})