-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
54 lines (42 loc) · 2.13 KB
/
utils_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 winter
import (
"bytes"
"github.com/stretchr/testify/require"
"net/http/httptest"
"testing"
)
func TestFlattenSimpleSlice(t *testing.T) {
require.Equal(t, "a", flattenSingleSlice([]string{"a"}))
require.Equal(t, []int{1, 2}, flattenSingleSlice([]int{1, 2}))
}
func TestExtractRequest(t *testing.T) {
req := httptest.NewRequest("GET", "https://example.com/get?aaa=bbb", nil)
m := map[string]any{}
err := extractRequest(m, req)
require.NoError(t, err)
require.Equal(t, map[string]any{"aaa": "bbb", "query_aaa": "bbb"}, m)
req = httptest.NewRequest("POST", "https://example.com/post?aaa=bbb", bytes.NewReader([]byte(`{"hello":"world"}`)))
req.Header.Set("Content-Type", "application/json;charset=utf-8")
m = map[string]any{}
err = extractRequest(m, req)
require.NoError(t, err)
require.Equal(t, map[string]any{"aaa": "bbb", "header_content_type": "application/json;charset=utf-8", "hello": "world", "query_aaa": "bbb"}, m)
req = httptest.NewRequest("POST", "https://example.com/post?aaa=bbb", bytes.NewReader([]byte(`hello=world`)))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
m = map[string]any{}
err = extractRequest(m, req)
require.NoError(t, err)
require.Equal(t, map[string]any{"aaa": "bbb", "header_content_type": "application/x-www-form-urlencoded;charset=utf-8", "hello": "world", "query_aaa": "bbb"}, m)
req = httptest.NewRequest("POST", "https://example.com/post?aaa=bbb", bytes.NewReader([]byte(`hello=world`)))
req.Header.Set("Content-Type", "text/plain;charset=utf-8")
m = map[string]any{}
err = extractRequest(m, req)
require.NoError(t, err)
require.Equal(t, map[string]any{"aaa": "bbb", "header_content_type": "text/plain;charset=utf-8", "query_aaa": "bbb", "body": "hello=world"}, m)
req = httptest.NewRequest("POST", "https://example.com/post?aaa=bbb", bytes.NewReader([]byte(`hello=world`)))
req.Header.Set("Content-Type", "application/x-custom")
m = map[string]any{}
err = extractRequest(m, req)
require.NoError(t, err)
require.Equal(t, map[string]any{"aaa": "bbb", "header_content_type": "application/x-custom", "query_aaa": "bbb", "body": []byte("hello=world")}, m)
}