Test API endpoints that return CSV
The CSV export is the endpoint finance, support and customers actually use. It's also the one with the fewest tests, because "it returns a file" feels done. Then a customer name with a comma shifts every column after it, or Excel shows ü for ü.
The headers
pm.test('CSV content type with charset', () => {
const type = (pm.response.headers.get('Content-Type') || '').toLowerCase();
pm.expect(type).to.include('text/csv');
pm.expect(type).to.include('charset=utf-8');
});
pm.test('downloads with a file name', () => {
const cd = pm.response.headers.get('Content-Disposition') || '';
pm.expect(cd).to.include('attachment');
pm.expect(cd).to.match(/filename\*?=/);
});If Excel users open the file directly, many teams also add a UTF-8 byte order mark at the start, which makes Excel read it as UTF-8. Whether you do or not, test it, so it doesn't change:
pm.test('starts with a UTF-8 BOM for Excel', () => {
pm.expect(pm.response.text().charCodeAt(0)).to.equal(0xfeff);
});Parse it properly
Splitting on commas breaks on the first quoted value. A small parser that handles quotes, doubled quotes and line breaks inside quotes fits in a post-response script:
function parseCsv(text) {
const rows = [];
let row = [], field = '', quoted = false;
text = text.replace(/^/, '');
for (let i = 0; i < text.length; i++) {
const c = text[i];
if (quoted) {
if (c === '"' && text[i + 1] === '"') { field += '"'; i++; }
else if (c === '"') quoted = false;
else field += c;
} else if (c === '"') quoted = true;
else if (c === ',') { row.push(field); field = ''; }
else if (c === '\n' || c === '\r') {
if (c === '\r' && text[i + 1] === '\n') i++;
row.push(field); rows.push(row); row = []; field = '';
} else field += c;
}
if (field || row.length) { row.push(field); rows.push(row); }
return rows;
}
const rows = parseCsv(pm.response.text());
const [header, ...data] = rows;Put the function in the folder's post-response script if several export requests need it; folder scripts run before the request's own script for the same phase.
The header row
Column names are an API contract. Scripts and spreadsheets that read the file break when a column is renamed or moved:
pm.test('columns are exactly these, in this order', () => {
pm.expect(header).to.eql(['orderId', 'createdAt', 'customer', 'total', 'currency']);
});
pm.test('every row has the same number of columns', () => {
data.forEach((r, i) => pm.expect(r.length, `row ${i + 2}`).to.equal(header.length));
});The second check is the one that catches broken quoting.
Quoting
Create test data that needs it, in a setup request before the export:
{"customer": "Smith, \"Bob\" & Co\nLtd", "total": "10.00"}Then find that row and check the field came through whole:
const customerCol = header.indexOf('customer');
pm.test('commas, quotes and line breaks survive', () => {
const found = data.some((r) => r[customerCol] === 'Smith, "Bob" & Co\nLtd');
pm.expect(found).to.equal(true);
});Formula injection
A cell that starts with =, +, - or @ is run as a formula when the file is opened in a spreadsheet. A customer named =HYPERLINK("http://evil.example","click") becomes a link in your finance team's Excel. Exports of user-entered data should escape these, usually by prefixing a ':
pm.test('cells are not formulas', () => {
data.forEach((r) => r.forEach((cell) => pm.expect(cell).to.not.match(/^[=+\-@]/)));
});Negative numbers start with - too, so limit the check to text columns if the export has them.
Row counts
The CSV and the JSON list endpoint should agree for the same filter. Call the JSON one first and store its total:
// "List orders (JSON)"
pm.environment.set('orderTotal', String(pm.response.json().total));// "Export orders (CSV)"
pm.test('CSV has the same rows as the API', () => {
pm.expect(data.filter((r) => r.join('') !== '')).to.have.lengthOf(Number(pm.environment.get('orderTotal')));
});