Files
verdaccio/packages/middleware/test/media.spec.ts
Marc Bernard ab3bf4a5d4 chore: replace mime package with constants (#5471)
* chore: replace mime package with constants

* Revert types/mime

* Revert types/mime

* Update lock file
2025-11-14 07:59:14 +01:00

56 lines
1.4 KiB
TypeScript

import request from 'supertest';
import { test } from 'vitest';
import { HEADERS, HTTP_STATUS } from '@verdaccio/core';
import { media } from '../src';
import { getApp } from './helper';
test('media is json', async () => {
const app = getApp([]);
app.get('/json', media(HEADERS.JSON), (req, res) => {
res.status(200).json();
});
return request(app)
.get('/json')
.set(HEADERS.CONTENT_TYPE, 'application/json')
.expect('Content-Type', /json/)
.expect(200);
});
test('media is json with charset', async () => {
const app = getApp([]);
app.get('/json', media(HEADERS.JSON), (req, res) => {
res.status(200).json();
});
return request(app)
.get('/json')
.set(HEADERS.CONTENT_TYPE, 'application/json; charset=utf-8')
.expect('Content-Type', /json/)
.expect(200);
});
test('media is not json', async () => {
const app = getApp([]);
app.get('/json', media(HEADERS.JSON), (req, res) => {
res.status(HTTP_STATUS.OK).json({});
});
return request(app)
.get('/json')
.set(HEADERS.CONTENT_TYPE, 'text/html; charset=utf-8')
.expect('Content-Type', /html/)
.expect(HTTP_STATUS.UNSUPPORTED_MEDIA);
});
test('missing content-type', async () => {
const app = getApp([]);
app.get('/json', media(HEADERS.JSON), (req, res) => {
res.status(HTTP_STATUS.OK).json({});
});
return request(app).get('/json').expect(HTTP_STATUS.UNSUPPORTED_MEDIA);
});