mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 03:11:30 +00:00
1.gofmt httplib.go httplib_test.go
2.replace test url to http://httpbin.org functions
This commit is contained in:
parent
ebb3b91df9
commit
bd537554ea
7
.idea/go_settings.xml
generated
Normal file
7
.idea/go_settings.xml
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="GoProjectSettings">
|
||||||
|
<option name="enableOptimizeImports" value="true" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
|
|
@ -16,13 +16,13 @@ import (
|
|||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var defaultSetting = BeegoHttpSettings{false, "beegoServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false}
|
var defaultSetting = BeegoHttpSettings{false, "beegoServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false}
|
||||||
@ -42,10 +42,10 @@ func SetDefaultSetting(setting BeegoHttpSettings) {
|
|||||||
defer settingMutex.Unlock()
|
defer settingMutex.Unlock()
|
||||||
defaultSetting = setting
|
defaultSetting = setting
|
||||||
if defaultSetting.ConnectTimeout == 0 {
|
if defaultSetting.ConnectTimeout == 0 {
|
||||||
defaultSetting.ConnectTimeout = 60*time.Second
|
defaultSetting.ConnectTimeout = 60 * time.Second
|
||||||
}
|
}
|
||||||
if defaultSetting.ReadWriteTimeout == 0 {
|
if defaultSetting.ReadWriteTimeout == 0 {
|
||||||
defaultSetting.ReadWriteTimeout = 60*time.Second
|
defaultSetting.ReadWriteTimeout = 60 * time.Second
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ func Get(url string) *BeegoHttpRequest {
|
|||||||
var req http.Request
|
var req http.Request
|
||||||
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}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post returns *BeegoHttpRequest with POST method.
|
// Post returns *BeegoHttpRequest with POST method.
|
||||||
@ -62,7 +62,7 @@ func Post(url string) *BeegoHttpRequest {
|
|||||||
var req http.Request
|
var req http.Request
|
||||||
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}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put returns *BeegoHttpRequest with PUT method.
|
// Put returns *BeegoHttpRequest with PUT method.
|
||||||
@ -70,7 +70,7 @@ func Put(url string) *BeegoHttpRequest {
|
|||||||
var req http.Request
|
var req http.Request
|
||||||
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}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete returns *BeegoHttpRequest DELETE GET method.
|
// Delete returns *BeegoHttpRequest DELETE GET method.
|
||||||
@ -78,7 +78,7 @@ func Delete(url string) *BeegoHttpRequest {
|
|||||||
var req http.Request
|
var req http.Request
|
||||||
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}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Head returns *BeegoHttpRequest with HEAD method.
|
// Head returns *BeegoHttpRequest with HEAD method.
|
||||||
@ -86,11 +86,11 @@ func Head(url string) *BeegoHttpRequest {
|
|||||||
var req http.Request
|
var req http.Request
|
||||||
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}
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeegoHttpSettings
|
// BeegoHttpSettings
|
||||||
type BeegoHttpSettings struct{
|
type BeegoHttpSettings struct {
|
||||||
ShowDebug bool
|
ShowDebug bool
|
||||||
UserAgent string
|
UserAgent string
|
||||||
ConnectTimeout time.Duration
|
ConnectTimeout time.Duration
|
||||||
@ -103,11 +103,11 @@ type BeegoHttpSettings struct{
|
|||||||
|
|
||||||
// BeegoHttpRequest provides more useful methods for requesting one url than http.Request.
|
// BeegoHttpRequest provides more useful methods for requesting one url than http.Request.
|
||||||
type BeegoHttpRequest struct {
|
type BeegoHttpRequest struct {
|
||||||
url string
|
url string
|
||||||
req *http.Request
|
req *http.Request
|
||||||
params map[string]string
|
params map[string]string
|
||||||
files map[string]string
|
files map[string]string
|
||||||
setting BeegoHttpSettings
|
setting BeegoHttpSettings
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change request settings
|
// Change request settings
|
||||||
@ -238,9 +238,9 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
|
|||||||
|
|
||||||
if b.req.Method == "GET" && len(paramBody) > 0 {
|
if b.req.Method == "GET" && len(paramBody) > 0 {
|
||||||
if strings.Index(b.url, "?") != -1 {
|
if strings.Index(b.url, "?") != -1 {
|
||||||
b.url += "&"+paramBody
|
b.url += "&" + paramBody
|
||||||
} else {
|
} else {
|
||||||
b.url = b.url+"?"+paramBody
|
b.url = b.url + "?" + paramBody
|
||||||
}
|
}
|
||||||
} else if b.req.Method == "POST" && b.req.Body == nil && len(paramBody) > 0 {
|
} else if b.req.Method == "POST" && b.req.Body == nil && len(paramBody) > 0 {
|
||||||
if len(b.files) > 0 {
|
if len(b.files) > 0 {
|
||||||
@ -278,7 +278,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
|
|||||||
|
|
||||||
url, err := url.Parse(b.url)
|
url, err := url.Parse(b.url)
|
||||||
if url.Scheme == "" {
|
if url.Scheme == "" {
|
||||||
b.url = "http://"+b.url
|
b.url = "http://" + b.url
|
||||||
url, err = url.Parse(b.url)
|
url, err = url.Parse(b.url)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -324,13 +324,13 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
|
|||||||
createDefaultCookie()
|
createDefaultCookie()
|
||||||
}
|
}
|
||||||
jar = defaultCookieJar
|
jar = defaultCookieJar
|
||||||
}else {
|
} else {
|
||||||
jar = nil
|
jar = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Transport: trans,
|
Transport: trans,
|
||||||
Jar:jar,
|
Jar: jar,
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.setting.UserAgent != "" {
|
if b.setting.UserAgent != "" {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// Beego (http://localhost/httplib_test.php)
|
// Beego (http://beego.me)
|
||||||
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
||||||
// @link http://github.com/astaxie/beego for the canonical source repository
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
||||||
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
||||||
@ -7,13 +7,13 @@
|
|||||||
package httplib
|
package httplib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetUrl(t *testing.T) {
|
func TestGetUrl(t *testing.T) {
|
||||||
resp, err := Get("http://localhost/httplib_test.php").Debug(true).Response()
|
resp, err := Get("http://beego.me").Debug(true).Response()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -29,7 +29,7 @@ func TestGetUrl(t *testing.T) {
|
|||||||
t.Fatal("data is no")
|
t.Fatal("data is no")
|
||||||
}
|
}
|
||||||
|
|
||||||
str, err := Get("http://localhost/httplib_test.php").String()
|
str, err := Get("http://beego.me").String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ func TestGetUrl(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPost(t *testing.T) {
|
func TestPost(t *testing.T) {
|
||||||
b := Post("http://localhost/httplib_test.php").Debug(true)
|
b := Post("http://beego.me").Debug(true)
|
||||||
b.Param("username", "astaxie")
|
b.Param("username", "astaxie")
|
||||||
b.Param("password", "hello")
|
b.Param("password", "hello")
|
||||||
b.PostFile("uploadfile", "httplib_test.php")
|
b.PostFile("uploadfile", "httplib_test.php")
|
||||||
@ -52,7 +52,7 @@ func TestPost(t *testing.T) {
|
|||||||
|
|
||||||
func TestSimpleGetString(t *testing.T) {
|
func TestSimpleGetString(t *testing.T) {
|
||||||
fmt.Println("TestSimpleGetString==========================================")
|
fmt.Println("TestSimpleGetString==========================================")
|
||||||
html, err := Get("http://localhost/httplib_test.php").SetAgent("beegoooooo").String()
|
html, err := Get("http://httpbin.org/headers").SetAgent("beegoooooo").String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -62,12 +62,12 @@ func TestSimpleGetString(t *testing.T) {
|
|||||||
|
|
||||||
func TestSimpleGetStringWithDefaultCookie(t *testing.T) {
|
func TestSimpleGetStringWithDefaultCookie(t *testing.T) {
|
||||||
fmt.Println("TestSimpleGetStringWithDefaultCookie==========================================")
|
fmt.Println("TestSimpleGetStringWithDefaultCookie==========================================")
|
||||||
html, err := Get("http://localhost/httplib_test.php").SetEnableCookie(true).String()
|
html, err := Get("http://httpbin.org/cookies/set?k1=v1").SetEnableCookie(true).String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println(html)
|
fmt.Println(html)
|
||||||
html, err = Get("http://localhost/httplib_test.php").SetEnableCookie(true).String()
|
html, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -83,15 +83,15 @@ func TestDefaultSetting(t *testing.T) {
|
|||||||
def.UserAgent = "UserAgent"
|
def.UserAgent = "UserAgent"
|
||||||
//def.ConnectTimeout = 60*time.Second
|
//def.ConnectTimeout = 60*time.Second
|
||||||
//def.ReadWriteTimeout = 60*time.Second
|
//def.ReadWriteTimeout = 60*time.Second
|
||||||
def.Transport = nil//http.DefaultTransport
|
def.Transport = nil //http.DefaultTransport
|
||||||
SetDefaultSetting(def)
|
SetDefaultSetting(def)
|
||||||
|
|
||||||
html, err := Get("http://localhost/httplib_test.php").String()
|
html, err := Get("http://httpbin.org/headers").String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Println(html)
|
fmt.Println(html)
|
||||||
html, err = Get("http://localhost/httplib_test.php").String()
|
html, err = Get("http://httpbin.org/headers").String()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user