Repository navigation
Expand file tree
/
Copy pathtest_main.php
More file actions
89 lines (67 loc) · 4.12 KB
/
Copy pathtest_main.php
File metadata and controls
89 lines (67 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* Run: php test_main.php
* Unit tests for main.php logic — no live API calls.
*/
$pass = 0;
$fail = 0;
function ok(bool $condition, string $label): void {
global $pass, $fail;
if ($condition) {
echo "[PASS] {$label}\n";
$pass++;
} else {
echo "[FAIL] {$label}\n";
$fail++;
}
}
// ── Credential validation ────────────────────────────────────────────────────
function credentialsValid(?string $u, ?string $p): bool {
return !empty($u) && !empty($p);
}
ok(!credentialsValid("", "pass"), "empty username fails validation");
ok(!credentialsValid("user", ""), "empty password fails validation");
ok(!credentialsValid("", ""), "both empty fails validation");
ok(!credentialsValid(null, "pass"), "null username fails validation");
ok(!credentialsValid("user", null), "null password fails validation");
ok(credentialsValid("user", "pass"),"valid credentials pass");
// ── Payload structure ────────────────────────────────────────────────────────
$payload = [
"prompt" => "What are the top SEO trends in 2026?",
"engine" => "chatgpt",
"web_search" => true,
"country" => "us",
];
ok(isset($payload["prompt"]), "payload has prompt key");
ok($payload["engine"] === "chatgpt", "payload engine is chatgpt");
ok($payload["web_search"] === true, "payload web_search is true (bool)");
ok($payload["country"] === "us", "payload country is us");
// ── JSON encoding ────────────────────────────────────────────────────────────
$encoded = json_encode($payload);
ok($encoded !== false, "payload encodes to valid JSON");
$decoded = json_decode($encoded, true);
ok($decoded["engine"] === "chatgpt", "decoded engine matches");
ok($decoded["web_search"] === true, "decoded web_search is bool true");
// ── Basic auth encoding ──────────────────────────────────────────────────────
$username = "testuser";
$password = "testpass";
$authString = base64_encode("{$username}:{$password}");
ok(strlen($authString) > 0, "base64 auth string is non-empty");
$decoded_auth = base64_decode($authString);
ok($decoded_auth === "testuser:testpass", "base64 decodes to correct credentials");
// ── API URL ──────────────────────────────────────────────────────────────────
$apiUrl = "https://llm-scraper.netnut.io/search";
ok(str_starts_with($apiUrl, "https://"), "API URL uses HTTPS");
ok(str_contains($apiUrl, "netnut.io"), "API URL targets netnut.io");
ok(str_ends_with($apiUrl, "/search"), "API URL ends with /search");
// ── json_last_error() guard ──────────────────────────────────────────────────
$good = json_decode('{"answer":"ok"}', true);
ok(json_last_error() === JSON_ERROR_NONE, "valid JSON produces no error");
$bad = json_decode('not-json', true);
ok(json_last_error() !== JSON_ERROR_NONE, "invalid JSON triggers json_last_error");
// ── Timeout ──────────────────────────────────────────────────────────────────
$timeout = 180;
ok($timeout === 180, "timeout is 180 seconds");
// ── Summary ──────────────────────────────────────────────────────────────────
echo "\n{$pass} passed, {$fail} failed\n";
exit($fail > 0 ? 1 : 0);