// This is a simple test script to test the blog API routes // Run with: node scripts/test-blog-api.js const fetch = require('node-fetch'); const BASE_URL = 'http://localhost:3000/api/blog'; async function testCreateBlogPost() { console.log('Testing create blog post...'); const response = await fetch(BASE_URL, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ title: 'Test Blog Post', content: '# Test Blog Post\n\nThis is a test blog post created via the API.', excerpt: 'This is a test blog post.', author: 'API Test', tags: ['test', 'api'], }), }); const data = await response.json(); console.log('Response:', data); return data.slug; } async function testGetAllBlogPosts() { console.log('\nTesting get all blog posts...'); const response = await fetch(BASE_URL); const data = await response.json(); console.log('Response:', data); } async function testGetBlogPost(slug) { console.log(`\nTesting get blog post with slug: ${slug}...`); const response = await fetch(`${BASE_URL}/${slug}`); const data = await response.json(); console.log('Response:', data); } async function testUpdateBlogPost(slug) { console.log(`\nTesting update blog post with slug: ${slug}...`); const response = await fetch(`${BASE_URL}/${slug}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ title: 'Updated Test Blog Post', content: '# Updated Test Blog Post\n\nThis blog post has been updated via the API.', excerpt: 'This is an updated test blog post.', author: 'API Test', tags: ['test', 'api', 'updated'], }), }); const data = await response.json(); console.log('Response:', data); } async function testDeleteBlogPost(slug) { console.log(`\nTesting delete blog post with slug: ${slug}...`); const response = await fetch(`${BASE_URL}/${slug}`, { method: 'DELETE', }); const data = await response.json(); console.log('Response:', data); } async function runTests() { try { // Create a blog post const slug = await testCreateBlogPost(); // Get all blog posts await testGetAllBlogPosts(); // Get the created blog post await testGetBlogPost(slug); // Update the blog post await testUpdateBlogPost(slug); // Get the updated blog post await testGetBlogPost(slug); // Delete the blog post await testDeleteBlogPost(slug); // Verify the blog post was deleted await testGetAllBlogPosts(); console.log('\nAll tests completed successfully!'); } catch (error) { console.error('Error running tests:', error); } } runTests();