ghttp.Client updates; add more unit test cases for web server

This commit is contained in:
John 2018-12-31 00:50:55 +08:00
parent 24ce4d098e
commit 1966b40d01
10 changed files with 242 additions and 401 deletions

View File

@ -8,182 +8,6 @@
package ghttp
import (
"time"
"bytes"
"strings"
"net/http"
"mime/multipart"
"os"
"io"
"gitee.com/johng/gf/g/os/gfile"
"errors"
"fmt"
)
// http客户端
type Client struct {
http.Client // 底层http client对象
header map[string]string // HEADER信息Map
authUser string // HTTP基本权限设置名称
authPass string // HTTP基本权限设置密码
}
// http客户端对象指针
func NewClient() (*Client) {
return &Client{
Client : http.Client {
Transport: &http.Transport {
DisableKeepAlives: true,
},
},
header : make(map[string]string),
}
}
// 设置HTTP Header
func (c *Client) SetHeader(key, value string) {
c.header[key] = value
}
// 设置请求过期时间
func (c *Client) SetTimeOut(t time.Duration) {
c.Timeout = t
}
// 设置HTTP访问账号密码
func (c *Client) SetBasicAuth(user, pass string) {
c.authUser = user
c.authPass = pass
}
// GET请求
func (c *Client) Get(url string) (*ClientResponse, error) {
return c.DoRequest("GET", url, []byte(""))
}
// PUT请求
func (c *Client) Put(url, data string) (*ClientResponse, error) {
return c.DoRequest("PUT", url, []byte(data))
}
// POST请求提交数据默认使用表单方式提交数据(绝大部分场景下也是如此)。
// 如果服务端对Content-Type有要求可使用Client对象进行请求单独设置相关属性。
// 支持文件上传需要字段格式为FieldName=@file:
func (c *Client) Post(url, data string) (*ClientResponse, error) {
var req *http.Request
if strings.Contains(data, "@file:") {
buffer := new(bytes.Buffer)
writer := multipart.NewWriter(buffer)
for _, item := range strings.Split(data, "&") {
array := strings.Split(item, "=")
if len(array[1]) > 6 && strings.Compare(array[1][0:6], "@file:") == 0 {
path := array[1][6:]
if !gfile.Exists(path) {
return nil, errors.New(fmt.Sprintf(`"%s" does not exist`, path))
}
if file, err := writer.CreateFormFile(array[0], path); err == nil {
if f, err := os.Open(path); err == nil {
defer f.Close()
if _, err = io.Copy(file, f); err != nil {
return nil, err
}
} else {
return nil, err
}
} else {
return nil, err
}
} else {
writer.WriteField(array[0], array[1])
}
}
writer.Close()
if r, err := http.NewRequest("POST", url, buffer); err != nil {
return nil, err
} else {
req = r
req.Header.Set("Content-Type", writer.FormDataContentType())
}
} else {
if r, err := http.NewRequest("POST", url, bytes.NewReader([]byte(data))); err != nil {
return nil, err
} else {
req = r
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
}
// 自定义header
if len(c.header) > 0 {
for k, v := range c.header {
req.Header.Set(k, v)
}
}
// HTTP账号密码
if len(c.authUser) > 0 {
req.SetBasicAuth(c.authUser, c.authPass)
}
// 执行请求
resp, err := c.Do(req)
if err != nil {
return nil, err
}
r := &ClientResponse{}
r.Response = *resp
return r, nil
}
// DELETE请求
func (c *Client) Delete(url, data string) (*ClientResponse, error) {
return c.DoRequest("DELETE", url, []byte(data))
}
func (c *Client) Head(url, data string) (*ClientResponse, error) {
return c.DoRequest("HEAD", url, []byte(data))
}
func (c *Client) Patch(url, data string) (*ClientResponse, error) {
return c.DoRequest("PATCH", url, []byte(data))
}
func (c *Client) Connect(url, data string) (*ClientResponse, error) {
return c.DoRequest("CONNECT", url, []byte(data))
}
func (c *Client) Options(url, data string) (*ClientResponse, error) {
return c.DoRequest("OPTIONS", url, []byte(data))
}
func (c *Client) Trace(url, data string) (*ClientResponse, error) {
return c.DoRequest("TRACE", url, []byte(data))
}
// 请求并返回response对象该方法支持二进制提交数据
func (c *Client) DoRequest(method, url string, data []byte) (*ClientResponse, error) {
if strings.Compare("POST", strings.ToUpper(method)) == 0 {
return c.Post(url, string(data))
}
req, err := http.NewRequest(strings.ToUpper(method), url, bytes.NewReader(data))
if err != nil {
return nil, err
}
// 自定义header
if len(c.header) > 0 {
for k, v := range c.header {
req.Header.Set(k, v)
}
}
// 执行请求
resp, err := c.Do(req)
if err != nil {
return nil, err
}
r := &ClientResponse{}
r.Response = *resp
return r, nil
}
func Get(url string) (*ClientResponse, error) {
return DoRequest("GET", url, []byte(""))
}
@ -267,15 +91,6 @@ func TraceContent(url string, data...string) string {
// 请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
func RequestContent(method string, url string, data...string) string {
content := ""
if len(data) > 0 {
content = data[0]
}
response, err := NewClient().DoRequest(method, url, []byte(content))
if err != nil {
return ""
}
defer response.Close()
return string(response.ReadAll())
return NewClient().DoRequestContent(method, url, data...)
}

View File

@ -25,6 +25,7 @@ import (
type Client struct {
http.Client // 底层http client对象
header map[string]string // HEADER信息Map
prefix string // 设置请求的URL前缀
authUser string // HTTP基本权限设置名称
authPass string // HTTP基本权限设置密码
}
@ -46,6 +47,11 @@ func (c *Client) SetHeader(key, value string) {
c.header[key] = value
}
// 设置请求的URL前缀
func (c *Client) SetPrefix(prefix string) {
c.prefix = prefix
}
// 设置请求过期时间
func (c *Client) SetTimeOut(t time.Duration) {
c.Timeout = t
@ -71,7 +77,10 @@ func (c *Client) Put(url, data string) (*ClientResponse, error) {
// 如果服务端对Content-Type有要求可使用Client对象进行请求单独设置相关属性。
// 支持文件上传需要字段格式为FieldName=@file:
func (c *Client) Post(url, data string) (*ClientResponse, error) {
var req *http.Request
if len(c.prefix) > 0 {
url = c.prefix + url
}
req := (*http.Request)(nil)
if strings.Contains(data, "@file:") {
buffer := new(bytes.Buffer)
writer := multipart.NewWriter(buffer)
@ -158,11 +167,68 @@ func (c *Client) Trace(url, data string) (*ClientResponse, error) {
return c.DoRequest("TRACE", url, []byte(data))
}
// GET请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
func (c *Client) GetContent(url string, data...string) string {
return c.DoRequestContent("GET", url, data...)
}
// PUT请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
func (c *Client) PutContent(url string, data...string) string {
return c.DoRequestContent("PUT", url, data...)
}
// POST请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
func (c *Client) PostContent(url string, data...string) string {
return c.DoRequestContent("POST", url, data...)
}
// DELETE请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
func (c *Client) DeleteContent(url string, data...string) string {
return c.DoRequestContent("DELETE", url, data...)
}
func (c *Client) HeadContent(url string, data...string) string {
return c.DoRequestContent("HEAD", url, data...)
}
func (c *Client) PatchContent(url string, data...string) string {
return c.DoRequestContent("PATCH", url, data...)
}
func (c *Client) ConnectContent(url string, data...string) string {
return c.DoRequestContent("CONNECT", url, data...)
}
func (c *Client) OptionsContent(url string, data...string) string {
return c.DoRequestContent("OPTIONS", url, data...)
}
func (c *Client) TraceContent(url string, data...string) string {
return c.DoRequestContent("TRACE", url, data...)
}
// 请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
func (c *Client) DoRequestContent(method string, url string, data...string) string {
content := ""
if len(data) > 0 {
content = data[0]
}
response, err := c.DoRequest(method, url, []byte(content))
if err != nil {
return ""
}
defer response.Close()
return string(response.ReadAll())
}
// 请求并返回response对象该方法支持二进制提交数据
func (c *Client) DoRequest(method, url string, data []byte) (*ClientResponse, error) {
if strings.Compare("POST", strings.ToUpper(method)) == 0 {
if strings.EqualFold("POST", method) {
return c.Post(url, string(data))
}
if len(c.prefix) > 0 {
url = c.prefix + url
}
req, err := http.NewRequest(strings.ToUpper(method), url, bytes.NewReader(data))
if err != nil {
return nil, err
@ -184,98 +250,5 @@ func (c *Client) DoRequest(method, url string, data []byte) (*ClientResponse, er
}
func Get(url string) (*ClientResponse, error) {
return DoRequest("GET", url, []byte(""))
}
func Put(url, data string) (*ClientResponse, error) {
return DoRequest("PUT", url, []byte(data))
}
func Post(url, data string) (*ClientResponse, error) {
return DoRequest("POST", url, []byte(data))
}
func Delete(url, data string) (*ClientResponse, error) {
return DoRequest("DELETE", url, []byte(data))
}
func Head(url, data string) (*ClientResponse, error) {
return DoRequest("HEAD", url, []byte(data))
}
func Patch(url, data string) (*ClientResponse, error) {
return DoRequest("PATCH", url, []byte(data))
}
func Connect(url, data string) (*ClientResponse, error) {
return DoRequest("CONNECT", url, []byte(data))
}
func Options(url, data string) (*ClientResponse, error) {
return DoRequest("OPTIONS", url, []byte(data))
}
func Trace(url, data string) (*ClientResponse, error) {
return DoRequest("TRACE", url, []byte(data))
}
// 该方法支持二进制提交数据
func DoRequest(method, url string, data []byte) (*ClientResponse, error) {
return NewClient().DoRequest(method, url, data)
}
// GET请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
func GetContent(url string, data...string) string {
return RequestContent("GET", url, data...)
}
// PUT请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
func PutContent(url string, data...string) string {
return RequestContent("PUT", url, data...)
}
// POST请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
func PostContent(url string, data...string) string {
return RequestContent("POST", url, data...)
}
// DELETE请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
func DeleteContent(url string, data...string) string {
return RequestContent("DELETE", url, data...)
}
func HeadContent(url string, data...string) string {
return RequestContent("HEAD", url, data...)
}
func PatchContent(url string, data...string) string {
return RequestContent("PATCH", url, data...)
}
func ConnectContent(url string, data...string) string {
return RequestContent("CONNECT", url, data...)
}
func OptionsContent(url string, data...string) string {
return RequestContent("OPTIONS", url, data...)
}
func TraceContent(url string, data...string) string {
return RequestContent("TRACE", url, data...)
}
// 请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
func RequestContent(method string, url string, data...string) string {
content := ""
if len(data) > 0 {
content = data[0]
}
response, err := NewClient().DoRequest(method, url, []byte(content))
if err != nil {
return ""
}
defer response.Close()
return string(response.ReadAll())
}

View File

@ -8,12 +8,21 @@ package ghttp
import (
"gitee.com/johng/gf/g/util/gconv"
"strings"
)
// 初始化GET请求参数
func (r *Request) initGet() {
if !r.parsedGet {
r.queryVars = r.URL.Query()
if strings.EqualFold(r.Method, "GET") {
if raw := r.GetRaw(); len(raw) > 0 {
for _, item := range strings.Split(string(raw), "&") {
array := strings.Split(item, "=")
r.queryVars[array[0]] = append(r.queryVars[array[0]], array[1])
}
}
}
r.parsedGet = true
}
}

View File

@ -4,7 +4,7 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
// 基本路由功能以及优先级测试
package ghttp_test
import (
@ -16,7 +16,7 @@ import (
"time"
)
// 基本路由功能以及优先级测试
func Test_Router_Basic(t *testing.T) {
s := g.Server(gtime.Nanosecond())
s.BindHandler("/:name", func(r *ghttp.Request){
@ -41,9 +41,12 @@ func Test_Router_Basic(t *testing.T) {
// 等待启动完成
time.Sleep(time.Second)
gtest.Case(func() {
gtest.Assert(ghttp.GetContent("http://127.0.0.1:8199/john"), "")
gtest.Assert(ghttp.GetContent("http://127.0.0.1:8199/john/update"), "john")
gtest.Assert(ghttp.GetContent("http://127.0.0.1:8199/john/edit"), "edit")
gtest.Assert(ghttp.GetContent("http://127.0.0.1:8199/user/list/100.html"), "100")
client := ghttp.NewClient()
client.SetPrefix("http://127.0.0.1:8199")
gtest.Assert(client.GetContent("/john"), "")
gtest.Assert(client.GetContent("/john/update"), "john")
gtest.Assert(client.GetContent("/john/edit"), "edit")
gtest.Assert(client.GetContent("/user/list/100.html"), "100")
})
}

View File

@ -5,7 +5,6 @@
// You can obtain one at https://gitee.com/johng/gf.
// 分组路由测试
package ghttp_test
import (
@ -65,17 +64,20 @@ func Test_Router_Group1(t *testing.T) {
defer s.Shutdown()
time.Sleep(time.Second)
gtest.Case(func() {
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/handler"), "Handler")
client := ghttp.NewClient()
client.SetPrefix("http://127.0.0.1:8199")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/ctl/my-show"), "Controller Show")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/ctl/post"), "Controller REST Post")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/ctl/show"), "Controller Show")
gtest.Assert(ghttp.PostContent("http://127.0.0.1:8199/api/ctl/rest"), "Controller REST Post")
gtest.Assert(client.GetContent ("/api/handler"), "Handler")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/obj/delete"), "Object REST Delete")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/obj/my-show"), "Object Show")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/obj/show"), "Object Show")
gtest.Assert(ghttp.DeleteContent("http://127.0.0.1:8199/api/obj/rest"), "Object REST Delete")
gtest.Assert(client.GetContent ("/api/ctl/my-show"), "Controller Show")
gtest.Assert(client.GetContent ("/api/ctl/post"), "Controller REST Post")
gtest.Assert(client.GetContent ("/api/ctl/show"), "Controller Show")
gtest.Assert(client.PostContent("/api/ctl/rest"), "Controller REST Post")
gtest.Assert(client.GetContent ("/api/obj/delete"), "Object REST Delete")
gtest.Assert(client.GetContent ("/api/obj/my-show"), "Object Show")
gtest.Assert(client.GetContent ("/api/obj/show"), "Object Show")
gtest.Assert(client.DeleteContent("/api/obj/rest"), "Object REST Delete")
})
}
@ -100,16 +102,19 @@ func Test_Router_Group2(t *testing.T) {
defer s.Shutdown()
time.Sleep(time.Second)
gtest.Case(func() {
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/handler"), "Handler")
client := ghttp.NewClient()
client.SetPrefix("http://127.0.0.1:8199")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/ctl/my-show"), "Controller Show")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/ctl/post"), "Controller REST Post")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/ctl/show"), "Controller Show")
gtest.Assert(ghttp.PostContent("http://127.0.0.1:8199/api/ctl/rest"), "Controller REST Post")
gtest.Assert(client.GetContent ("/api/handler"), "Handler")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/obj/delete"), "Object REST Delete")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/obj/my-show"), "Object Show")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/obj/show"), "Object Show")
gtest.Assert(ghttp.DeleteContent("http://127.0.0.1:8199/api/obj/rest"), "Object REST Delete")
gtest.Assert(client.GetContent ("/api/ctl/my-show"), "Controller Show")
gtest.Assert(client.GetContent ("/api/ctl/post"), "Controller REST Post")
gtest.Assert(client.GetContent ("/api/ctl/show"), "Controller Show")
gtest.Assert(client.PostContent("/api/ctl/rest"), "Controller REST Post")
gtest.Assert(client.GetContent ("/api/obj/delete"), "Object REST Delete")
gtest.Assert(client.GetContent ("/api/obj/my-show"), "Object Show")
gtest.Assert(client.GetContent ("/api/obj/show"), "Object Show")
gtest.Assert(client.DeleteContent("/api/obj/rest"), "Object REST Delete")
})
}

View File

@ -4,12 +4,11 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
// 分组路由测试
// 请求参数测试
package ghttp_test
import (
"gitee.com/johng/gf/g"
"gitee.com/johng/gf/g/frame/gmvc"
"gitee.com/johng/gf/g/net/ghttp"
"gitee.com/johng/gf/g/os/gtime"
"gitee.com/johng/gf/g/util/gtest"
@ -17,98 +16,131 @@ import (
"time"
)
// 执行对象
type Object struct {}
func (o *Object) Show(r *ghttp.Request) {
r.Response.Write("Object Show")
}
func (o *Object) Delete(r *ghttp.Request) {
r.Response.Write("Object REST Delete")
}
// 控制器
type Controller struct {
gmvc.Controller
}
func (c *Controller) Show() {
c.Response.Write("Controller Show")
}
func (c *Controller) Post() {
c.Response.Write("Controller REST Post")
}
func Handler(r *ghttp.Request) {
r.Response.Write("Handler")
}
func Test_Router_Group1(t *testing.T) {
s := g.Server(gtime.Nanosecond())
obj := new(Object)
ctl := new(Controller)
// 分组路由方法注册
g := s.Group("/api")
g.ALL ("/handler", Handler)
g.ALL ("/ctl", ctl)
g.GET ("/ctl/my-show", ctl, "Show")
g.REST("/ctl/rest", ctl)
g.ALL ("/obj", obj)
g.GET ("/obj/my-show", obj, "Show")
g.REST("/obj/rest", obj)
s.SetPort(8199)
s.SetDumpRouteMap(false)
go s.Run()
defer s.Shutdown()
time.Sleep(time.Second)
gtest.Case(func() {
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/handler"), "Handler")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/ctl/my-show"), "Controller Show")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/ctl/post"), "Controller REST Post")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/ctl/show"), "Controller Show")
gtest.Assert(ghttp.PostContent("http://127.0.0.1:8199/api/ctl/rest"), "Controller REST Post")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/obj/delete"), "Object REST Delete")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/obj/my-show"), "Object Show")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/obj/show"), "Object Show")
gtest.Assert(ghttp.DeleteContent("http://127.0.0.1:8199/api/obj/rest"), "Object REST Delete")
func Test_Params(t *testing.T) {
type User struct {
Id int
Name string
Pass1 string `params:"password1"`
Pass2 string `params:"password2"`
}
s := g.Server(gtime.Nanosecond())
s.BindHandler("/get", func(r *ghttp.Request){
if r.GetQuery("slice") != nil {
r.Response.Write(r.GetQuery("slice"))
}
if r.GetQuery("bool") != nil {
r.Response.Write(r.GetQueryBool("bool"))
}
if r.GetQuery("float32") != nil {
r.Response.Write(r.GetQueryFloat32("float32"))
}
if r.GetQuery("float64") != nil {
r.Response.Write(r.GetQueryFloat64("float64"))
}
if r.GetQuery("int") != nil {
r.Response.Write(r.GetQueryInt("int"))
}
if r.GetQuery("uint") != nil {
r.Response.Write(r.GetQueryUint("uint"))
}
if r.GetQuery("string") != nil {
r.Response.Write(r.GetQueryString("string"))
}
})
}
func Test_Router_Group2(t *testing.T) {
s := g.Server(gtime.Nanosecond())
obj := new(Object)
ctl := new(Controller)
// 分组路由批量注册
s.Group("/api").Bind("/api", []ghttp.GroupItem{
{"ALL", "/handler", Handler},
{"ALL", "/ctl", ctl},
{"GET", "/ctl/my-show", ctl, "Show"},
{"REST", "/ctl/rest", ctl},
{"ALL", "/obj", obj},
{"GET", "/obj/my-show", obj, "Show"},
{"REST", "/obj/rest", obj},
s.BindHandler("/post", func(r *ghttp.Request){
if r.GetPost("slice") != nil {
r.Response.Write(r.GetPost("slice"))
}
if r.GetPost("bool") != nil {
r.Response.Write(r.GetPostBool("bool"))
}
if r.GetPost("float32") != nil {
r.Response.Write(r.GetPostFloat32("float32"))
}
if r.GetPost("float64") != nil {
r.Response.Write(r.GetPostFloat64("float64"))
}
if r.GetPost("int") != nil {
r.Response.Write(r.GetPostInt("int"))
}
if r.GetPost("uint") != nil {
r.Response.Write(r.GetPostUint("uint"))
}
if r.GetPost("string") != nil {
r.Response.Write(r.GetPostString("string"))
}
})
s.BindHandler("/map", func(r *ghttp.Request){
if m := r.GetQueryMap(); len(m) > 0 {
r.Response.Write(m["name"])
}
if m := r.GetPostMap(); len(m) > 0 {
r.Response.Write(m["name"])
}
})
s.BindHandler("/raw", func(r *ghttp.Request){
r.Response.Write(r.GetRaw())
})
s.BindHandler("/json", func(r *ghttp.Request){
r.Response.Write(r.GetJson().Get("name"))
})
s.BindHandler("/struct", func(r *ghttp.Request){
if m := r.GetQueryMap(); len(m) > 0 {
user := new(User)
r.GetQueryToStruct(user)
r.Response.Write(user.Id, user.Name, user.Pass1, user.Pass2)
}
if m := r.GetPostMap(); len(m) > 0 {
user := new(User)
r.GetPostToStruct(user)
r.Response.Write(user.Id, user.Name, user.Pass1, user.Pass2)
}
})
s.SetPort(8199)
s.SetDumpRouteMap(false)
go s.Run()
defer s.Shutdown()
// 等待启动完成
time.Sleep(time.Second)
gtest.Case(func() {
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/handler"), "Handler")
client := ghttp.NewClient()
client.SetPrefix("http://127.0.0.1:8199")
// GET
gtest.Assert(client.GetContent("/get", "slice=1&slice=2"), `["1","2"]`)
gtest.Assert(client.GetContent("/get", "bool=1"), `true`)
gtest.Assert(client.GetContent("/get", "bool=0"), `false`)
gtest.Assert(client.GetContent("/get", "float32=0.11"), `0.11`)
gtest.Assert(client.GetContent("/get", "float64=0.22"), `0.22`)
gtest.Assert(client.GetContent("/get", "int=-10000"), `-10000`)
gtest.Assert(client.GetContent("/get", "int=10000"), `10000`)
gtest.Assert(client.GetContent("/get", "uint=-10000"), `10000`)
gtest.Assert(client.GetContent("/get", "uint=9"), `9`)
gtest.Assert(client.GetContent("/get", "string=key"), `key`)
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/ctl/my-show"), "Controller Show")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/ctl/post"), "Controller REST Post")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/ctl/show"), "Controller Show")
gtest.Assert(ghttp.PostContent("http://127.0.0.1:8199/api/ctl/rest"), "Controller REST Post")
// POST
gtest.Assert(client.PostContent("/post", "slice=1&slice=2"), `["1","2"]`)
gtest.Assert(client.PostContent("/post", "bool=1"), `true`)
gtest.Assert(client.PostContent("/post", "bool=0"), `false`)
gtest.Assert(client.PostContent("/post", "float32=0.11"), `0.11`)
gtest.Assert(client.PostContent("/post", "float64=0.22"), `0.22`)
gtest.Assert(client.PostContent("/post", "int=-10000"), `-10000`)
gtest.Assert(client.PostContent("/post", "int=10000"), `10000`)
gtest.Assert(client.PostContent("/post", "uint=-10000"), `10000`)
gtest.Assert(client.PostContent("/post", "uint=9"), `9`)
gtest.Assert(client.PostContent("/post", "string=key"), `key`)
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/obj/delete"), "Object REST Delete")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/obj/my-show"), "Object Show")
gtest.Assert(ghttp.GetContent ("http://127.0.0.1:8199/api/obj/show"), "Object Show")
gtest.Assert(ghttp.DeleteContent("http://127.0.0.1:8199/api/obj/rest"), "Object REST Delete")
// Map
gtest.Assert(client.GetContent ("/map", "id=1&name=john"), `john`)
gtest.Assert(client.PostContent("/map", "id=1&name=john"), `john`)
// Raw
gtest.Assert(client.PutContent("/raw", "id=1&name=john"), `id=1&name=john`)
// Json
gtest.Assert(client.PostContent("/json", `{"id":1, "name":"john"}`), `john`)
// Struct
gtest.Assert(client.GetContent("/struct", `id=1&name=john&password1=123&password2=456`), `1john123456`)
gtest.Assert(client.PostContent("/struct", `id=1&name=john&password1=123&password2=456`), `1john123456`)
})
}

View File

@ -220,7 +220,11 @@ func Uint(i interface{}) uint {
}
return 0
default:
return uint(Float64(value))
v := Float64(value)
if v < 0 {
v = -v
}
return uint(v)
}
}

View File

@ -175,4 +175,4 @@ func Nl2Br(str string) string {
str = Replace(str, "\n\r", "\n")
str = Replace(str, "\n", "<br />")
return str
}
}

View File

@ -7,14 +7,10 @@ import (
func main() {
//path := "D:\\Workspace\\Go\\GOPATH\\src\\gitee.com\\johng\\gf\\geg\\other\\test.go"
path := "/Users/john/Temp/"
path := "/Users/john/Temp/test"
_, err := gfsnotify.Add(path, func(event *gfsnotify.Event) {
glog.Println(event)
}, true)
// 移除对该path的监听
//gfsnotify.Remove(path)
if err != nil {
glog.Fatal(err)
} else {

View File

@ -1,8 +1,12 @@
package main
import "fmt"
import (
"fmt"
"gitee.com/johng/gf/g/os/gfile"
)
func main(){
fmt.Println(fmt.Sprintf(`@every %ds`, 12345))
for i := 0; i < 100; i++ {
gfile.Create(fmt.Sprintf(`/Users/john/Documents/test/%d`, i))
}
}