How to test API search, filtering and sorting
List endpoints with ?status=, ?q= and ?sort= look simple and hide a surprising number of bugs. A misspelled filter is ignored and returns everything. Sorting by a field with duplicates gives a different order on each call, so pagination skips items. A search for O'Brien returns 500. The good news: these tests are generic, so one set of patterns covers every list endpoint you have.
Filters: check every item
GET {{baseUrl}}/orders?status=shipped&createdAfter=2026-11-01const items = pm.response.json().items;
pm.test('found something', () => pm.expect(items).to.not.be.empty);
pm.test('every order is shipped', () => {
const wrong = items.filter((order) => order.status !== 'shipped').map((order) => order.id);
pm.expect(wrong).to.be.empty; // a failure lists the ids that aren't shipped
});
pm.test('every order is recent enough', () => {
const after = new Date('2026-11-01T00:00:00Z');
pm.expect(items.every((order) => new Date(order.createdAt) >= after)).to.be.true;
});The "found something" test matters: a filter on an empty result passes every other check.
Bad filters should be loud
| Request | Good answer | Bad answer |
|---|---|---|
?stauts=shipped (typo) | 400 naming the unknown parameter | 200 with every order |
?status=shiped | 400, or 200 with an empty list | 200 with every order |
?createdAfter=yesterday | 400 with the expected format | 500 |
?limit=-1, ?limit=1000000 | 400, or clamped to a maximum | 500, or a million rows |
An ignored filter is the dangerous one. A script that deletes "all shipped orders" after filtering deletes all orders.
Sorting: test ties
GET {{baseUrl}}/orders?sort=-totalconst totals = pm.response.json().items.map((order) => order.total);
pm.test('sorted by total, descending', () => {
const sorted = [...totals].sort((a, b) => b - a);
pm.expect(totals).to.eql(sorted);
});Then sort by a field with many equal values (status, country) and call it twice. If the order of items with the same value changes between calls, the API needs a tiebreaker such as id. Without one, page 2 can repeat items from page 1 and skip others.
Also check: an unknown sort field returns 400, and sorting is the same for ?sort=name with mixed case and accents as your users expect (Émile near Emma, not after Zoe).
Search: the inputs that break things
q= | Why |
|---|---|
| (empty) | everything, nothing, or 400? Decide and test |
O'Brien | quote handling, SQL built by string concatenation |
50% and a_b | % and _ are wildcards in SQL LIKE |
<script> | echoed back in a "no results for…" message? |
Łódź, 日本, café | Unicode and accent folding |
| 2,000 characters | a limit with 400, not a timeout |
spaces | trimmed or not, consistently |
Every one of these must return 200 or 400, never 500:
pm.test('no server error', () => pm.expect(pm.response.code).to.be.below(500));Filters plus pagination
Fetch every page with a filter applied and check the totals add up:
const ids = JSON.parse(pm.environment.get('seenIds') || '[]');
const page = pm.response.json();
const duplicates = page.items.filter((order) => ids.includes(order.id));
pm.test('no order on two pages', () => pm.expect(duplicates).to.be.empty);
pm.environment.set('seenIds', JSON.stringify(ids.concat(page.items.map((order) => order.id))));At the end, the number of collected IDs should equal the total the API reported. See How to test API pagination for walking the pages.