-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoteip_test.go
113 lines (96 loc) · 5.93 KB
/
remoteip_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
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
package main
import (
"io/ioutil"
"net/http"
"strings"
"testing"
)
var apiTests = []struct {
method string
postData string
url string
status int
expected string
}{
// plain
{`GET`, ``, `http://127.0.0.1:8080/plain`, 200, `127.0.0.1`},
{`POST`, ``, `http://127.0.0.1:8080/plain`, 200, `127.0.0.1`},
// json
{`GET`, ``, `http://127.0.0.1:8080/json`, 200, `{"ip":"127.0.0.1"}`},
{`POST`, ``, `http://127.0.0.1:8080/json`, 200, `{"ip":"127.0.0.1"}`},
// jsonp
{`POST`, ``, `http://127.0.0.1:8080/jsonp`, 405, `Method Not Allowed`},
{`GET`, ``, `http://127.0.0.1:8080/jsonp`, 200, `callback("127.0.0.1");`},
{`GET`, ``, `http://127.0.0.1:8080/jsonp?call`, 200, `callback("127.0.0.1");`},
{`GET`, ``, `http://127.0.0.1:8080/jsonp?callback=`, 200, `callback("127.0.0.1");`},
{`GET`, ``, `http://127.0.0.1:8080/jsonp?callback=G`, 200, `G("127.0.0.1");`},
// jsonrpc
// ERROR
// wrong method
{`GET`, ``, `http://127.0.0.1:8080/jsonrpc`, 405, `Method Not Allowed`},
// not valid json
{`POST`, ``, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}`},
{`POST`, `{"json`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}}`},
// `id` as empty string
{`POST`, `{"jsonrpc":"2.0","id":"","method":"GET"}`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}}`},
// `id` is bool
{`POST`, `{"jsonrpc":"2.0","id":true,"method":"GET"}`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}}`},
// `method` missing
{`POST`, `{"jsonrpc":"2.0","id":"xDcF"}`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":"xDcF","error":{"code":-32600,"message":"Invalid Request"}}`},
// `method` mixed case
{`POST`, `{"jsonrpc":"2.0","id":"xDcF","Method":"GET"}`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":"xDcF","error":{"code":-32600,"message":"Invalid Request"}}`},
// `method` as empty string
{`POST`, `{"jsonrpc":"2.0","id":"xDcF","method":""}`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":"xDcF","error":{"code":-32600,"message":"Invalid Request"}}`},
// `jsonrpc` missing
{`POST`, `{"id":"xDcF","method":"GET"}`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":"xDcF","error":{"code":-32600,"message":"Invalid Request"}}`},
// `jsonrpc` mixed case
{`POST`, `{"jsonRPC":"2.0","id":"xDcF","method":"GET"}`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":"xDcF","error":{"code":-32600,"message":"Invalid Request"}}`},
// `jsonrpc` wrong version
{`POST`, `{"jsonrpc":"2.5","id":"xDcF","method":"GET"}`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":"xDcF","error":{"code":-32600,"message":"Invalid Request"}}`},
// `jsonrpc` not string
{`POST`, `{"jsonrpc":2.0,"id":"xDcF","method":"GET"}`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":"xDcF","error":{"code":-32600,"message":"Invalid Request"}}`},
// OK
// `id` as string
{`POST`, `{"jsonrpc":"2.0","id":"xDcF","method":"GET"}`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":"xDcF","result":"127.0.0.1"}`},
// `id` as int
{`POST`, `{"jsonrpc":"2.0","id":453,"method":"GET"}`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":453,"result":"127.0.0.1"}`},
// `id` as float
{`POST`, `{"jsonrpc":"2.0","id":453.4,"method":"GET"}`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":453.4,"result":"127.0.0.1"}`},
// `id` is null
{`POST`, `{"jsonrpc":"2.0","id":null,"method":"GET"}`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":null,"result":"127.0.0.1"}`},
// notification
{`POST`, `{"jsonrpc":"2.0","method":"GET"}`, `http://127.0.0.1:8080/jsonrpc`, 200, ``},
// batch
{`POST`, `[{"jsonrpc":"2.0","id":"xDcF","method":"GET"}]`, `http://127.0.0.1:8080/jsonrpc`, 200, `[{"jsonrpc":"2.0","id":"xDcF","result":"127.0.0.1"}]`},
{`POST`, `[{"jsonrpc":"2.0","id":"xDcF","method":"GET"},{"jsonrpc":"2.0","id":"s4bH","method":"GET"}]`, `http://127.0.0.1:8080/jsonrpc`, 200, `[{"jsonrpc":"2.0","id":"xDcF","result":"127.0.0.1"},{"jsonrpc":"2.0","id":"s4bH","result":"127.0.0.1"}]`},
{`POST`, `[]`, `http://127.0.0.1:8080/jsonrpc`, 200, `{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}}`},
{`POST`, `[1]`, `http://127.0.0.1:8080/jsonrpc`, 200, `[{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}}]`},
{`POST`, `[1,2,3]`, `http://127.0.0.1:8080/jsonrpc`, 200, `[{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}},{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}},{"jsonrpc":"2.0","id":null,"error":{"code":-32600,"message":"Invalid Request"}}]`},
{`POST`, `[{"jsonrpc":"2.0","method":"GET"},{"jsonrpc":"2.0","method":"GET"}]`, `http://127.0.0.1:8080/jsonrpc`, 200, ``},
{`POST`, `[{"jsonrpc":"2.0","id":"dRgg","method":"GET"},{"jsonrpc":"2.0","method":"GET"}]`, `http://127.0.0.1:8080/jsonrpc`, 200, `[{"jsonrpc":"2.0","id":"dRgg","result":"127.0.0.1"}]`},
{`POST`, `[{"jsonrpc":"2.0","id":"dRgg","method":"GET"},{"jsonrpc":"2.5","id":"KJid","method":"GET"}]`, `http://127.0.0.1:8080/jsonrpc`, 200, `[{"jsonrpc":"2.0","id":"dRgg","result":"127.0.0.1"},{"jsonrpc":"2.0","id":"KJid","error":{"code":-32600,"message":"Invalid Request"}}]`},
}
func TestAPI(t *testing.T) {
hc := http.Client{}
for _, tt := range apiTests {
t.Log(tt.method, tt.postData, tt.url)
req, err := http.NewRequest(tt.method, tt.url, strings.NewReader(tt.postData))
if err != nil {
t.Error(err)
}
resp, err := hc.Do(req)
if err != nil {
t.Error(err)
}
if resp.StatusCode != tt.status {
t.Error("expected status:", tt.status, "got:", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
if tt.expected != string(body) {
t.Error("expected:", tt.expected, "got:", string(body))
}
}
}