|
| 1 | +import { QA } from '@objectstack/spec'; |
| 2 | +import { TestExecutionAdapter } from './adapter.js'; |
| 3 | + |
| 4 | +export class HttpTestAdapter implements TestExecutionAdapter { |
| 5 | + constructor(private baseUrl: string, private authToken?: string) {} |
| 6 | + |
| 7 | + async execute(action: QA.TestAction, context: Record<string, unknown>): Promise<unknown> { |
| 8 | + const headers: Record<string, string> = { |
| 9 | + 'Content-Type': 'application/json', |
| 10 | + }; |
| 11 | + if (this.authToken) { |
| 12 | + headers['Authorization'] = `Bearer ${this.authToken}`; |
| 13 | + } |
| 14 | + // If action.user is specified, maybe add a specific header for impersonation if supported? |
| 15 | + if (action.user) { |
| 16 | + headers['X-Run-As'] = action.user; |
| 17 | + } |
| 18 | + |
| 19 | + switch (action.type) { |
| 20 | + case 'create_record': |
| 21 | + return this.createRecord(action.target, action.payload || {}, headers); |
| 22 | + case 'update_record': |
| 23 | + return this.updateRecord(action.target, action.payload || {}, headers); |
| 24 | + case 'delete_record': |
| 25 | + return this.deleteRecord(action.target, action.payload || {}, headers); |
| 26 | + case 'read_record': |
| 27 | + return this.readRecord(action.target, action.payload || {}, headers); |
| 28 | + case 'query_records': |
| 29 | + return this.queryRecords(action.target, action.payload || {}, headers); |
| 30 | + case 'api_call': |
| 31 | + return this.rawApiCall(action.target, action.payload || {}, headers); |
| 32 | + case 'wait': |
| 33 | + const ms = Number(action.payload?.duration || 1000); |
| 34 | + return new Promise(resolve => setTimeout(() => resolve({ waited: ms }), ms)); |
| 35 | + default: |
| 36 | + throw new Error(`Unsupported action type in HttpAdapter: ${action.type}`); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private async createRecord(objectName: string, data: Record<string, unknown>, headers: Record<string, string>) { |
| 41 | + const response = await fetch(`${this.baseUrl}/api/data/${objectName}`, { |
| 42 | + method: 'POST', |
| 43 | + headers, |
| 44 | + body: JSON.stringify(data) |
| 45 | + }); |
| 46 | + return this.handleResponse(response); |
| 47 | + } |
| 48 | + |
| 49 | + private async updateRecord(objectName: string, data: Record<string, unknown>, headers: Record<string, string>) { |
| 50 | + const id = data._id || data.id; |
| 51 | + if (!id) throw new Error('Update record requires _id or id in payload'); |
| 52 | + const response = await fetch(`${this.baseUrl}/api/data/${objectName}/${id}`, { |
| 53 | + method: 'PUT', |
| 54 | + headers, |
| 55 | + body: JSON.stringify(data) |
| 56 | + }); |
| 57 | + return this.handleResponse(response); |
| 58 | + } |
| 59 | + |
| 60 | + private async deleteRecord(objectName: string, data: Record<string, unknown>, headers: Record<string, string>) { |
| 61 | + const id = data._id || data.id; |
| 62 | + if (!id) throw new Error('Delete record requires _id or id in payload'); |
| 63 | + const response = await fetch(`${this.baseUrl}/api/data/${objectName}/${id}`, { |
| 64 | + method: 'DELETE', |
| 65 | + headers |
| 66 | + }); |
| 67 | + return this.handleResponse(response); |
| 68 | + } |
| 69 | + |
| 70 | + private async readRecord(objectName: string, data: Record<string, unknown>, headers: Record<string, string>) { |
| 71 | + const id = data._id || data.id; |
| 72 | + if (!id) throw new Error('Read record requires _id or id in payload'); |
| 73 | + const response = await fetch(`${this.baseUrl}/api/data/${objectName}/${id}`, { |
| 74 | + method: 'GET', |
| 75 | + headers |
| 76 | + }); |
| 77 | + return this.handleResponse(response); |
| 78 | + } |
| 79 | + |
| 80 | + private async queryRecords(objectName: string, data: Record<string, unknown>, headers: Record<string, string>) { |
| 81 | + // Assuming query via POST or GraphQL-like endpoint |
| 82 | + const response = await fetch(`${this.baseUrl}/api/data/${objectName}/query`, { |
| 83 | + method: 'POST', |
| 84 | + headers, |
| 85 | + body: JSON.stringify(data) |
| 86 | + }); |
| 87 | + return this.handleResponse(response); |
| 88 | + } |
| 89 | + |
| 90 | + private async rawApiCall(endpoint: string, data: Record<string, unknown>, headers: Record<string, string>) { |
| 91 | + const method = (data.method as string) || 'GET'; |
| 92 | + const body = data.body ? JSON.stringify(data.body) : undefined; |
| 93 | + const url = endpoint.startsWith('http') ? endpoint : `${this.baseUrl}${endpoint}`; |
| 94 | + |
| 95 | + const response = await fetch(url, { |
| 96 | + method, |
| 97 | + headers, |
| 98 | + body |
| 99 | + }); |
| 100 | + return this.handleResponse(response); |
| 101 | + } |
| 102 | + |
| 103 | + private async handleResponse(response: Response) { |
| 104 | + if (!response.ok) { |
| 105 | + const text = await response.text(); |
| 106 | + throw new Error(`HTTP Error ${response.status}: ${text}`); |
| 107 | + } |
| 108 | + const contentType = response.headers.get('content-type'); |
| 109 | + if (contentType && contentType.includes('application/json')) { |
| 110 | + return response.json(); |
| 111 | + } |
| 112 | + return response.text(); |
| 113 | + } |
| 114 | +} |
0 commit comments