-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl_test.go
More file actions
95 lines (81 loc) · 1.94 KB
/
url_test.go
File metadata and controls
95 lines (81 loc) · 1.94 KB
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
package httputil
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
var (
// 模拟一个jquery.ajax数据
postData = map[string][]string{
"id": []string{"10068"},
"name": []string{"涂涂"},
"title": []string{"123456"},
"tags[]": []string{"ruby", "jason", "mike"},
"ids[]": []string{"1", "2", "3"},
"coauthor": []string{"🐦", "🐟", "🐘"},
}
)
type TodoAuthor struct {
Name string `http:"name"`
}
type TodoItem struct {
ID int64 `http:"id"`
Title string `http:"title"`
Done bool `http:"done,omit"`
Tags []string `http:"tags[],omit"`
IDs []*int `http:"ids[]"`
TodoAuthor
}
func TestUnpackURLValues(t *testing.T) {
f1 := &TodoItem{}
err := UnpackURLValues(postData, f1)
if err != nil {
fmt.Println(err)
t.Fail()
}
fmt.Println(f1)
}
func TestUnpackRequest(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
f1 := &TodoItem{}
err := UnpackRequest(r, f1)
if err != nil {
w.WriteHeader(400)
io.WriteString(w, err.Error())
} else {
io.WriteString(w, fmt.Sprintf("%v", f1))
}
}
req := httptest.NewRequest("GET", "http://example.com/?id=1&title=hello&name=tuhuayuan&ids[]=1&ids[]=2&ids[]=3", nil)
w := httptest.NewRecorder()
handler(w, req)
assert.Equal(t, 200, w.Code)
all, _ := ioutil.ReadAll(w.Body)
fmt.Println(string(all))
}
func TestMapURLValues(t *testing.T) {
f1 := &TodoItem{}
e := &ValidateErrors{}
mapURLValues(reflect.ValueOf(f1), postData, e)
fmt.Println(f1)
}
func TestSetProperType(t *testing.T) {
f1 := &TodoItem{}
v := reflect.ValueOf(f1).Elem()
err := setProperType(reflect.TypeOf(f1.Title), v.Field(1), "123")
assert.NoError(t, err)
assert.Equal(t, "123", f1.Title)
}
func TestUnpackError(t *testing.T) {
f1 := &TodoItem{}
e := &ValidateErrors{}
mapURLValues(reflect.ValueOf(f1), map[string][]string{
"id": []string{"abc"},
}, e)
fmt.Println(e)
}