mirror of
https://github.com/astaxie/beego.git
synced 2024-11-16 17:20:55 +00:00
Merge pull request #750 from smallfish/develop
Update httplib support read data from response buffer, add some testcase...
This commit is contained in:
commit
be005f9774
@ -76,41 +76,46 @@ func SetDefaultSetting(setting BeegoHttpSettings) {
|
|||||||
// Get returns *BeegoHttpRequest with GET method.
|
// Get returns *BeegoHttpRequest with GET method.
|
||||||
func Get(url string) *BeegoHttpRequest {
|
func Get(url string) *BeegoHttpRequest {
|
||||||
var req http.Request
|
var req http.Request
|
||||||
|
var resp http.Response
|
||||||
req.Method = "GET"
|
req.Method = "GET"
|
||||||
req.Header = http.Header{}
|
req.Header = http.Header{}
|
||||||
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting}
|
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post returns *BeegoHttpRequest with POST method.
|
// Post returns *BeegoHttpRequest with POST method.
|
||||||
func Post(url string) *BeegoHttpRequest {
|
func Post(url string) *BeegoHttpRequest {
|
||||||
var req http.Request
|
var req http.Request
|
||||||
|
var resp http.Response
|
||||||
req.Method = "POST"
|
req.Method = "POST"
|
||||||
req.Header = http.Header{}
|
req.Header = http.Header{}
|
||||||
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting}
|
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put returns *BeegoHttpRequest with PUT method.
|
// Put returns *BeegoHttpRequest with PUT method.
|
||||||
func Put(url string) *BeegoHttpRequest {
|
func Put(url string) *BeegoHttpRequest {
|
||||||
var req http.Request
|
var req http.Request
|
||||||
|
var resp http.Response
|
||||||
req.Method = "PUT"
|
req.Method = "PUT"
|
||||||
req.Header = http.Header{}
|
req.Header = http.Header{}
|
||||||
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting}
|
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete returns *BeegoHttpRequest DELETE GET method.
|
// Delete returns *BeegoHttpRequest DELETE GET method.
|
||||||
func Delete(url string) *BeegoHttpRequest {
|
func Delete(url string) *BeegoHttpRequest {
|
||||||
var req http.Request
|
var req http.Request
|
||||||
|
var resp http.Response
|
||||||
req.Method = "DELETE"
|
req.Method = "DELETE"
|
||||||
req.Header = http.Header{}
|
req.Header = http.Header{}
|
||||||
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting}
|
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Head returns *BeegoHttpRequest with HEAD method.
|
// Head returns *BeegoHttpRequest with HEAD method.
|
||||||
func Head(url string) *BeegoHttpRequest {
|
func Head(url string) *BeegoHttpRequest {
|
||||||
var req http.Request
|
var req http.Request
|
||||||
|
var resp http.Response
|
||||||
req.Method = "HEAD"
|
req.Method = "HEAD"
|
||||||
req.Header = http.Header{}
|
req.Header = http.Header{}
|
||||||
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting}
|
return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting, &resp, nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeegoHttpSettings
|
// BeegoHttpSettings
|
||||||
@ -132,6 +137,8 @@ type BeegoHttpRequest struct {
|
|||||||
params map[string]string
|
params map[string]string
|
||||||
files map[string]string
|
files map[string]string
|
||||||
setting BeegoHttpSettings
|
setting BeegoHttpSettings
|
||||||
|
resp *http.Response
|
||||||
|
body []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change request settings
|
// Change request settings
|
||||||
@ -247,6 +254,9 @@ func (b *BeegoHttpRequest) Body(data interface{}) *BeegoHttpRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
|
func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
|
||||||
|
if b.resp.StatusCode != 0 {
|
||||||
|
return b.resp, nil
|
||||||
|
}
|
||||||
var paramBody string
|
var paramBody string
|
||||||
if len(b.params) > 0 {
|
if len(b.params) > 0 {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
@ -365,6 +375,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
b.resp = resp
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -382,6 +393,9 @@ func (b *BeegoHttpRequest) String() (string, error) {
|
|||||||
// Bytes returns the body []byte in response.
|
// Bytes returns the body []byte in response.
|
||||||
// it calls Response inner.
|
// it calls Response inner.
|
||||||
func (b *BeegoHttpRequest) Bytes() ([]byte, error) {
|
func (b *BeegoHttpRequest) Bytes() ([]byte, error) {
|
||||||
|
if b.body != nil {
|
||||||
|
return b.body, nil
|
||||||
|
}
|
||||||
resp, err := b.getResponse()
|
resp, err := b.getResponse()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -394,6 +408,7 @@ func (b *BeegoHttpRequest) Bytes() ([]byte, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
b.body = data
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,27 +15,51 @@
|
|||||||
package httplib
|
package httplib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSimpleGet(t *testing.T) {
|
func TestResponse(t *testing.T) {
|
||||||
str, err := Get("http://httpbin.org/get").String()
|
req := Get("http://httpbin.org/get")
|
||||||
|
resp, err := req.Response()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Log(str)
|
t.Log(resp)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGet(t *testing.T) {
|
||||||
|
req := Get("http://httpbin.org/get")
|
||||||
|
b, err := req.Bytes()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(b)
|
||||||
|
|
||||||
|
s, err := req.String()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(s)
|
||||||
|
|
||||||
|
if string(b) != s {
|
||||||
|
t.Fatal("request data not match")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSimplePost(t *testing.T) {
|
func TestSimplePost(t *testing.T) {
|
||||||
v := "smallfish"
|
v := "smallfish"
|
||||||
req := Post("http://httpbin.org/post")
|
req := Post("http://httpbin.org/post")
|
||||||
req.Param("username", v)
|
req.Param("username", v)
|
||||||
|
|
||||||
str, err := req.String()
|
str, err := req.String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
|
|
||||||
n := strings.Index(str, v)
|
n := strings.Index(str, v)
|
||||||
if n == -1 {
|
if n == -1 {
|
||||||
t.Fatal(v + " not found in post")
|
t.Fatal(v + " not found in post")
|
||||||
@ -47,17 +71,35 @@ func TestPostFile(t *testing.T) {
|
|||||||
req := Post("http://httpbin.org/post")
|
req := Post("http://httpbin.org/post")
|
||||||
req.Param("username", v)
|
req.Param("username", v)
|
||||||
req.PostFile("uploadfile", "httplib_test.go")
|
req.PostFile("uploadfile", "httplib_test.go")
|
||||||
|
|
||||||
str, err := req.String()
|
str, err := req.String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
|
|
||||||
n := strings.Index(str, v)
|
n := strings.Index(str, v)
|
||||||
if n == -1 {
|
if n == -1 {
|
||||||
t.Fatal(v + " not found in post")
|
t.Fatal(v + " not found in post")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSimplePut(t *testing.T) {
|
||||||
|
str, err := Put("http://httpbin.org/put").String()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(str)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSimpleDelete(t *testing.T) {
|
||||||
|
str, err := Delete("http://httpbin.org/delete").String()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(str)
|
||||||
|
}
|
||||||
|
|
||||||
func TestWithCookie(t *testing.T) {
|
func TestWithCookie(t *testing.T) {
|
||||||
v := "smallfish"
|
v := "smallfish"
|
||||||
str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String()
|
str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String()
|
||||||
@ -65,11 +107,13 @@ func TestWithCookie(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
|
|
||||||
str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
|
str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
|
|
||||||
n := strings.Index(str, v)
|
n := strings.Index(str, v)
|
||||||
if n == -1 {
|
if n == -1 {
|
||||||
t.Fatal(v + " not found in cookie")
|
t.Fatal(v + " not found in cookie")
|
||||||
@ -83,6 +127,7 @@ func TestWithUserAgent(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
|
|
||||||
n := strings.Index(str, v)
|
n := strings.Index(str, v)
|
||||||
if n == -1 {
|
if n == -1 {
|
||||||
t.Fatal(v + " not found in user-agent")
|
t.Fatal(v + " not found in user-agent")
|
||||||
@ -102,8 +147,47 @@ func TestWithSetting(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
t.Log(str)
|
t.Log(str)
|
||||||
|
|
||||||
n := strings.Index(str, v)
|
n := strings.Index(str, v)
|
||||||
if n == -1 {
|
if n == -1 {
|
||||||
t.Fatal(v + " not found in user-agent")
|
t.Fatal(v + " not found in user-agent")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToJson(t *testing.T) {
|
||||||
|
req := Get("http://httpbin.org/ip")
|
||||||
|
resp, err := req.Response()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(resp)
|
||||||
|
|
||||||
|
// httpbin will return http remote addr
|
||||||
|
type Ip struct {
|
||||||
|
Origin string `json:"origin"`
|
||||||
|
}
|
||||||
|
var ip Ip
|
||||||
|
err = req.ToJson(&ip)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(ip.Origin)
|
||||||
|
|
||||||
|
if n := strings.Count(ip.Origin, "."); n != 3 {
|
||||||
|
t.Fatal("response is not valid ip")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToFile(t *testing.T) {
|
||||||
|
f := "beego_testfile"
|
||||||
|
req := Get("http://httpbin.org/ip")
|
||||||
|
err := req.ToFile(f)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer os.Remove(f)
|
||||||
|
b, err := ioutil.ReadFile(f)
|
||||||
|
if n := strings.Index(string(b), "origin"); n == -1 {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user