forked from tuupke/cuproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuproxy_test.go
More file actions
125 lines (109 loc) · 2.89 KB
/
Copy pathcuproxy_test.go
File metadata and controls
125 lines (109 loc) · 2.89 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
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
121
122
123
124
125
package main
import (
"encoding/binary"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestExtractInt(t *testing.T) {
body := "ipp://localhost:631/jobs/795!\u0000\u0006job-id\u0000\u0004\u0000\u0000\u0003\u001B# \tjob-state \u0004 \u0004A \u0011job-state-message"
val, found := extractInt("job-id", []byte(body))
require.True(t, found)
require.EqualValues(t, 795, val)
body = "\u0001\u0001\u0003\u001B"
fmt.Println(int32(binary.BigEndian.Uint32([]byte(body))))
}
func TestLoadTwice(t *testing.T) {
ip := net.ParseIP("127.0.0.1")
if a, b := Load(ip, nil), Load(ip, nil); a != b {
t.Fail()
}
}
func TestParseToCallString(t *testing.T) {
res, err := parseToCallString(strings.Join([]string{
`team;GET;https://domjudge.org/demoweb/api/|user;DELETE;https://domjudge.org/demoweb/api/`,
`pixie;POST;https://localhost:9000/`,
}, "&&"))
require.Nil(t, err)
require.EqualValues(t, endpointsSet{
endpoints{
endpoint{
method: http.MethodGet,
name: "team",
url: "https://domjudge.org/demoweb/api/",
},
endpoint{
method: http.MethodDelete,
name: "user",
url: "https://domjudge.org/demoweb/api/",
},
},
endpoints{
endpoint{
method: http.MethodPost,
name: "pixie",
url: "https://localhost:9000/",
},
},
}, res)
}
func TestLoad(t *testing.T) {
toCall = endpointsSet{
endpoints{
endpoint{
method: http.MethodGet,
name: "user",
url: "https://jury:jury@www.domjudge.org/demoweb/api/user?strict=false",
},
endpoint{
method: http.MethodGet,
name: "user",
url: "https://jury:jury@www.domjudge.org/demoweb/api/teams/{{user_team_id}}?strict=false",
},
},
endpoints{
endpoint{
method: http.MethodGet,
name: "image",
url: "https://www.w3.org/MarkUp/Test/xhtml-print/20050519/tests/jpeg444.jpg",
},
},
}
ip := net.ParseIP("127.0.0.1")
firstTime := time.Now()
Load(ip, nil)
first := time.Since(firstTime)
secondTime := time.Now()
p := Load(ip, nil)
second := time.Since(secondTime)
assert.Greater(t, first, second)
fmt.Println(firstTime, secondTime, first, second)
bts, err := io.ReadAll(p.json(nil))
fmt.Println(err, string(bts))
}
func TestToJson(t *testing.T) {
p := Load(nil, nil)
doCheck := func(expected map[string]string) {
var found map[string]string
err := json.NewDecoder(p.json(nil)).Decode(&found)
require.Nil(t, err)
require.NotNil(t, found)
assert.EqualValues(t, expected, found)
}
doCheck(make(map[string]string))
// Set a key
p.Store("foo", "bar")
doCheck(map[string]string{"foo": "bar"})
// Set another key, note the second check only serves to illustrate that order
// does not matter.
p.Store("foobar", "baz")
doCheck(map[string]string{"foo": "bar", "foobar": "baz"})
doCheck(map[string]string{"foobar": "baz", "foo": "bar"})
}