Testing the Contentful Delivery, Preview and Management APIs
Contentful has three APIs that look alike and behave differently: Delivery for published content, Preview for drafts, and Management for writing. Mixing up their hosts and tokens is the most common bug. The second most common is a frontend that breaks when an editor leaves a field empty. A test collection covers both.
Step 1: Environments as Requestarium environments
Contentful environments map neatly onto Requestarium environments:
| Variable | Staging | Production |
|---|---|---|
cfSpace | your space id | your space id |
cfEnv | staging | master |
cfDeliveryToken | delivery token (secret) | delivery token (secret) |
cfPreviewToken | preview token (secret) | preview token (secret) |
cfManagementToken | CMA token (secret) | not set |
Leaving the management token out of the production environment means the write requests simply can't run there. See environments for local, staging and production.
Step 2: Published entries
GET https://cdn.contentful.com/spaces/{{cfSpace}}/environments/{{cfEnv}}/entries?content_type=blogPost&order=-fields.publishDate&limit=5&include=1
Authorization: Bearer {{cfDeliveryToken}}const body = pm.response.json();
pm.test('posts published', () => pm.expect(body.total).to.be.above(0));
pm.test('every post has what the frontend needs', () => {
body.items.forEach((p) => {
pm.expect(p.fields.title, p.sys.id).to.be.a('string');
pm.expect(p.fields.slug, p.sys.id).to.match(/^[a-z0-9-]+$/);
});
});The second test is the valuable one: it fails when an editor publishes a post without a slug, before the page 404s.
Step 3: Resolve linked entries
A post's author is a link: {"sys": {"type": "Link", "linkType": "Entry", "id": "…"}}. With include=1, the linked entries arrive in includes.Entry:
const authors = Object.fromEntries((body.includes?.Entry || []).map((e) => [e.sys.id, e]));
pm.test('every author link resolves', () => {
body.items.forEach((p) => {
const link = p.fields.author?.sys?.id;
pm.expect(authors[link], `author of ${p.fields.slug}`).to.exist;
});
});A link to an unpublished or deleted entry doesn't appear in includes, which is exactly the broken-author-box bug this test catches.
Step 4: Drafts through Preview
Same request, preview host and token:
GET https://preview.contentful.com/spaces/{{cfSpace}}/environments/{{cfEnv}}/entries?content_type=blogPost&limit=5
Authorization: Bearer {{cfPreviewToken}}Using the delivery token on the preview host answers 401. That's worth one negative test, because a preview build configured with the wrong token shows editors a blank page.
Step 5: Writing with version locking
GET https://api.contentful.com/spaces/{{cfSpace}}/environments/{{cfEnv}}/entries/{{entryId}}
Authorization: Bearer {{cfManagementToken}}pm.environment.set('entryVersion', String(pm.response.json().sys.version));PUT https://api.contentful.com/spaces/{{cfSpace}}/environments/{{cfEnv}}/entries/{{entryId}}
Authorization: Bearer {{cfManagementToken}}
Content-Type: application/vnd.contentful.management.v1+json
X-Contentful-Version: {{entryVersion}}
{"fields": {"title": {"en-US": "Updated by API tests"}, "slug": {"en-US": "api-test-entry"}}}Management fields are keyed by locale, and a PUT replaces all fields, so send every field. Send the same PUT again with the old version and Contentful answers 409 VersionMismatch. That's the lost-update protection working, and your integration should handle it by reloading.