Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-cronin committed Oct 14, 2020
1 parent f5b0d80 commit 571defd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/api/HttpApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,12 @@ class HttpApi {
const response = this.getSecret(vaultName, secretName)

const accepts = req.accepts()[0]
if (accepts == 'text/plain') {
if (!accepts || accepts == 'text/plain' || accepts == '*/*') {
this.writeString(res, response.toString())
} else if (accepts == 'application/octet-stream') {
this.writeBinary(res, secretName, response);
} else {
throw Error('MIME type not supported')
throw Error(`MIME type not supported: ${accepts}`)
}
} catch (error) {
this.writeError(res, error);
Expand All @@ -366,7 +366,7 @@ class HttpApi {
req.on('end', () => resolve(Buffer.concat(bufferList)))
})
} else {
throw Error('MIME type not supported')
throw Error(`MIME type not supported: ${contentType}`)
}

await this.newSecret(vaultName, secretName, secretContent);
Expand Down
21 changes: 10 additions & 11 deletions tests/lib/api/API.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('HTTP API', () => {
let accessToken: string

let vaultSet: Set<string>
let secretMap: Map<string, string>
let secretMap: Map<string, Buffer>

beforeEach(async () => {
tempDir = fs.mkdtempSync(`${os.tmpdir}/pktest${randomString()}`)
Expand All @@ -31,7 +31,7 @@ describe('HTTP API', () => {
// initialize data sets
vaultSet = new Set()
secretMap = new Map()
secretMap.set('secret1', 'secret 1 content')
secretMap.set('secret1', Buffer.from('secret 1 content'))

api = new HttpApi(
(apiAddress: Address) => { peerInfo.apiAddress = apiAddress; },
Expand All @@ -44,8 +44,8 @@ describe('HTTP API', () => {
async (vaultName: string) => { vaultSet.delete(vaultName); },
(vaultName: string) => Array.from(secretMap.entries()).map((e) => e[0]),
(vaultName: string, secretName: string) => secretMap.get(secretName)!,
async (vaultName: string, secretName: string, secretContent: string) => { secretMap.set(secretName, secretContent); return true },
async (vaultName: string, secretName: string) => { return secretMap.delete(secretName) }
async (vaultName: string, secretName: string, secretContent: Buffer) => { secretMap.set(secretName, secretContent) },
async (vaultName: string, secretName: string) => { secretMap.delete(secretName) }
)
await api.start()
accessToken = api.newOAuthToken(['admin'])
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('HTTP API', () => {
test('can get secret content', async () => {
const response = await makeRequest('GET', '/secrets/vault1/secret1')
const expectedSecretContent = secretMap.get('secret1')
expect(response).toEqual(expectedSecretContent)
expect(response).toEqual(expectedSecretContent?.toString())
})

test('can add new secret', async () => {
Expand Down Expand Up @@ -147,30 +147,29 @@ describe('HTTP API', () => {
return <string>publicKey
}

const makeRequest = async (method: string, path: string, body?: Object | string): Promise<any> => {
const makeRequest = async (method: string, path: string, body?: string): Promise<any> => {
return await new Promise((resolve, reject) => {
const headers = {}
headers['Authorization'] = `Bearer ${accessToken}`
if (body) {
headers['Content-Type'] = (typeof body === 'string') ? 'text/plain' : 'application/json'
headers['Content-Type'] = 'text/plain'
}
const options: http.RequestOptions = {
hostname: peerInfo?.apiAddress?.host,
port: peerInfo?.apiAddress?.port,
path,
method,
ca: [tlsCredentials.rootCertificate],
headers
headers,
}

const req = http.request(options, (res) => {
res.setEncoding('utf8');
res.on('data', (data) => {
try {
resolve(JSON.parse(data))
} catch (error) {
reject(data)
}
} catch (error) {}
resolve(data)
});
res.on('error', (err) => {
reject(err)
Expand Down

0 comments on commit 571defd

Please sign in to comment.