File size: 4,544 Bytes
137da8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
add681a
137da8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import requests
import os
import pandas as pd
import io

BASE_URL = "http://127.0.0.1:8000"

def print_status(component, status, message=""):
    symbol = "✅" if status else "❌"
    print(f"{symbol} {component}: {message}")

def test_health():
    try:
        response = requests.get(f"{BASE_URL}/")
        if response.status_code == 200:
            print_status("Health Check", True, "Server is reachable")
        else:
            print_status("Health Check", False, f"Status Code {response.status_code}")
    except Exception as e:
        print_status("Health Check", False, f"Connection Failed: {str(e)}")

def test_training():
    print("\n[ Testing Training Endpoint ]")
    try:
        # Note: This might take a while if actual training happens
        response = requests.post(f"{BASE_URL}/api/train")
        if response.status_code == 200:
            data = response.json()
            print_status("Training", True, data.get("message", "Training started"))
        else:
            print_status("Training", False, f"Failed with {response.status_code}: {response.text}")
    except Exception as e:
        print_status("Training", False, str(e))

def test_single_prediction():
    print("\n[ Testing Single Prediction ]")
    # Sample legitimate-looking data (mostly 1s or 0s depending on feature encoding)
    data = {
        "having_IP_Address": 1,
        "URL_Length": 1,
        "Shortining_Service": 1,
        "having_At_Symbol": 1,
        "double_slash_redirecting": 1,
        "Prefix_Suffix": 1,
        "having_Sub_Domain": 1,
        "SSLfinal_State": 1,
        "Domain_registeration_length": 1,
        "Favicon": 1,
        "port": 1,
        "HTTPS_token": 0,
        "Request_URL": 1,
        "URL_of_Anchor": 1,
        "Links_in_tags": 1,
        "SFH": 1,
        "Submitting_to_email": 1,
        "Abnormal_URL": 1,
        "Redirect": 1,
        "on_mouseover": 1,
        "RightClick": 1,
        "popUpWidnow": 1,
        "Iframe": 1,
        "age_of_domain": 1,
        "DNSRecord": 1,
        "web_traffic": 1,
        "Page_Rank": 1,
        "Google_Index": 1,
        "Links_pointing_to_page": 1,
        "Statistical_report": 1
    }
    
    try:
        response = requests.post(f"{BASE_URL}/api/predict/single", data=data)
        if response.status_code == 200:
            result = response.json()
            print_status("Single Predict", True, f"Prediction: {result.get('prediction')} (Safe: {result.get('is_safe')})")
        else:
            print_status("Single Predict", False, f"Status {response.status_code}: {response.text}")
    except Exception as e:
        print_status("Single Predict", False, str(e))

def test_batch_prediction():
    print("\n[ Testing Batch Prediction ]")
    # Create dummy CSV
    df = pd.DataFrame([{
        "having_IP_Address": 1, "URL_Length": 1, "Shortining_Service": 1, "having_At_Symbol": 1,
        "double_slash_redirecting": 1, "Prefix_Suffix": 1, "having_Sub_Domain": 1, "SSLfinal_State": 1,
        "Domain_registeration_length": 1, "Favicon": 1, "port": 1, "HTTPS_token": 1, "Request_URL": 1,
        "URL_of_Anchor": 1, "Links_in_tags": 1, "SFH": 1, "Submitting_to_email": 1, "Abnormal_URL": 1,
        "Redirect": 1, "on_mouseover": 1, "RightClick": 1, "popUpWidnow": 1, "Iframe": 1, "age_of_domain": 1,
        "DNSRecord": 1, "web_traffic": 1, "Page_Rank": 1, "Google_Index": 1, "Links_pointing_to_page": 1,
        "Statistical_report": 1
    }] * 5) # 5 rows
    
    csv_buffer = io.StringIO()
    df.to_csv(csv_buffer, index=False)
    csv_buffer.seek(0)
    
    files = {'file': ('test_batch.csv', csv_buffer.getvalue(), 'text/csv')}
    
    try:
        response = requests.post(f"{BASE_URL}/predict", files=files)
        if response.status_code == 200:
            # It returns a template, so checking text length or specific marker
            if "Prediction Results" in response.text or "table" in response.text:
                print_status("Batch Predict", True, "Received HTML response with results")
            else:
                print_status("Batch Predict", True, f"Response received (Length: {len(response.text)})")
        else:
            print_status("Batch Predict", False, f"Status {response.status_code}")
    except Exception as e:
        print_status("Batch Predict", False, str(e))

if __name__ == "__main__":
    print("🚀 Starting Unified Verification...")
    test_health()
    test_training()
    test_single_prediction()
    test_batch_prediction()
    print("\n✨ Verification Complete")