Skip to content

Commit

Permalink
fix http empty url
Browse files Browse the repository at this point in the history
  • Loading branch information
eyasliu committed Aug 12, 2019
1 parent 5015646 commit c48a6c2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
12 changes: 10 additions & 2 deletions http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ func TestGet(t *testing.T) {
fmt.Printf("http 接收 %s %s\n", req.SuperAgent.Method, req.SuperAgent.Url)
return res
}).Timeout(time.Second * 10).
BaseURL("https://api.github.com")
BaseURL("https://api.github.com").
BaseURL("/repos")

res, err := http.Get("/repos/eyasliu/blog/issues", map[string]interface{}{
res, err := http.Get("/eyasliu/blog/issues", map[string]interface{}{
"per_page": 1,
})
if err != nil {
Expand Down Expand Up @@ -44,6 +45,13 @@ func TestError(t *testing.T) {
panic("should get 404 error")
}

res, err = Get("", nil)
if err != nil {
t.Logf("success empty url, statusCode=%d body=%s error=%s", res.Status(), res.String(), err.Error())
} else {
panic("should error")
}

}

// func TestProxy(t *testing.T) {
Expand Down
15 changes: 12 additions & 3 deletions http/request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package http

import (
"errors"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -88,17 +89,25 @@ func (r Request) UseResponse(mdl responseMidlewareHandler) *Request {

// BaseURL 设置url前缀
func (r Request) BaseURL(url string) *Request {
r.baseURL = url
r.baseURL += url
return &r
}

// Do 发出请求,method 请求方法,url 请求地址, query 查询参数,body 请求数据,file 文件对象/地址
func (r Request) Do(method, url string, query, body, file interface{}) (*Response, error) {

// set mthod url
// r.SuperAgent = r.SuperAgent.CustomMethod(method, r.baseURL+url)
if method == "" || url == "" {
return &Response{
Request: &r,
Raw: nil,
Body: []byte{},
Errs: []error{errors.New("url is empty")},
}, fmt.Errorf("http url can't empty")
}
r.SuperAgent = r.SuperAgent.CustomMethod(method, r.baseURL+url)
r.SuperAgent.Method = strings.ToUpper(method)
r.SuperAgent.Url = url
r.SuperAgent.Url = r.baseURL + url
r.SuperAgent.Errors = nil

// set query string
Expand Down
15 changes: 12 additions & 3 deletions http/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,26 @@ func (r *Response) Byte() []byte {

// Status 获取响应状态码
func (r *Response) Status() int {
return r.Raw.StatusCode
if r.Raw != nil {
return r.Raw.StatusCode
}
return 0
}

// Header 获取响应header
func (r *Response) Header() http.Header {
return r.Raw.Header
if r.Raw != nil {
return r.Raw.Header
}
return nil
}

// Cookies 获取响应 cookie
func (r *Response) Cookies() []*http.Cookie {
return r.Raw.Cookies()
if r.Raw != nil {
return r.Raw.Cookies()
}
return nil
}

// IsError 是否响应错误
Expand Down

0 comments on commit c48a6c2

Please sign in to comment.