Skip to content

Commit

Permalink
pass the content-type through and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
neptunian committed Oct 17, 2019
1 parent 58d22a8 commit 178c336
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ export async function handleGetInfo(req: PackageRequest, extra: Extra) {
return integrationInfo;
}

export const handleGetImage = async (req: ImageRequest) => getImage(req.params);
export const handleGetImage = async (req: ImageRequest, extra: Extra) => {
const response = await getImage(req.params);
const newResponse = extra.response(response.body);
// set the content type from the registry response
newResponse.header('Content-Type', response.headers.get('content-type') || '');
return newResponse;
};

export async function handleRequestInstall(req: InstallAssetRequest, extra: Extra) {
const { pkgkey, asset } = req.params;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { URL } from 'url';
import { Response } from 'node-fetch';
import {
AssetsGroupedByServiceByType,
AssetParts,
Expand All @@ -15,7 +16,7 @@ import {
} from '../../common/types';
import { cacheGet, cacheSet } from './cache';
import { ArchiveEntry, untarBuffer } from './extract';
import { fetchUrl, getResponseStream } from './requests';
import { fetchUrl, getResponseStream, getResponse } from './requests';
import { streamToBuffer } from './streams';
import { integrationsManagerConfigStore } from '../config';

Expand Down Expand Up @@ -45,10 +46,10 @@ export async function fetchInfo(key: string): Promise<RegistryPackage> {
return fetchUrl(`${registryUrl}/package/${key}`).then(JSON.parse);
}

export async function fetchImage(params: ImageRequestParams): Promise<NodeJS.ReadableStream> {
export async function fetchImage(params: ImageRequestParams): Promise<Response> {
const { registryUrl } = integrationsManagerConfigStore.getConfig();
const { pkgkey, imgPath } = params;
return getResponseStream(`${registryUrl}/package/${pkgkey}/img/${imgPath}`);
return getResponse(`${registryUrl}/package/${pkgkey}/img/${imgPath}`);
}

export async function fetchCategories(): Promise<CategorySummaryList> {
Expand Down
61 changes: 61 additions & 0 deletions x-pack/test/integrations_manager_api_integration/apis/image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import ServerMock from 'mock-http-server';
import { FtrProviderContext } from '../../api_integration/ftr_provider_context';

export default function({ getService }: FtrProviderContext) {
describe('images', () => {
const server = new ServerMock({ host: 'localhost', port: 6666 });
beforeEach(() => {
server.start(() => {});
});
afterEach(() => {
server.stop(() => {});
});
it('fetches a png screenshot image from the registry', async () => {
server.on({
method: 'GET',
path: '/package/auditd-2.0.4/img/screenshots/auditbeat-file-integrity-dashboard.png',
reply: {
headers: { 'content-type': 'image/png' },
},
});

const supertest = getService('supertest');
const fetchImage = async () => {
await supertest
.get(
'/api/integrations_manager/package/auditd-2.0.4/img/screenshots/auditbeat-file-integrity-dashboard.png'
)
.set('kbn-xsrf', 'xxx')
.expect('Content-Type', 'image/png')
.expect(200);
};
await fetchImage();
});

it('fetches an icon image from the registry', async () => {
server.on({
method: 'GET',
path: '/package/auditd-2.0.4/img/icon.svg',
reply: {
headers: { 'content-type': 'image/svg' },
},
});

const supertest = getService('supertest');
const fetchImage = async () => {
await supertest
.get('/api/integrations_manager/package/auditd-2.0.4/img/icon.svg')
.set('kbn-xsrf', 'xxx')
.expect('Content-Type', 'image/svg')
.expect(200);
};
await fetchImage();
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export default function ({ loadTestFile }) {
describe('Integrations Manager Endpoints', function () {
this.tags('ciGroup7');
loadTestFile(require.resolve('./list'));
loadTestFile(require.resolve('./image'));
});
}

0 comments on commit 178c336

Please sign in to comment.