rename api.test and add tag test

This commit is contained in:
2025-07-17 10:42:40 +02:00
parent 3774b40987
commit f40a834c8c
2 changed files with 72 additions and 0 deletions
+94
View File
@@ -0,0 +1,94 @@
const request = require('supertest');
const express = require('express');
const helmet = require('helmet');
const badgeRouter = require('../src/api/badge');
const app = express();
app.use(helmet({ crossOriginResourcePolicy: false }));
app.use(badgeRouter);
describe('GET /badge', () => {
it('should return SVG with default params', async () => {
const res = await request(app).get('/badge');
const resBody = Buffer.from(res.body).toString();
expect(res.statusCode).toBe(200);
expect(res.headers['content-type']).toMatch(/image\/svg\+xml/);
expect(resBody).toContain('<svg');
expect(resBody).toContain('<rect');
});
it('should use custom icon and label', async () => {
const res = await request(app).get('/badge?icon=github&label=GitHub');
const resBody = Buffer.from(res.body).toString();
expect(res.statusCode).toBe(200);
expect(resBody).toContain('GitHub');
expect(resBody).toContain('<svg');
});
it('should use label from SVG title if label param is missing', async () => {
const res = await request(app).get('/badge?icon=github');
const resBody = Buffer.from(res.body).toString();
expect(res.statusCode).toBe(200);
expect(resBody).toContain('<svg');
// The title from the SVG should be present as label
expect(resBody.toLowerCase()).toContain('github');
});
it('should set icon background to transparent', async () => {
const res = await request(app).get('/badge?bgicon=none');
const resBody = Buffer.from(res.body).toString();
expect(res.statusCode).toBe(200);
expect(resBody).toContain('fill="none"');
});
it('should set label background to transparent', async () => {
const res = await request(app).get('/badge?bglabel=none');
const resBody = Buffer.from(res.body).toString();
expect(res.statusCode).toBe(200);
expect(resBody).toContain('fill="none"');
});
it('should use flat style with rounded corners', async () => {
const res = await request(app).get('/badge?style=flat');
const resBody = Buffer.from(res.body).toString();
expect(res.statusCode).toBe(200);
expect(resBody).toContain('rx="');
});
it('should handle labelpos left', async () => {
const res = await request(app).get('/badge?labelpos=left');
const resBody = Buffer.from(res.body).toString();
expect(res.statusCode).toBe(200);
expect(resBody).toContain('<rect');
expect(resBody).toContain('<text');
});
it('should handle labelpos above', async () => {
const res = await request(app).get('/badge?labelpos=above');
const resBody = Buffer.from(res.body).toString();
expect(res.statusCode).toBe(200);
expect(resBody).toContain('<rect');
expect(resBody).toContain('<text');
});
it('should handle labelpos below', async () => {
const res = await request(app).get('/badge?labelpos=below');
const resBody = Buffer.from(res.body).toString();
expect(res.statusCode).toBe(200);
expect(resBody).toContain('<rect');
expect(resBody).toContain('<text');
});
it('should handle custom fontweight', async () => {
const res = await request(app).get('/badge?fontweight=bold');
const resBody = Buffer.from(res.body).toString();
expect(res.statusCode).toBe(200);
expect(resBody).toContain('font-weight="bold"');
});
// Test not applicable, since icon is not real should handle 404 from CDN
it('should return 404 for unknown icon', async () => {
const res = await request(app).get('/badge?icon=notarealicon');
expect(res.statusCode).toBe(404);
});
});