у меня есть код, который с отправлет запросы на сайте, там происходит проверка его…
- User
у меня есть код, который с отправлет запросы на сайте, там происходит проверка его статуса: import React, { useState } from 'react'; import axios from 'axios';
const apiTests = [ { id: 1, description: 'Создание подключения', method: 'POST', url: 'http://10.80.36.11:8090/cd/bi/createConnection', headers: { 'Content-Type': 'application/json', }, body: { "host": "clickhouse://10.80.36.13", "name": "{{randomString}}", "port": 8123, "type": "clickhouse", "username": "clusterdata", "password": "p5H-5Df-4bz-Cwk", "dirPath": "1_Актульное для стейбл/1 Баркова/Подключения123/" } }, // { // id: 2, // description: 'Проверка POST /posts', // method: 'POST', // url: 'https://jsonplaceholder.typicode.com/posts', // headers: { // 'Content-Type': 'application/json', // }, // body: { // title: 'foo', // body: 'bar', // userId: 1 // } // }, // Добавь свои запросы сюда ];
function App() { const [isTesting, setIsTesting] = useState(false); const [progress, setProgress] = useState(0); const [results, setResults] = useState([]);
async function runApiTests() { setIsTesting(true); setResults([]); setProgress(0);
for (let i = 0; i < apiTests.length; i++) { const test = apiTests[i]; try { let response; if (test.method === 'GET' || test.method === 'DELETE') { response = await axios({ method: test.method, url: test.url, headers: test.headers, }); } else { // POST, PUT, PATCH response = await axios({ method: test.method, url: test.url, headers: test.headers, data: test.body, }); } // Ответ с кодом 2xx считаем успехом const passed = response.status >= 200 && response.status < 300; setResults(prev => [ ...prev, { id: test.id, description: test.description, passed, status: response.status, statusText: response.statusText, data: response.data, } ]); } catch (error) { // Если ошибка запроса — считаем провал setResults(prev => [ ...prev, { id: test.id, description: test.description, passed: false, error: error.message, } ]); } setProgress(Math.round(((i + 1) / apiTests.length) * 100)); } setIsTesting(false);}
return ( <div style={{ maxWidth: 800, margin: '20px auto', fontFamily: 'Arial' }}> API Тесты (симулятор) <button onClick={runApiTests} disabled={isTesting} style={{ padding: '10px 20px', fontSize: 16 }} > {isTesting ? 'Тестирование...' : 'Запустить тесты'}
{/* Прогресс-бар */} {isTesting && ( <div style={{ marginTop: 20, height: 20, backgroundColor: '#eee', borderRadius: 10, overflow: 'hidden' }}> <div style={{ height: '100%', width: `${progress}%`, backgroundColor: '#4caf50', transition: 'width 0.3s ease', }} /> </div> )} {/* Результаты */} {!isTesting && results.length > 0 && ( <div style={{ marginTop: 20 }}> <h2>Результаты:</h2> <ul style={{ listStyle: 'none', paddingLeft: 0 }}> {results.map(r => ( <li key={r.id} style={{ marginBottom: 10, padding: 10, borderRadius: 5, backgroundColor: r.passed ? 'green' : 'red', color: 'white', }}> <strong>{r.passed ? '✔' : '✖'} {r.description}</strong><br /> Статус: {r.status || 'Ошибка'} {r.statusText || ''}<br /> {r.error && <span>Ошибка: {r.error}</span>} {/* При желании можно выводить r.data (ответ) */} </li> ))} </ul> </div> )} </div> - Assistant