fix issue with error response status code 200 of web server; ghttp request client updates

This commit is contained in:
John 2019-02-17 20:45:35 +08:00
parent fcc3a1b2f6
commit 989d543a1f
6 changed files with 189 additions and 48 deletions

View File

@ -9,44 +9,44 @@
package ghttp
func Get(url string) (*ClientResponse, error) {
return DoRequest("GET", url, []byte(""))
return DoRequest("GET", url)
}
func Put(url, data string) (*ClientResponse, error) {
return DoRequest("PUT", url, []byte(data))
func Put(url string, data...string) (*ClientResponse, error) {
return DoRequest("PUT", url, data...)
}
func Post(url, data string) (*ClientResponse, error) {
return DoRequest("POST", url, []byte(data))
func Post(url string, data...string) (*ClientResponse, error) {
return DoRequest("POST", url, data...)
}
func Delete(url, data string) (*ClientResponse, error) {
return DoRequest("DELETE", url, []byte(data))
func Delete(url string, data...string) (*ClientResponse, error) {
return DoRequest("DELETE", url, data...)
}
func Head(url, data string) (*ClientResponse, error) {
return DoRequest("HEAD", url, []byte(data))
func Head(url string, data...string) (*ClientResponse, error) {
return DoRequest("HEAD", url, data...)
}
func Patch(url, data string) (*ClientResponse, error) {
return DoRequest("PATCH", url, []byte(data))
func Patch(url string, data...string) (*ClientResponse, error) {
return DoRequest("PATCH", url, data...)
}
func Connect(url, data string) (*ClientResponse, error) {
return DoRequest("CONNECT", url, []byte(data))
func Connect(url string, data...string) (*ClientResponse, error) {
return DoRequest("CONNECT", url, data...)
}
func Options(url, data string) (*ClientResponse, error) {
return DoRequest("OPTIONS", url, []byte(data))
func Options(url string, data...string) (*ClientResponse, error) {
return DoRequest("OPTIONS", url, data...)
}
func Trace(url, data string) (*ClientResponse, error) {
return DoRequest("TRACE", url, []byte(data))
func Trace(url string, data...string) (*ClientResponse, error) {
return DoRequest("TRACE", url, data...)
}
// 该方法支持二进制提交数据
func DoRequest(method, url string, data []byte) (*ClientResponse, error) {
return NewClient().DoRequest(method, url, data)
func DoRequest(method, url string, data...string) (*ClientResponse, error) {
return NewClient().DoRequest(method, url, data...)
}
// GET请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)

View File

@ -76,26 +76,30 @@ func (c *Client) SetBasicAuth(user, pass string) {
// GET请求
func (c *Client) Get(url string) (*ClientResponse, error) {
return c.DoRequest("GET", url, []byte(""))
return c.DoRequest("GET", url)
}
// PUT请求
func (c *Client) Put(url, data string) (*ClientResponse, error) {
return c.DoRequest("PUT", url, []byte(data))
func (c *Client) Put(url string, data...string) (*ClientResponse, error) {
return c.DoRequest("PUT", url, data...)
}
// POST请求提交数据默认使用表单方式提交数据(绝大部分场景下也是如此)。
// 如果服务端对Content-Type有要求可使用Client对象进行请求单独设置相关属性。
// 支持文件上传需要字段格式为FieldName=@file:
func (c *Client) Post(url, data string) (*ClientResponse, error) {
func (c *Client) Post(url string, data...string) (*ClientResponse, error) {
if len(c.prefix) > 0 {
url = c.prefix + url
}
param := ""
if len(data) > 0 {
param = data[0]
}
req := (*http.Request)(nil)
if strings.Contains(data, "@file:") {
if strings.Contains(param, "@file:") {
buffer := new(bytes.Buffer)
writer := multipart.NewWriter(buffer)
for _, item := range strings.Split(data, "&") {
for _, item := range strings.Split(param, "&") {
array := strings.Split(item, "=")
if len(array[1]) > 6 && strings.Compare(array[1][0:6], "@file:") == 0 {
path := array[1][6:]
@ -126,7 +130,7 @@ func (c *Client) Post(url, data string) (*ClientResponse, error) {
req.Header.Set("Content-Type", writer.FormDataContentType())
}
} else {
if r, err := http.NewRequest("POST", url, bytes.NewReader([]byte(data))); err != nil {
if r, err := http.NewRequest("POST", url, bytes.NewReader([]byte(param))); err != nil {
return nil, err
} else {
req = r
@ -154,28 +158,28 @@ func (c *Client) Post(url, data string) (*ClientResponse, error) {
}
// DELETE请求
func (c *Client) Delete(url, data string) (*ClientResponse, error) {
return c.DoRequest("DELETE", url, []byte(data))
func (c *Client) Delete(url string, data...string) (*ClientResponse, error) {
return c.DoRequest("DELETE", url, data...)
}
func (c *Client) Head(url, data string) (*ClientResponse, error) {
return c.DoRequest("HEAD", url, []byte(data))
func (c *Client) Head(url string, data...string) (*ClientResponse, error) {
return c.DoRequest("HEAD", url, data...)
}
func (c *Client) Patch(url, data string) (*ClientResponse, error) {
return c.DoRequest("PATCH", url, []byte(data))
func (c *Client) Patch(url string, data...string) (*ClientResponse, error) {
return c.DoRequest("PATCH", url, data...)
}
func (c *Client) Connect(url, data string) (*ClientResponse, error) {
return c.DoRequest("CONNECT", url, []byte(data))
func (c *Client) Connect(url string, data...string) (*ClientResponse, error) {
return c.DoRequest("CONNECT", url, data...)
}
func (c *Client) Options(url, data string) (*ClientResponse, error) {
return c.DoRequest("OPTIONS", url, []byte(data))
func (c *Client) Options(url string, data...string) (*ClientResponse, error) {
return c.DoRequest("OPTIONS", url, data...)
}
func (c *Client) Trace(url, data string) (*ClientResponse, error) {
return c.DoRequest("TRACE", url, []byte(data))
func (c *Client) Trace(url string, data...string) (*ClientResponse, error) {
return c.DoRequest("TRACE", url, data...)
}
// GET请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
@ -220,11 +224,7 @@ func (c *Client) TraceContent(url string, data...string) string {
// 请求并返回服务端结果(内部会自动读取服务端返回结果并关闭缓冲区指针)
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))
response, err := c.DoRequest(method, url, data...)
if err != nil {
return ""
}
@ -233,14 +233,18 @@ func (c *Client) DoRequestContent(method string, url string, data...string) stri
}
// 请求并返回response对象该方法支持二进制提交数据
func (c *Client) DoRequest(method, url string, data []byte) (*ClientResponse, error) {
func (c *Client) DoRequest(method, url string, data...string) (*ClientResponse, error) {
if strings.EqualFold("POST", method) {
return c.Post(url, string(data))
return c.Post(url, data...)
}
if len(c.prefix) > 0 {
url = c.prefix + url
}
req, err := http.NewRequest(strings.ToUpper(method), url, bytes.NewReader(data))
param := ""
if len(data) > 0 {
param = data[0]
}
req, err := http.NewRequest(strings.ToUpper(method), url, bytes.NewReader([]byte(param)))
if err != nil {
return nil, err
}

View File

@ -17,7 +17,7 @@ type ClientResponse struct {
http.Response
}
// 获取返回的数据
// 获取返回的数据(二进制).
func (r *ClientResponse) ReadAll() []byte {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
@ -26,6 +26,11 @@ func (r *ClientResponse) ReadAll() []byte {
return body
}
// 获取返回的数据(字符串).
func (r *ClientResponse) ReadAllString() string {
return string(r.ReadAll())
}
// 关闭返回的HTTP链接
func (r *ClientResponse) Close() {
r.Response.Close = true

View File

@ -32,7 +32,6 @@ func newResponse(s *Server, w http.ResponseWriter) *Response {
Server : s,
ResponseWriter : ResponseWriter {
ResponseWriter : w,
Status : http.StatusOK,
buffer : bytes.NewBuffer(nil),
},
}

View File

@ -17,6 +17,7 @@ import (
)
// 基本路由功能测试
func Test_Router_Basic(t *testing.T) {
s := g.Server(gtime.Nanosecond())
s.BindHandler("/:name", func(r *ghttp.Request){
@ -53,3 +54,124 @@ func Test_Router_Basic(t *testing.T) {
gtest.Assert(client.GetContent("/user/list/100.html"), "100")
})
}
// 测试HTTP Method注册.
func Test_Router_Method(t *testing.T) {
s := g.Server(gtime.Nanosecond())
s.BindHandler("GET:/get", func(r *ghttp.Request){
})
s.BindHandler("POST:/post", func(r *ghttp.Request){
})
s.SetPort(8105)
s.SetDumpRouteMap(false)
go s.Run()
defer func() {
s.Shutdown()
time.Sleep(time.Second)
}()
// 等待启动完成
time.Sleep(time.Second)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix("http://127.0.0.1:8105")
resp1, err := client.Get("/get")
defer resp1.Close()
gtest.Assert(err, nil)
gtest.Assert(resp1.StatusCode, 200)
resp2, err := client.Post("/get")
defer resp2.Close()
gtest.Assert(err, nil)
gtest.Assert(resp2.StatusCode, 404)
resp3, err := client.Get("/post")
defer resp3.Close()
gtest.Assert(err, nil)
gtest.Assert(resp3.StatusCode, 404)
resp4, err := client.Post("/post")
defer resp4.Close()
gtest.Assert(err, nil)
gtest.Assert(resp4.StatusCode, 200)
})
}
// 测试状态返回.
func Test_Router_Status(t *testing.T) {
s := g.Server(gtime.Nanosecond())
s.BindHandler("/200", func(r *ghttp.Request){
r.Response.WriteStatus(200)
})
s.BindHandler("/300", func(r *ghttp.Request){
r.Response.WriteStatus(300)
})
s.BindHandler("/400", func(r *ghttp.Request){
r.Response.WriteStatus(400)
})
s.BindHandler("/500", func(r *ghttp.Request){
r.Response.WriteStatus(500)
})
s.SetPort(8110)
s.SetDumpRouteMap(false)
go s.Run()
defer func() {
s.Shutdown()
time.Sleep(time.Second)
}()
// 等待启动完成
time.Sleep(time.Second)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix("http://127.0.0.1:8110")
resp1, err := client.Get("/200")
defer resp1.Close()
gtest.Assert(err, nil)
gtest.Assert(resp1.StatusCode, 200)
resp2, err := client.Get("/300")
defer resp2.Close()
gtest.Assert(err, nil)
gtest.Assert(resp2.StatusCode, 300)
resp3, err := client.Get("/400")
defer resp3.Close()
gtest.Assert(err, nil)
gtest.Assert(resp3.StatusCode, 400)
resp4, err := client.Get("/500")
defer resp4.Close()
gtest.Assert(err, nil)
gtest.Assert(resp4.StatusCode, 500)
})
}
// 测试不存在的路由.
func Test_Router_404(t *testing.T) {
s := g.Server(gtime.Nanosecond())
s.BindHandler("/", func(r *ghttp.Request){
r.Response.Write("hello")
})
s.SetPort(8120)
s.SetDumpRouteMap(false)
go s.Run()
defer func() {
s.Shutdown()
time.Sleep(time.Second)
}()
// 等待启动完成
time.Sleep(time.Second)
gtest.Case(t, func() {
client := ghttp.NewClient()
client.SetPrefix("http://127.0.0.1:8120")
gtest.Assert(client.GetContent("/"), "hello")
resp, err := client.Get("/ThisDoesNotExist")
defer resp.Close()
gtest.Assert(err, nil)
gtest.Assert(resp.StatusCode, 404)
})
}

View File

@ -82,6 +82,11 @@ func Test_Router_Group1(t *testing.T) {
gtest.Assert(client.GetContent ("/api/obj/show"), "Object Show")
gtest.Assert(client.DeleteContent("/api/obj/rest"), "Object REST Delete")
// 测试404
resp, err := client.Get("/ThisDoesNotExist")
defer resp.Close()
gtest.Assert(err, nil)
gtest.Assert(resp.StatusCode, 404)
})
}
@ -122,5 +127,11 @@ func Test_Router_Group2(t *testing.T) {
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")
// 测试404
resp, err := client.Get("/ThisDoesNotExist")
defer resp.Close()
gtest.Assert(err, nil)
gtest.Assert(resp.StatusCode, 404)
})
}