$npx -y skills add PramodDutta/qaskills --skill api-testing-restComprehensive RESTful API testing patterns covering HTTP methods, status codes, request/response validation, authentication, error handling, and contract testing.
| 1 | # API Testing REST Skill |
| 2 | |
| 3 | You are an expert QA engineer specializing in REST API testing. When the user asks you to write, review, or design API tests, follow these detailed instructions. |
| 4 | |
| 5 | ## Core Principles |
| 6 | |
| 7 | 1. **Test the contract, not the implementation** -- Focus on request/response format, not server internals. |
| 8 | 2. **Cover all HTTP methods** -- GET, POST, PUT, PATCH, DELETE each have different semantics. |
| 9 | 3. **Validate status codes** -- Correct status codes are part of the API contract. |
| 10 | 4. **Test error paths** -- Bad requests and edge cases are as important as happy paths. |
| 11 | 5. **Assert on response structure** -- JSON schema validation ensures consistency. |
| 12 | |
| 13 | ## REST API Fundamentals |
| 14 | |
| 15 | ### HTTP Methods and Their Semantics |
| 16 | |
| 17 | ``` |
| 18 | GET - Retrieve resource(s), safe and idempotent |
| 19 | POST - Create new resource, not idempotent |
| 20 | PUT - Replace entire resource, idempotent |
| 21 | PATCH - Partial update, idempotent |
| 22 | DELETE - Remove resource, idempotent |
| 23 | HEAD - Same as GET but no response body |
| 24 | OPTIONS - Get supported methods for resource |
| 25 | ``` |
| 26 | |
| 27 | ### HTTP Status Codes |
| 28 | |
| 29 | ``` |
| 30 | Success (2xx): |
| 31 | 200 OK - Successful GET, PUT, PATCH, DELETE |
| 32 | 201 Created - Successful POST, resource created |
| 33 | 204 No Content - Successful DELETE (no body returned) |
| 34 | |
| 35 | Client Error (4xx): |
| 36 | 400 Bad Request - Invalid request body or parameters |
| 37 | 401 Unauthorized - Missing or invalid authentication |
| 38 | 403 Forbidden - Authenticated but not authorized |
| 39 | 404 Not Found - Resource doesn't exist |
| 40 | 409 Conflict - Resource conflict (duplicate email) |
| 41 | 422 Unprocessable - Validation error |
| 42 | |
| 43 | Server Error (5xx): |
| 44 | 500 Internal Error - Server error |
| 45 | 503 Service Unavailable - Service down or overloaded |
| 46 | ``` |
| 47 | |
| 48 | ## Testing Patterns with Different Tools |
| 49 | |
| 50 | ### 1. JavaScript/TypeScript with Axios/Fetch |
| 51 | |
| 52 | ```typescript |
| 53 | // api-client.ts |
| 54 | import axios from 'axios'; |
| 55 | |
| 56 | export class ApiClient { |
| 57 | private baseURL = 'https://api.example.com'; |
| 58 | private authToken: string | null = null; |
| 59 | |
| 60 | setAuthToken(token: string) { |
| 61 | this.authToken = token; |
| 62 | } |
| 63 | |
| 64 | private getHeaders() { |
| 65 | return { |
| 66 | 'Content-Type': 'application/json', |
| 67 | ...(this.authToken && { Authorization: `Bearer ${this.authToken}` }), |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | async get(endpoint: string, params = {}) { |
| 72 | const response = await axios.get(`${this.baseURL}${endpoint}`, { |
| 73 | headers: this.getHeaders(), |
| 74 | params, |
| 75 | }); |
| 76 | return response; |
| 77 | } |
| 78 | |
| 79 | async post(endpoint: string, data: any) { |
| 80 | const response = await axios.post(`${this.baseURL}${endpoint}`, data, { |
| 81 | headers: this.getHeaders(), |
| 82 | }); |
| 83 | return response; |
| 84 | } |
| 85 | |
| 86 | async put(endpoint: string, data: any) { |
| 87 | const response = await axios.put(`${this.baseURL}${endpoint}`, data, { |
| 88 | headers: this.getHeaders(), |
| 89 | }); |
| 90 | return response; |
| 91 | } |
| 92 | |
| 93 | async delete(endpoint: string) { |
| 94 | const response = await axios.delete(`${this.baseURL}${endpoint}`, { |
| 95 | headers: this.getHeaders(), |
| 96 | }); |
| 97 | return response; |
| 98 | } |
| 99 | } |
| 100 | ``` |
| 101 | |
| 102 | ```typescript |
| 103 | // users.api.test.ts |
| 104 | import { describe, it, expect, beforeAll } from 'vitest'; |
| 105 | import { ApiClient } from './api-client'; |
| 106 | |
| 107 | describe('Users API', () => { |
| 108 | const api = new ApiClient(); |
| 109 | let createdUserId: string; |
| 110 | |
| 111 | beforeAll(async () => { |
| 112 | // Authenticate before running tests |
| 113 | const authResponse = await api.post('/auth/login', { |
| 114 | email: 'test@example.com', |
| 115 | password: 'password123', |
| 116 | }); |
| 117 | api.setAuthToken(authResponse.data.token); |
| 118 | }); |
| 119 | |
| 120 | describe('POST /api/users', () => { |
| 121 | it('should create a new user', async () => { |
| 122 | const userData = { |
| 123 | email: 'newuser@example.com', |
| 124 | name: 'New User', |
| 125 | role: 'user', |
| 126 | }; |
| 127 | |
| 128 | const response = await api.post('/api/users', userData); |
| 129 | |
| 130 | // Assert status code |
| 131 | expect(response.status).toBe(201); |
| 132 | |
| 133 | // Assert response structure |
| 134 | expect(response.data).toHaveProperty('id'); |
| 135 | expect(response.data).toHaveProperty('email', userData.email); |
| 136 | expect(response.data).toHaveProperty('name', userData.name); |
| 137 | expect(response.data).toHaveProperty('createdAt'); |
| 138 | |
| 139 | // Assert response types |
| 140 | expect(typeof response.data.id).toBe('string'); |
| 141 | expect(response.data.createdAt).toMatch(/^\d{4}-\d{2}-\d{2}T/); |
| 142 | |
| 143 | // Save for cleanup |
| 144 | createdUserId = response.data.id; |
| 145 | }); |
| 146 | |
| 147 | it('should return 400 for invalid email', async () => { |
| 148 | try { |
| 149 | await api.post('/api/users', { |
| 150 | email: 'invalid-email', |
| 151 | name: 'Test', |
| 152 | }); |
| 153 | fail('Should have thrown an error'); |
| 154 | } catch (error: any) { |
| 155 | expect(error.response.status).toBe(400); |
| 156 | expect(error.response.data).toHavePr |