-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_test.go
54 lines (48 loc) · 1.43 KB
/
request_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
package mantil
import (
"encoding/json"
"fmt"
"io/ioutil"
"testing"
"github.com/stretchr/testify/require"
)
func TestParseRequest(t *testing.T) {
data := []struct {
filename string
typ RequestType
methods []string
}{
{"api-gateway.json", APIGateway, []string{"MethodName"}},
{"ws-connect.json", WSConnect, []string{"Connect", ""}},
{"ws-disconnect.json", WSDisconnect, []string{"Disconnect", ""}},
{"ws-message.json", WSMessage, []string{"Message", ""}},
{"streaming.json", Streaming, []string{"methodName"}},
{"console_test.json", RequestTypeUnknown, []string{"myMethod"}},
{"bad_attribute_type.json", RequestTypeUnknown, []string{"method"}},
}
for _, d := range data {
buf, err := ioutil.ReadFile("testdata/" + d.filename)
require.NoError(t, err)
req := parseRequest(buf)
require.Equal(t, d.typ, req.Type, d.filename)
require.Equal(t, buf, req.Raw)
require.Equal(t, d.methods, req.Methods, d.filename)
if req.Type == WSConnect || req.Type == WSDisconnect {
require.Nil(t, req.Body)
continue
}
var kv struct {
Key string
}
err = json.Unmarshal(req.Body, &kv)
require.NoError(t, err, d.filename)
require.Equal(t, "value", kv.Key, d.filename)
fmt.Printf("remoteIP: %s", req.RemoteIP())
}
}
func TestRemoteIP(t *testing.T) {
buf, err := ioutil.ReadFile("testdata/api-gateway.json")
require.NoError(t, err)
req := parseRequest(buf)
require.Equal(t, "93.136.54.42", req.RemoteIP())
}