-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
79 lines (60 loc) · 2.09 KB
/
server_test.go
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
package main
import (
"ShadowTest/ssproxy"
"bytes"
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestHealthcheck(t *testing.T) {
router, err := getRouter(true)
assert.NoError(t, err)
req, _ := http.NewRequest("GET", "/health", nil)
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
}
func TestGetProxyDetailsFromServerJSON(t *testing.T) {
address := "ss://Y2hhY2hhMjAtaWV0Zi1wb2x5MTMwNTpwYXNzd29yZA@localhost:6276/?outline=1"
router, err := getRouter(true)
assert.NoError(t, err)
body := bytes.NewBuffer([]byte(fmt.Sprintf("{ \"address\":\"%s\" }", address)))
req, _ := http.NewRequest("POST", "/v2/test", body)
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
details := ssproxy.WTFIsMyIPData{}
err = json.NewDecoder(rr.Body).Decode(&details)
assert.NoError(t, err)
assert.NotEmpty(t, details.YourFuckingIPAddress)
assert.NotEmpty(t, details.YourFuckingLocation)
}
func TestGetProxyDetailsFromServerTimeout(t *testing.T) {
address := "ss://Y2hhY2hhMjAtaWV0Zi1wb2x5MTMwNTpwYXNzd29yZA@shadowtest.akiel.dev:6276"
router, err := getRouter(true)
assert.NoError(t, err)
body := bytes.NewBuffer([]byte(fmt.Sprintf("{ \"address\":\"%s\", \"timeout\": 1 }", address)))
req, _ := http.NewRequest("POST", "/v2/test", body)
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
}
func TestDeprecatedV1(t *testing.T) {
router, err := getRouter(true)
assert.NoError(t, err)
body := bytes.NewBuffer([]byte(fmt.Sprintf("{ \"address\":\"%s\" }", "")))
req, _ := http.NewRequest("POST", "/v1/test", body)
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
router.ServeHTTP(rr, req)
assert.Equal(t, http.StatusNotFound, rr.Code)
content, err := io.ReadAll(rr.Body)
assert.NoError(t, err)
assert.Equal(t, []byte("Deprecated endpoint. Use v2 instead.\n"), content)
}