input_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. package context
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestBind(t *testing.T) {
  22. type testItem struct {
  23. field string
  24. empty interface{}
  25. want interface{}
  26. }
  27. type Human struct {
  28. ID int
  29. Nick string
  30. Pwd string
  31. Ms bool
  32. }
  33. cases := []struct {
  34. request string
  35. valueGp []testItem
  36. }{
  37. {"/?p=str", []testItem{{"p", interface{}(""), interface{}("str")}}},
  38. {"/?p=", []testItem{{"p", "", ""}}},
  39. {"/?p=str", []testItem{{"p", "", "str"}}},
  40. {"/?p=123", []testItem{{"p", 0, 123}}},
  41. {"/?p=123", []testItem{{"p", uint(0), uint(123)}}},
  42. {"/?p=1.0", []testItem{{"p", 0.0, 1.0}}},
  43. {"/?p=1", []testItem{{"p", false, true}}},
  44. {"/?p=true", []testItem{{"p", false, true}}},
  45. {"/?p=ON", []testItem{{"p", false, true}}},
  46. {"/?p=on", []testItem{{"p", false, true}}},
  47. {"/?p=1", []testItem{{"p", false, true}}},
  48. {"/?p=2", []testItem{{"p", false, false}}},
  49. {"/?p=false", []testItem{{"p", false, false}}},
  50. {"/?p[a]=1&p[b]=2&p[c]=3", []testItem{{"p", map[string]int{}, map[string]int{"a": 1, "b": 2, "c": 3}}}},
  51. {"/?p[a]=v1&p[b]=v2&p[c]=v3", []testItem{{"p", map[string]string{}, map[string]string{"a": "v1", "b": "v2", "c": "v3"}}}},
  52. {"/?p[]=8&p[]=9&p[]=10", []testItem{{"p", []int{}, []int{8, 9, 10}}}},
  53. {"/?p[0]=8&p[1]=9&p[2]=10", []testItem{{"p", []int{}, []int{8, 9, 10}}}},
  54. {"/?p[0]=8&p[1]=9&p[2]=10&p[5]=14", []testItem{{"p", []int{}, []int{8, 9, 10, 0, 0, 14}}}},
  55. {"/?p[0]=8.0&p[1]=9.0&p[2]=10.0", []testItem{{"p", []float64{}, []float64{8.0, 9.0, 10.0}}}},
  56. {"/?p[]=10&p[]=9&p[]=8", []testItem{{"p", []string{}, []string{"10", "9", "8"}}}},
  57. {"/?p[0]=8&p[1]=9&p[2]=10", []testItem{{"p", []string{}, []string{"8", "9", "10"}}}},
  58. {"/?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}}}},
  59. {"/?human.Nick=astaxie", []testItem{{"human", Human{}, Human{Nick: "astaxie"}}}},
  60. {"/?human.ID=888&human.Nick=astaxie&human.Ms=true&human[Pwd]=pass", []testItem{{"human", Human{}, Human{ID: 888, Nick: "astaxie", Ms: true, Pwd: "pass"}}}},
  61. {"/?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",
  62. []testItem{{"human", []Human{}, []Human{
  63. Human{ID: 888, Nick: "astaxie", Ms: true, Pwd: "pass01"},
  64. Human{ID: 999, Nick: "ysqi", Ms: true, Pwd: "pass02"},
  65. }}}},
  66. {
  67. "/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&human.Nick=astaxie",
  68. []testItem{
  69. {"id", 0, 123},
  70. {"isok", false, true},
  71. {"ft", 0.0, 1.2},
  72. {"ol", []int{}, []int{1, 2}},
  73. {"ul", []string{}, []string{"str", "array"}},
  74. {"human", Human{}, Human{Nick: "astaxie"}},
  75. },
  76. },
  77. }
  78. for _, c := range cases {
  79. r, _ := http.NewRequest("GET", c.request, nil)
  80. beegoInput := NewInput()
  81. beegoInput.Context = NewContext()
  82. beegoInput.Context.Reset(httptest.NewRecorder(), r)
  83. for _, item := range c.valueGp {
  84. got := item.empty
  85. err := beegoInput.Bind(&got, item.field)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. if !reflect.DeepEqual(got, item.want) {
  90. t.Fatalf("Bind %q error,should be:\n%#v \ngot:\n%#v", item.field, item.want, got)
  91. }
  92. }
  93. }
  94. }
  95. func TestSubDomain(t *testing.T) {
  96. 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)
  97. beegoInput := NewInput()
  98. beegoInput.Context = NewContext()
  99. beegoInput.Context.Reset(httptest.NewRecorder(), r)
  100. subdomain := beegoInput.SubDomains()
  101. if subdomain != "www" {
  102. t.Fatal("Subdomain parse error, got" + subdomain)
  103. }
  104. r, _ = http.NewRequest("GET", "http://localhost/", nil)
  105. beegoInput.Context.Request = r
  106. if beegoInput.SubDomains() != "" {
  107. t.Fatal("Subdomain parse error, should be empty, got " + beegoInput.SubDomains())
  108. }
  109. r, _ = http.NewRequest("GET", "http://aa.bb.example.com/", nil)
  110. beegoInput.Context.Request = r
  111. if beegoInput.SubDomains() != "aa.bb" {
  112. t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
  113. }
  114. /* TODO Fix this
  115. r, _ = http.NewRequest("GET", "http://127.0.0.1/", nil)
  116. beegoInput.Context.Request = r
  117. if beegoInput.SubDomains() != "" {
  118. t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
  119. }
  120. */
  121. r, _ = http.NewRequest("GET", "http://example.com/", nil)
  122. beegoInput.Context.Request = r
  123. if beegoInput.SubDomains() != "" {
  124. t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
  125. }
  126. r, _ = http.NewRequest("GET", "http://aa.bb.cc.dd.example.com/", nil)
  127. beegoInput.Context.Request = r
  128. if beegoInput.SubDomains() != "aa.bb.cc.dd" {
  129. t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
  130. }
  131. }
  132. func TestParams(t *testing.T) {
  133. inp := NewInput()
  134. inp.SetParam("p1", "val1_ver1")
  135. inp.SetParam("p2", "val2_ver1")
  136. inp.SetParam("p3", "val3_ver1")
  137. if l := inp.ParamsLen(); l != 3 {
  138. t.Fatalf("Input.ParamsLen wrong value: %d, expected %d", l, 3)
  139. }
  140. if val := inp.Param("p1"); val != "val1_ver1" {
  141. t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver1")
  142. }
  143. if val := inp.Param("p3"); val != "val3_ver1" {
  144. t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val3_ver1")
  145. }
  146. vals := inp.Params()
  147. expected := map[string]string{
  148. "p1": "val1_ver1",
  149. "p2": "val2_ver1",
  150. "p3": "val3_ver1",
  151. }
  152. if !reflect.DeepEqual(vals, expected) {
  153. t.Fatalf("Input.Params wrong value: %s, expected %s", vals, expected)
  154. }
  155. // overwriting existing params
  156. inp.SetParam("p1", "val1_ver2")
  157. inp.SetParam("p2", "val2_ver2")
  158. expected = map[string]string{
  159. "p1": "val1_ver2",
  160. "p2": "val2_ver2",
  161. "p3": "val3_ver1",
  162. }
  163. vals = inp.Params()
  164. if !reflect.DeepEqual(vals, expected) {
  165. t.Fatalf("Input.Params wrong value: %s, expected %s", vals, expected)
  166. }
  167. if l := inp.ParamsLen(); l != 3 {
  168. t.Fatalf("Input.ParamsLen wrong value: %d, expected %d", l, 3)
  169. }
  170. if val := inp.Param("p1"); val != "val1_ver2" {
  171. t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver2")
  172. }
  173. if val := inp.Param("p2"); val != "val2_ver2" {
  174. t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver2")
  175. }
  176. }