-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_request_test.go
91 lines (75 loc) · 2.35 KB
/
http_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
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
package clarity
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRequestParamTest(t *testing.T) {
var result interface{}
URL, _ := url.Parse("http://localhost?key=value&number=1&long_number=10&array[]=elem1,elem2")
wrongURL, _ := url.Parse("http://localhost?key=value&number=one&long_number=ten&array[]=elem1,elem2")
defaults := map[string]interface{}{
"s": "default",
"i": 0,
"i64": int64(0),
"a": []string{"default"},
}
// context: missing param
// string param
result = RequestParam(URL, "miss", defaults["s"]).(string)
assert.Equal(t, result, defaults["s"])
// int param
result = RequestParam(URL, "miss", defaults["i"]).(int)
assert.Equal(t, result, defaults["i"])
// int64 param
result = RequestParam(URL, "miss", defaults["i64"]).(int64)
assert.Equal(t, result, defaults["i64"])
// array param
result = RequestParam(URL, "miss", defaults["a"]).([]string)
assert.Equal(t, result, defaults["a"])
// context: existing param
// string param
result = RequestParam(URL, "key", defaults["s"]).(string)
assert.Equal(t, result, "value")
// int param
result = RequestParam(URL, "number", defaults["i"]).(int)
assert.Equal(t, result, 1)
// wrong int param
result = RequestParam(wrongURL, "number", defaults["i"]).(int)
assert.Equal(t, result, 0)
// int64 param
result = RequestParam(URL, "long_number", defaults["i64"]).(int64)
assert.Equal(t, result, int64(10))
// wrong int64 param
result = RequestParam(wrongURL, "long_number", defaults["i64"]).(int64)
assert.Equal(t, result, int64(0))
// array param
result = RequestParam(URL, "array", defaults["a"]).([]string)
assert.Equal(t, result, []string{"elem1", "elem2"})
}
func TestRequestBody(t *testing.T) {
var request *http.Request
var body []byte
var result interface{}
var err error
data := map[string]interface{}(
map[string]interface{}{
"key": "value",
},
)
// error on reading body
body = []byte("errror")
request, _ = http.NewRequest("GET", "http://localhost", bytes.NewBuffer(body))
err = RequestBody(request.Body, &result)
assert.NotEqual(t, result, data)
assert.NotNil(t, err)
// normal case
body, _ = json.Marshal(data)
request, _ = http.NewRequest("GET", "http://localhost", bytes.NewBuffer(body))
err = RequestBody(request.Body, &result)
assert.Equal(t, result, data)
assert.Nil(t, err)
}