-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput_test.go
More file actions
94 lines (82 loc) · 2.01 KB
/
Copy pathoutput_test.go
File metadata and controls
94 lines (82 loc) · 2.01 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
90
91
92
93
94
package tests
import (
"encoding/json"
"os"
"testing"
"time"
"github.com/ResistanceIsUseless/ProxyHawk/internal/output"
)
// TestOutputFormatting tests the output formatting functions
func TestOutputFormatting(t *testing.T) {
// Create test results
results := []output.ProxyResultOutput{
{
Proxy: "http://test1.com:8080",
Working: true,
Speed: 100 * time.Millisecond,
InteractshTest: true,
IsAnonymous: true,
CloudProvider: "AWS",
Timestamp: time.Now(),
},
{
Proxy: "http://test2.com:8080",
Working: false,
Error: "connection refused",
Speed: 0,
},
}
summary := output.SummaryOutput{
TotalProxies: 2,
WorkingProxies: 1,
InteractshProxies: 1,
AnonymousProxies: 1,
CloudProxies: 1,
SuccessRate: 50.0,
Results: results,
}
// Test text output
t.Run("Text Output", func(t *testing.T) {
tempFile := "test_output.txt"
defer os.Remove(tempFile)
err := output.WriteTextOutput(tempFile, results, summary)
if err != nil {
t.Errorf("output.WriteTextOutput() error = %v", err)
return
}
// Verify file exists and is not empty
stat, err := os.Stat(tempFile)
if err != nil {
t.Errorf("Failed to stat output file: %v", err)
return
}
if stat.Size() == 0 {
t.Error("Output file is empty")
}
})
// Test JSON output
t.Run("JSON Output", func(t *testing.T) {
tempFile := "test_output.json"
defer os.Remove(tempFile)
jsonData, err := json.MarshalIndent(summary, "", " ")
if err != nil {
t.Errorf("Failed to marshal JSON: %v", err)
return
}
err = os.WriteFile(tempFile, jsonData, 0644)
if err != nil {
t.Errorf("Failed to write JSON file: %v", err)
return
}
// Verify file exists and is valid JSON
data, err := os.ReadFile(tempFile)
if err != nil {
t.Errorf("Failed to read JSON file: %v", err)
return
}
var decoded output.SummaryOutput
if err := json.Unmarshal(data, &decoded); err != nil {
t.Errorf("Failed to decode JSON: %v", err)
}
})
}