simple-get-test.html 6 KB
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>simpleGet函数测试</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      max-width: 800px;
      margin: 0 auto;
      padding: 20px;
      background-color: #f5f5f5;
    }
    .test-section {
      background: white;
      margin: 20px 0;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .test-section h3 {
      color: #333;
      margin-top: 0;
    }
    button {
      background: #007cba;
      color: white;
      border: none;
      padding: 10px 20px;
      border-radius: 4px;
      cursor: pointer;
      margin: 5px;
    }
    button:hover {
      background: #005a87;
    }
    .result {
      margin-top: 10px;
      padding: 10px;
      border-radius: 4px;
      background: #f8f9fa;
      border-left: 4px solid #007cba;
      max-height: 300px;
      overflow-y: auto;
    }
    .error {
      border-left-color: #dc3545;
      background: #f8d7da;
    }
    .success {
      border-left-color: #28a745;
      background: #d4edda;
    }
    pre {
      white-space: pre-wrap;
      word-break: break-all;
    }
  </style>
</head>
<body>
  <h1>simpleGet函数测试</h1>
  <p>测试从主入口导出的simpleGet函数,用于简单的GET请求</p>

  <!-- 测试1:基本GET请求 -->
  <div class="test-section">
    <h3>测试1:基本GET请求</h3>
    <p>使用simpleGet函数发送简单的GET请求</p>
    <button onclick="testBasicGet()">测试基本GET请求</button>
    <div id="result1" class="result" style="display: none;"></div>
  </div>

  <!-- 测试2:JSON API请求 -->
  <div class="test-section">
    <h3>测试2:JSON API请求</h3>
    <p>请求JSON格式的API数据</p>
    <button onclick="testJsonApi()">测试JSON API</button>
    <div id="result2" class="result" style="display: none;"></div>
  </div>

  <!-- 测试3:错误处理 -->
  <div class="test-section">
    <h3>测试3:错误处理</h3>
    <p>测试无效URL的错误处理</p>
    <button onclick="testErrorHandling()">测试错误处理</button>
    <div id="result3" class="result" style="display: none;"></div>
  </div>

  <script type="module">
    // 导入 simpleGet 函数
    import { simpleGet } from '../dist/index.esm.js';
    
    // 检查函数是否正确导出
    if (typeof simpleGet !== 'function') {
      console.error('❌ simpleGet函数未正确导出');
      alert('❌ simpleGet函数未正确导出');
      throw new Error('simpleGet函数未正确导出');
    }
    
    console.log('✅ simpleGet函数导出成功,类型:', typeof simpleGet);
    
    // 将函数挂载到全局对象以供其他脚本使用
    window.simpleGet = simpleGet;

    // 测试1:基本GET请求
    window.testBasicGet = async function() {
      const resultDiv = document.getElementById('result1');
      resultDiv.style.display = 'block';
      resultDiv.className = 'result';
      resultDiv.innerHTML = '请求中...';
      
      try {
        if (!simpleGet) {
          throw new Error('simpleGet函数未正确导出');
        }
        
        // 测试一个简单的API
        const response = await simpleGet('https://httpbin.org/get');
        
        resultDiv.className = 'result success';
        resultDiv.innerHTML = `
          <strong>✓ 请求成功</strong><br>
          <strong>响应数据:</strong><br>
          <pre>${JSON.stringify(response, null, 2)}</pre>
        `;
      } catch (error) {
        resultDiv.className = 'result error';
        resultDiv.innerHTML = `
          <strong>✗ 请求失败</strong><br>
          错误信息:${error.message}<br>
          <pre>${error.stack || ''}</pre>
        `;
      }
    }

    // 测试2:JSON API请求
    window.testJsonApi = async function() {
      const resultDiv = document.getElementById('result2');
      resultDiv.style.display = 'block';
      resultDiv.className = 'result';
      resultDiv.innerHTML = '请求中...';
      
      try {
        if (!simpleGet) {
          throw new Error('simpleGet函数未正确导出');
        }
        
        // 测试JSON API
        const response = await simpleGet('https://jsonplaceholder.typicode.com/posts/1');
        
        resultDiv.className = 'result success';
        resultDiv.innerHTML = `
          <strong>✓ JSON API请求成功</strong><br>
          <strong>文章标题:</strong>${response.title}<br>
          <strong>文章内容:</strong>${response.body}<br>
          <strong>完整响应:</strong><br>
          <pre>${JSON.stringify(response, null, 2)}</pre>
        `;
      } catch (error) {
        resultDiv.className = 'result error';
        resultDiv.innerHTML = `
          <strong>✗ JSON API请求失败</strong><br>
          错误信息:${error.message}<br>
          <pre>${error.stack || ''}</pre>
        `;
      }
    }

    // 测试3:错误处理
    window.testErrorHandling = async function() {
      const resultDiv = document.getElementById('result3');
      resultDiv.style.display = 'block';
      resultDiv.className = 'result';
      resultDiv.innerHTML = '测试中...';
      
      try {
        if (!simpleGet) {
          throw new Error('simpleGet函数未正确导出');
        }
        
        // 测试无效URL
        await simpleGet('');
        
        resultDiv.className = 'result error';
        resultDiv.innerHTML = '<strong>✗ 错误处理测试失败</strong><br>应该抛出错误但没有';
      } catch (error) {
        resultDiv.className = 'result success';
        resultDiv.innerHTML = `
          <strong>✓ 错误处理正常</strong><br>
          正确捕获到错误:${error.message}
        `;
      }
    }

    // 页面加载时检查函数是否可用
    window.addEventListener('load', function() {
      if (simpleGet) {
        console.log('✓ simpleGet函数已成功导出并可用');
      } else {
        console.error('✗ simpleGet函数未正确导出');
      }
    });
  </script>
</body>
</html>