Repository navigation
Expand file tree
/
Copy pathProxyCache_test.go
More file actions
92 lines (64 loc) · 2.19 KB
/
Copy pathProxyCache_test.go
File metadata and controls
92 lines (64 loc) · 2.19 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
package structs
import "tholian-warps/protocols/http"
import net_url "net/url"
import "os"
import "strconv"
import "testing"
func TestProxyCache(t *testing.T) {
t.Run("Exists", func(t *testing.T) {
url, _ := net_url.Parse("http://localhost:8080/folder/to/file.html")
tmp, _ := os.MkdirTemp("", "tholian-warps-webcache-*")
webcache := NewProxyCache(tmp)
request := http.NewPacket()
request.SetURL(*url)
request.SetMethod(http.MethodGet)
payload := []byte("Hello, World!")
response := http.NewPacket()
response.SetURL(*url)
response.SetStatus(http.StatusOK)
response.SetHeader("Content-Type", "text/html")
response.SetHeader("Content-Length", strconv.Itoa(len(payload)))
response.SetPayload(payload)
result1 := webcache.Exists(request)
if result1 != false {
t.Errorf("Expected '%t' but got '%t'", false, result1)
}
result2 := webcache.Write(response)
if result2 != true {
t.Errorf("Expected '%t' but got '%t'", true, result2)
}
result3 := webcache.Exists(request)
if result3 != true {
t.Errorf("Expected '%t' but got '%t'", true, result3)
}
os.RemoveAll(tmp)
})
t.Run("Read", func(t *testing.T) {
url, _ := net_url.Parse("http://localhost:8080/folder/to/file.html")
tmp, _ := os.MkdirTemp("", "tholian-warps-webcache-*")
webcache := NewProxyCache(tmp)
request := http.NewPacket()
request.SetURL(*url)
request.SetMethod(http.MethodGet)
payload := []byte("Hello, World!")
response := http.NewPacket()
response.SetURL(*url)
response.SetStatus(http.StatusOK)
response.SetHeader("Content-Type", "text/html")
response.SetHeader("Content-Length", strconv.Itoa(len(payload)))
response.SetPayload(payload)
result1 := webcache.Read(request)
if result1.Status != http.StatusNotFound {
t.Errorf("Expected '%s' but got '%s'", http.Status(http.StatusNotFound).String(), result1.Status.String())
}
result2 := webcache.Write(response)
if result2 != true {
t.Errorf("Expected '%t' but got '%t'", true, result2)
}
result3 := webcache.Read(request)
if result3.Status != http.StatusOK {
t.Errorf("Expected '%s' but got '%s'", http.Status(http.StatusOK).String(), result1.Status.String())
}
os.RemoveAll(tmp)
})
}