github.com/astaxie/beego@v1.12.3/context/input_test.go (about) 1 // Copyright 2014 beego Author. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package context 16 17 import ( 18 "net/http" 19 "net/http/httptest" 20 "reflect" 21 "testing" 22 ) 23 24 func TestBind(t *testing.T) { 25 type testItem struct { 26 field string 27 empty interface{} 28 want interface{} 29 } 30 type Human struct { 31 ID int 32 Nick string 33 Pwd string 34 Ms bool 35 } 36 37 cases := []struct { 38 request string 39 valueGp []testItem 40 }{ 41 {"/?p=str", []testItem{{"p", interface{}(""), interface{}("str")}}}, 42 43 {"/?p=", []testItem{{"p", "", ""}}}, 44 {"/?p=str", []testItem{{"p", "", "str"}}}, 45 46 {"/?p=123", []testItem{{"p", 0, 123}}}, 47 {"/?p=123", []testItem{{"p", uint(0), uint(123)}}}, 48 49 {"/?p=1.0", []testItem{{"p", 0.0, 1.0}}}, 50 {"/?p=1", []testItem{{"p", false, true}}}, 51 52 {"/?p=true", []testItem{{"p", false, true}}}, 53 {"/?p=ON", []testItem{{"p", false, true}}}, 54 {"/?p=on", []testItem{{"p", false, true}}}, 55 {"/?p=1", []testItem{{"p", false, true}}}, 56 {"/?p=2", []testItem{{"p", false, false}}}, 57 {"/?p=false", []testItem{{"p", false, false}}}, 58 59 {"/?p[a]=1&p[b]=2&p[c]=3", []testItem{{"p", map[string]int{}, map[string]int{"a": 1, "b": 2, "c": 3}}}}, 60 {"/?p[a]=v1&p[b]=v2&p[c]=v3", []testItem{{"p", map[string]string{}, map[string]string{"a": "v1", "b": "v2", "c": "v3"}}}}, 61 62 {"/?p[]=8&p[]=9&p[]=10", []testItem{{"p", []int{}, []int{8, 9, 10}}}}, 63 {"/?p[0]=8&p[1]=9&p[2]=10", []testItem{{"p", []int{}, []int{8, 9, 10}}}}, 64 {"/?p[0]=8&p[1]=9&p[2]=10&p[5]=14", []testItem{{"p", []int{}, []int{8, 9, 10, 0, 0, 14}}}}, 65 {"/?p[0]=8.0&p[1]=9.0&p[2]=10.0", []testItem{{"p", []float64{}, []float64{8.0, 9.0, 10.0}}}}, 66 67 {"/?p[]=10&p[]=9&p[]=8", []testItem{{"p", []string{}, []string{"10", "9", "8"}}}}, 68 {"/?p[0]=8&p[1]=9&p[2]=10", []testItem{{"p", []string{}, []string{"8", "9", "10"}}}}, 69 70 {"/?p[0]=true&p[1]=false&p[2]=true&p[5]=1&p[6]=ON&p[7]=other", []testItem{{"p", []bool{}, []bool{true, false, true, false, false, true, true, false}}}}, 71 72 {"/?human.Nick=astaxie", []testItem{{"human", Human{}, Human{Nick: "astaxie"}}}}, 73 {"/?human.ID=888&human.Nick=astaxie&human.Ms=true&human[Pwd]=pass", []testItem{{"human", Human{}, Human{ID: 888, Nick: "astaxie", Ms: true, Pwd: "pass"}}}}, 74 {"/?human[0].ID=888&human[0].Nick=astaxie&human[0].Ms=true&human[0][Pwd]=pass01&human[1].ID=999&human[1].Nick=ysqi&human[1].Ms=On&human[1].Pwd=pass02", 75 []testItem{{"human", []Human{}, []Human{ 76 {ID: 888, Nick: "astaxie", Ms: true, Pwd: "pass01"}, 77 {ID: 999, Nick: "ysqi", Ms: true, Pwd: "pass02"}, 78 }}}}, 79 80 { 81 "/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&human.Nick=astaxie", 82 []testItem{ 83 {"id", 0, 123}, 84 {"isok", false, true}, 85 {"ft", 0.0, 1.2}, 86 {"ol", []int{}, []int{1, 2}}, 87 {"ul", []string{}, []string{"str", "array"}}, 88 {"human", Human{}, Human{Nick: "astaxie"}}, 89 }, 90 }, 91 } 92 for _, c := range cases { 93 r, _ := http.NewRequest("GET", c.request, nil) 94 beegoInput := NewInput() 95 beegoInput.Context = NewContext() 96 beegoInput.Context.Reset(httptest.NewRecorder(), r) 97 98 for _, item := range c.valueGp { 99 got := item.empty 100 err := beegoInput.Bind(&got, item.field) 101 if err != nil { 102 t.Fatal(err) 103 } 104 if !reflect.DeepEqual(got, item.want) { 105 t.Fatalf("Bind %q error,should be:\n%#v \ngot:\n%#v", item.field, item.want, got) 106 } 107 } 108 109 } 110 } 111 112 func TestSubDomain(t *testing.T) { 113 r, _ := http.NewRequest("GET", "http://www.example.com/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie", nil) 114 beegoInput := NewInput() 115 beegoInput.Context = NewContext() 116 beegoInput.Context.Reset(httptest.NewRecorder(), r) 117 118 subdomain := beegoInput.SubDomains() 119 if subdomain != "www" { 120 t.Fatal("Subdomain parse error, got" + subdomain) 121 } 122 123 r, _ = http.NewRequest("GET", "http://localhost/", nil) 124 beegoInput.Context.Request = r 125 if beegoInput.SubDomains() != "" { 126 t.Fatal("Subdomain parse error, should be empty, got " + beegoInput.SubDomains()) 127 } 128 129 r, _ = http.NewRequest("GET", "http://aa.bb.example.com/", nil) 130 beegoInput.Context.Request = r 131 if beegoInput.SubDomains() != "aa.bb" { 132 t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains()) 133 } 134 135 /* TODO Fix this 136 r, _ = http.NewRequest("GET", "http://127.0.0.1/", nil) 137 beegoInput.Context.Request = r 138 if beegoInput.SubDomains() != "" { 139 t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains()) 140 } 141 */ 142 143 r, _ = http.NewRequest("GET", "http://example.com/", nil) 144 beegoInput.Context.Request = r 145 if beegoInput.SubDomains() != "" { 146 t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains()) 147 } 148 149 r, _ = http.NewRequest("GET", "http://aa.bb.cc.dd.example.com/", nil) 150 beegoInput.Context.Request = r 151 if beegoInput.SubDomains() != "aa.bb.cc.dd" { 152 t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains()) 153 } 154 } 155 156 func TestParams(t *testing.T) { 157 inp := NewInput() 158 159 inp.SetParam("p1", "val1_ver1") 160 inp.SetParam("p2", "val2_ver1") 161 inp.SetParam("p3", "val3_ver1") 162 if l := inp.ParamsLen(); l != 3 { 163 t.Fatalf("Input.ParamsLen wrong value: %d, expected %d", l, 3) 164 } 165 166 if val := inp.Param("p1"); val != "val1_ver1" { 167 t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver1") 168 } 169 if val := inp.Param("p3"); val != "val3_ver1" { 170 t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val3_ver1") 171 } 172 vals := inp.Params() 173 expected := map[string]string{ 174 "p1": "val1_ver1", 175 "p2": "val2_ver1", 176 "p3": "val3_ver1", 177 } 178 if !reflect.DeepEqual(vals, expected) { 179 t.Fatalf("Input.Params wrong value: %s, expected %s", vals, expected) 180 } 181 182 // overwriting existing params 183 inp.SetParam("p1", "val1_ver2") 184 inp.SetParam("p2", "val2_ver2") 185 expected = map[string]string{ 186 "p1": "val1_ver2", 187 "p2": "val2_ver2", 188 "p3": "val3_ver1", 189 } 190 vals = inp.Params() 191 if !reflect.DeepEqual(vals, expected) { 192 t.Fatalf("Input.Params wrong value: %s, expected %s", vals, expected) 193 } 194 195 if l := inp.ParamsLen(); l != 3 { 196 t.Fatalf("Input.ParamsLen wrong value: %d, expected %d", l, 3) 197 } 198 199 if val := inp.Param("p1"); val != "val1_ver2" { 200 t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver2") 201 } 202 203 if val := inp.Param("p2"); val != "val2_ver2" { 204 t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver2") 205 } 206 207 } 208 func BenchmarkQuery(b *testing.B) { 209 beegoInput := NewInput() 210 beegoInput.Context = NewContext() 211 beegoInput.Context.Request, _ = http.NewRequest("POST", "http://www.example.com/?q=foo", nil) 212 b.RunParallel(func(pb *testing.PB) { 213 for pb.Next() { 214 beegoInput.Query("q") 215 } 216 }) 217 }