0, 'fail' => 0]; $check = function ($name, $ok, $detail = '') use (&$stat) { if ($ok) { $stat['pass']++; echo ' [PASS] ' . $name . PHP_EOL; } else { $stat['fail']++; echo ' [FAIL] ' . $name . ($detail === '' ? '' : ' -> ' . $detail) . PHP_EOL; } }; $base = 'https://img.example.com/tinyimg/functions/upload.php'; echo PHP_EOL . '1. 图床响应解析' . PHP_EOL; $cases = [ [ 'name' => 'TinyImg 2.0 成功响应(源码里的字段结构)', 'body' => '{"code":1,"id":9527,"url":"https://img.example.com/tinyimg/temp/2611/abcd.jpg","width":800,"height":600}', 'code' => 1, 'url' => 'https://img.example.com/tinyimg/temp/2611/abcd.jpg', ], [ 'name' => 'TinyImg 2.0 失败响应(Token 无效 / 非法请求)', 'body' => '{"code":0,"msg":"\u975e\u6cd5\u8bf7\u6c42\uff01"}', 'code' => 0, 'url' => null, ], [ 'name' => 'TinyImg 2.x 成功响应(data.url 嵌套)', 'body' => '{"code":200,"msg":"success","data":{"url":"https://img.example.com/a.png"}}', 'code' => 1, 'url' => 'https://img.example.com/a.png', ], [ 'name' => '通用 success / data.url 响应', 'body' => '{"success":true,"data":{"url":"https://img.example.com/b.jpg"}}', 'code' => 1, 'url' => 'https://img.example.com/b.jpg', ], [ 'name' => 'code=0 但带了 url,仍必须判失败', 'body' => '{"code":0,"url":"https://img.example.com/c.jpg","msg":"非法请求"}', 'code' => 0, 'url' => null, ], [ 'name' => '非 JSON 的 HTML 错误页', 'body' => '500 Internal Server Error', 'code' => 0, 'url' => null, ], [ 'name' => '空响应(PHP 报错 / 5xx 常见表现)', 'body' => '', 'code' => 0, 'url' => null, ], [ 'name' => 'JSON 前有 PHP notice 也能解析', 'body' => 'Notice: undefined index in upload.php' . "\n" . '{"code":1,"url":"https://img.example.com/d.png"}', 'code' => 1, 'url' => 'https://img.example.com/d.png', ], ]; foreach ($cases as $case) { $got = Util::parseResponse($case['body'], $base); $ok = ($got['code'] === $case['code']) && (($got['url'] ?: null) === $case['url']); $check($case['name'], $ok, json_encode($got, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); } echo PHP_EOL . '2. 图片地址归一化' . PHP_EOL; $normalized = [ ['{"code":1,"url":"/tinyimg/temp/2611/x.png"}', 'https://img.example.com/tinyimg/temp/2611/x.png'], ['{"code":1,"url":"//cdn.example.com/x.png"}', 'https://cdn.example.com/x.png'], ['{"code":1,"url":"temp/2611/x.jpg"}', 'https://img.example.com/tinyimg/functions/temp/2611/x.jpg'], ]; foreach ($normalized as $item) { $got = Util::parseResponse($item[0], $base); $check('归一化 ' . $item[1], $got['code'] === 1 && $got['url'] === $item[1], (string) $got['url']); } echo PHP_EOL . '3. 配置文本解析' . PHP_EOL; $pairs = Util::parsePairs("# 注释行\ntoken=abc123\nuid=1\n\nfolder=3", '='); $check('key=value 解析', count($pairs) === 3 && $pairs['token'] === 'abc123' && $pairs['uid'] === '1', json_encode($pairs)); $headerPairs = Util::parsePairs("Referer: https://img.example.com/tinyimg/\nCookie: sid=abc", ':'); $check( 'Key: Value 中值含冒号', $headerPairs['Referer'] === 'https://img.example.com/tinyimg/' && $headerPairs['Cookie'] === 'sid=abc', json_encode($headerPairs) ); $check( 'deriveReferer 去掉 functions 层', Util::deriveReferer($base) === 'https://img.example.com/tinyimg/', Util::deriveReferer($base) ); $check( 'deriveReferer 通用 /api/upload', Util::deriveReferer('https://x.example.com/api/upload') === 'https://x.example.com/api/' ); echo PHP_EOL . '4. 连接自检地址(?ping=1)' . PHP_EOL; $pingCases = [ ['https://img.example.com/functions/upload.php', 'https://img.example.com/functions/upload.php?ping=1'], ['https://img.example.com/functions/upload.php?x=1', 'https://img.example.com/functions/upload.php?x=1&ping=1'], ['https://img.example.com/functions/upload.php#frag', 'https://img.example.com/functions/upload.php?ping=1'], ['', ''], ]; foreach ($pingCases as $i => $case) { $got = Util::pingUrl($case[0]); $check('pingUrl 用例 ' . ($i + 1), $got === $case[1], $got); } $check('pingUrl 输出是 http(s) 绝对地址', (bool) preg_match('#^https://.+\?ping=1$#', Util::pingUrl('https://img.example.com/functions/upload.php'))); echo PHP_EOL . '5. 测试集有效性反证(故意错误的实现)' . PHP_EOL; // 错误实现 A:只认 TinyImg 2.x 的 data.url,漏掉 1.x 的顶层 url $badV2Only = function ($body) { $data = json_decode($body, true); return isset($data['data']['url']) ? $data['data']['url'] : null; }; $check( '错误实现 A 在 1.x 成功样例上必须取不到 URL', $badV2Only($cases[0]['body']) === null ); $check( '正确实现在同一份样例上必须成功', Util::parseResponse($cases[0]['body'], $base)['code'] === 1 ); // 错误实现 B:忽略 code=0,只要看到 url 就算成功 $badIgnoreCode = function ($body) { $data = json_decode($body, true); return isset($data['url']) ? $data['url'] : null; }; $failureWithUrl = '{"code":0,"url":"https://img.example.com/c.jpg","msg":"非法请求"}'; $check( '错误实现 B 会把失败响应误判为成功', $badIgnoreCode($failureWithUrl) === 'https://img.example.com/c.jpg' ); $check( '正确实现必须把该响应判为失败', Util::parseResponse($failureWithUrl, $base)['code'] === 0 ); echo PHP_EOL . str_repeat('-', 56) . PHP_EOL; echo '结果:' . $stat['pass'] . ' 通过 / ' . $stat['fail'] . ' 失败' . PHP_EOL; exit($stat['fail'] === 0 ? 0 : 1);