2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
context:add Bind function
// Bind data from request.Form[key] to dest
// like
/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=
astaxie
// var id int beegoInput.Bind(&id, "id") id ==123
// var isok bool beegoInput.Bind(&isok, "isok") id ==true
// var ft float64 beegoInput.Bind(&ft, "ft") ft ==1.2
// ol := make([]int, 0, 2) beegoInput.Bind(&ol, "ol") ol ==[1 2]
// ul := make([]string, 0, 2) beegoInput.Bind(&ul, "ul") ul ==[str
array]
// user struct{Name} beegoInput.Bind(&user, "user") user ==
{Name:"astaxie"}
2014-04-09 16:31:16 +00:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2016-01-08 08:24:59 +00:00
|
|
|
"net/http/httptest"
|
2016-01-27 13:58:50 +00:00
|
|
|
"reflect"
|
context:add Bind function
// Bind data from request.Form[key] to dest
// like
/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=
astaxie
// var id int beegoInput.Bind(&id, "id") id ==123
// var isok bool beegoInput.Bind(&isok, "isok") id ==true
// var ft float64 beegoInput.Bind(&ft, "ft") ft ==1.2
// ol := make([]int, 0, 2) beegoInput.Bind(&ol, "ol") ol ==[1 2]
// ul := make([]string, 0, 2) beegoInput.Bind(&ul, "ul") ul ==[str
array]
// user struct{Name} beegoInput.Bind(&user, "user") user ==
{Name:"astaxie"}
2014-04-09 16:31:16 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
r, _ := http.NewRequest("GET", "/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie", nil)
|
2016-01-08 08:24:59 +00:00
|
|
|
beegoInput := NewInput()
|
|
|
|
beegoInput.Context = NewContext()
|
|
|
|
beegoInput.Context.Reset(httptest.NewRecorder(), r)
|
context:add Bind function
// Bind data from request.Form[key] to dest
// like
/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=
astaxie
// var id int beegoInput.Bind(&id, "id") id ==123
// var isok bool beegoInput.Bind(&isok, "isok") id ==true
// var ft float64 beegoInput.Bind(&ft, "ft") ft ==1.2
// ol := make([]int, 0, 2) beegoInput.Bind(&ol, "ol") ol ==[1 2]
// ul := make([]string, 0, 2) beegoInput.Bind(&ul, "ul") ul ==[str
array]
// user struct{Name} beegoInput.Bind(&user, "user") user ==
{Name:"astaxie"}
2014-04-09 16:31:16 +00:00
|
|
|
beegoInput.ParseFormOrMulitForm(1 << 20)
|
|
|
|
|
|
|
|
var id int
|
|
|
|
err := beegoInput.Bind(&id, "id")
|
|
|
|
if id != 123 || err != nil {
|
|
|
|
t.Fatal("id should has int value")
|
|
|
|
}
|
|
|
|
fmt.Println(id)
|
|
|
|
|
|
|
|
var isok bool
|
|
|
|
err = beegoInput.Bind(&isok, "isok")
|
|
|
|
if !isok || err != nil {
|
|
|
|
t.Fatal("isok should be true")
|
|
|
|
}
|
|
|
|
fmt.Println(isok)
|
|
|
|
|
|
|
|
var float float64
|
|
|
|
err = beegoInput.Bind(&float, "ft")
|
|
|
|
if float != 1.2 || err != nil {
|
|
|
|
t.Fatal("float should be equal to 1.2")
|
|
|
|
}
|
|
|
|
fmt.Println(float)
|
|
|
|
|
|
|
|
ol := make([]int, 0, 2)
|
|
|
|
err = beegoInput.Bind(&ol, "ol")
|
|
|
|
if len(ol) != 2 || err != nil || ol[0] != 1 || ol[1] != 2 {
|
|
|
|
t.Fatal("ol should has two elements")
|
|
|
|
}
|
|
|
|
fmt.Println(ol)
|
|
|
|
|
|
|
|
ul := make([]string, 0, 2)
|
|
|
|
err = beegoInput.Bind(&ul, "ul")
|
|
|
|
if len(ul) != 2 || err != nil || ul[0] != "str" || ul[1] != "array" {
|
|
|
|
t.Fatal("ul should has two elements")
|
|
|
|
}
|
|
|
|
fmt.Println(ul)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
user := User{}
|
|
|
|
err = beegoInput.Bind(&user, "user")
|
|
|
|
if err != nil || user.Name != "astaxie" {
|
|
|
|
t.Fatal("user should has name")
|
|
|
|
}
|
|
|
|
fmt.Println(user)
|
|
|
|
}
|
2014-12-17 04:06:53 +00:00
|
|
|
|
|
|
|
func TestSubDomain(t *testing.T) {
|
|
|
|
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)
|
2016-01-08 08:24:59 +00:00
|
|
|
beegoInput := NewInput()
|
|
|
|
beegoInput.Context = NewContext()
|
|
|
|
beegoInput.Context.Reset(httptest.NewRecorder(), r)
|
2014-12-17 04:06:53 +00:00
|
|
|
|
|
|
|
subdomain := beegoInput.SubDomains()
|
|
|
|
if subdomain != "www" {
|
|
|
|
t.Fatal("Subdomain parse error, got" + subdomain)
|
|
|
|
}
|
|
|
|
|
|
|
|
r, _ = http.NewRequest("GET", "http://localhost/", nil)
|
2016-01-08 08:24:59 +00:00
|
|
|
beegoInput.Context.Request = r
|
2014-12-17 04:06:53 +00:00
|
|
|
if beegoInput.SubDomains() != "" {
|
|
|
|
t.Fatal("Subdomain parse error, should be empty, got " + beegoInput.SubDomains())
|
|
|
|
}
|
|
|
|
|
|
|
|
r, _ = http.NewRequest("GET", "http://aa.bb.example.com/", nil)
|
2016-01-08 08:24:59 +00:00
|
|
|
beegoInput.Context.Request = r
|
2014-12-17 04:06:53 +00:00
|
|
|
if beegoInput.SubDomains() != "aa.bb" {
|
|
|
|
t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO Fix this
|
|
|
|
r, _ = http.NewRequest("GET", "http://127.0.0.1/", nil)
|
2016-03-25 02:56:15 +00:00
|
|
|
beegoInput.Context.Request = r
|
2014-12-17 04:06:53 +00:00
|
|
|
if beegoInput.SubDomains() != "" {
|
|
|
|
t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
r, _ = http.NewRequest("GET", "http://example.com/", nil)
|
2016-01-08 08:24:59 +00:00
|
|
|
beegoInput.Context.Request = r
|
2014-12-17 04:06:53 +00:00
|
|
|
if beegoInput.SubDomains() != "" {
|
|
|
|
t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
|
|
|
|
}
|
|
|
|
|
|
|
|
r, _ = http.NewRequest("GET", "http://aa.bb.cc.dd.example.com/", nil)
|
2016-01-08 08:24:59 +00:00
|
|
|
beegoInput.Context.Request = r
|
2014-12-17 04:06:53 +00:00
|
|
|
if beegoInput.SubDomains() != "aa.bb.cc.dd" {
|
|
|
|
t.Fatal("Subdomain parse error, got " + beegoInput.SubDomains())
|
|
|
|
}
|
|
|
|
}
|
2016-01-27 13:58:50 +00:00
|
|
|
|
|
|
|
func TestParams(t *testing.T) {
|
|
|
|
inp := NewInput()
|
|
|
|
|
|
|
|
inp.SetParam("p1", "val1_ver1")
|
|
|
|
inp.SetParam("p2", "val2_ver1")
|
|
|
|
inp.SetParam("p3", "val3_ver1")
|
|
|
|
if l := inp.ParamsLen(); l != 3 {
|
|
|
|
t.Fatalf("Input.ParamsLen wrong value: %d, expected %d", l, 3)
|
|
|
|
}
|
|
|
|
|
|
|
|
if val := inp.Param("p1"); val != "val1_ver1" {
|
|
|
|
t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver1")
|
|
|
|
}
|
|
|
|
if val := inp.Param("p3"); val != "val3_ver1" {
|
|
|
|
t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val3_ver1")
|
|
|
|
}
|
|
|
|
vals := inp.Params()
|
|
|
|
expected := map[string]string{
|
|
|
|
"p1": "val1_ver1",
|
|
|
|
"p2": "val2_ver1",
|
|
|
|
"p3": "val3_ver1",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(vals, expected) {
|
|
|
|
t.Fatalf("Input.Params wrong value: %s, expected %s", vals, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwriting existing params
|
|
|
|
inp.SetParam("p1", "val1_ver2")
|
|
|
|
inp.SetParam("p2", "val2_ver2")
|
|
|
|
expected = map[string]string{
|
|
|
|
"p1": "val1_ver2",
|
|
|
|
"p2": "val2_ver2",
|
|
|
|
"p3": "val3_ver1",
|
|
|
|
}
|
|
|
|
vals = inp.Params()
|
|
|
|
if !reflect.DeepEqual(vals, expected) {
|
|
|
|
t.Fatalf("Input.Params wrong value: %s, expected %s", vals, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
if l := inp.ParamsLen(); l != 3 {
|
|
|
|
t.Fatalf("Input.ParamsLen wrong value: %d, expected %d", l, 3)
|
|
|
|
}
|
|
|
|
|
|
|
|
if val := inp.Param("p1"); val != "val1_ver2" {
|
|
|
|
t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver2")
|
|
|
|
}
|
|
|
|
|
|
|
|
if val := inp.Param("p2"); val != "val2_ver2" {
|
|
|
|
t.Fatalf("Input.Param wrong value: %s, expected %s", val, "val1_ver2")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|