PHP Classes

File: tests/test_api_routes.php

Recommend this page to a friend!
  Packages of Matthew Asham   Binkterm PHP   tests/test_api_routes.php   Download  
File: tests/test_api_routes.php
Role: Auxiliary script
Content type: text/plain
Description: Configuration script
Class: Binkterm PHP
Bulletin board system based on the Web
Author: By
Last change:
Date: 5 days ago
Size: 1,527 bytes
 

Contents

Class file image Download
<?php

echo "Testing API Routes via HTTP\n";
echo
"===========================\n\n";

// Test routes by making internal HTTP requests
function testRoute($url) {
   
$fullUrl = "http://localhost:8000" . $url; // Adjust port as needed
   
   
$context = stream_context_create([
       
'http' => [
           
'method' => 'GET',
           
'header' => "Accept: application/json\r\n",
           
'timeout' => 10
       
]
    ]);
   
    echo
"Testing: $url\n";
   
   
$response = @file_get_contents($fullUrl, false, $context);
   
    if (
$response === false) {
       
$error = error_get_last();
        echo
" ? Failed: " . ($error['message'] ?? 'Unknown error') . "\n\n";
        return
false;
    }
   
   
$httpCode = null;
    if (isset(
$http_response_header)) {
        foreach (
$http_response_header as $header) {
            if (
preg_match('/^HTTP\/\d+\.\d+\s+(\d+)/', $header, $matches)) {
               
$httpCode = intval($matches[1]);
                break;
            }
        }
    }
   
    echo
" Status Code: $httpCode\n";
    echo
" Response: " . substr($response, 0, 200) . "\n\n";
   
    return
$httpCode === 200;
}

// Test the statistics endpoints
echo "1. Testing netmail stats endpoint:\n";
testRoute('/api/messages/netmail/stats');

echo
"2. Testing echomail stats endpoint:\n";
testRoute('/api/messages/echomail/stats');

echo
"3. Testing basic API endpoint:\n";
testRoute('/api/messages/netmail');

echo
"4. Testing non-API route:\n";
testRoute('/login');

echo
"Test completed.\n";