AI Resource Pro / website · airesourcepro.com / scripts / tests / test-blog-api.js
| 1 | // This is a simple test script to test the blog API routes |
| 2 | // Run with: node scripts/test-blog-api.js |
| 3 | |
| 4 | const fetch = require('node-fetch'); |
| 5 | |
| 6 | const BASE_URL = 'http://localhost:3000/api/blog'; |
| 7 | |
| 8 | async function testCreateBlogPost() { |
| 9 | console.log('Testing create blog post...'); |
| 10 | |
| 11 | const response = await fetch(BASE_URL, { |
| 12 | method: 'POST', |
| 13 | headers: { |
| 14 | 'Content-Type': 'application/json', |
| 15 | }, |
| 16 | body: JSON.stringify({ |
| 17 | title: 'Test Blog Post', |
| 18 | content: '# Test Blog Post\n\nThis is a test blog post created via the API.', |
| 19 | excerpt: 'This is a test blog post.', |
| 20 | author: 'API Test', |
| 21 | tags: ['test', 'api'], |
| 22 | }), |
| 23 | }); |
| 24 | |
| 25 | const data = await response.json(); |
| 26 | console.log('Response:', data); |
| 27 | |
| 28 | return data.slug; |
| 29 | } |
| 30 | |
| 31 | async function testGetAllBlogPosts() { |
| 32 | console.log('\nTesting get all blog posts...'); |
| 33 | |
| 34 | const response = await fetch(BASE_URL); |
| 35 | const data = await response.json(); |
| 36 | |
| 37 | console.log('Response:', data); |
| 38 | } |
| 39 | |
| 40 | async function testGetBlogPost(slug) { |
| 41 | console.log(`\nTesting get blog post with slug: ${slug}...`); |
| 42 | |
| 43 | const response = await fetch(`${BASE_URL}/${slug}`); |
| 44 | const data = await response.json(); |
| 45 | |
| 46 | console.log('Response:', data); |
| 47 | } |
| 48 | |
| 49 | async function testUpdateBlogPost(slug) { |
| 50 | console.log(`\nTesting update blog post with slug: ${slug}...`); |
| 51 | |
| 52 | const response = await fetch(`${BASE_URL}/${slug}`, { |
| 53 | method: 'PUT', |
| 54 | headers: { |
| 55 | 'Content-Type': 'application/json', |
| 56 | }, |
| 57 | body: JSON.stringify({ |
| 58 | title: 'Updated Test Blog Post', |
| 59 | content: '# Updated Test Blog Post\n\nThis blog post has been updated via the API.', |
| 60 | excerpt: 'This is an updated test blog post.', |
| 61 | author: 'API Test', |
| 62 | tags: ['test', 'api', 'updated'], |
| 63 | }), |
| 64 | }); |
| 65 | |
| 66 | const data = await response.json(); |
| 67 | console.log('Response:', data); |
| 68 | } |
| 69 | |
| 70 | async function testDeleteBlogPost(slug) { |
| 71 | console.log(`\nTesting delete blog post with slug: ${slug}...`); |
| 72 | |
| 73 | const response = await fetch(`${BASE_URL}/${slug}`, { |
| 74 | method: 'DELETE', |
| 75 | }); |
| 76 | |
| 77 | const data = await response.json(); |
| 78 | console.log('Response:', data); |
| 79 | } |
| 80 | |
| 81 | async function runTests() { |
| 82 | try { |
| 83 | // Create a blog post |
| 84 | const slug = await testCreateBlogPost(); |
| 85 | |
| 86 | // Get all blog posts |
| 87 | await testGetAllBlogPosts(); |
| 88 | |
| 89 | // Get the created blog post |
| 90 | await testGetBlogPost(slug); |
| 91 | |
| 92 | // Update the blog post |
| 93 | await testUpdateBlogPost(slug); |
| 94 | |
| 95 | // Get the updated blog post |
| 96 | await testGetBlogPost(slug); |
| 97 | |
| 98 | // Delete the blog post |
| 99 | await testDeleteBlogPost(slug); |
| 100 | |
| 101 | // Verify the blog post was deleted |
| 102 | await testGetAllBlogPosts(); |
| 103 | |
| 104 | console.log('\nAll tests completed successfully!'); |
| 105 | } catch (error) { |
| 106 | console.error('Error running tests:', error); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | runTests(); |