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"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/http/cookiejar"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
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()
|
||||
defaultSetting = setting
|
||||
if defaultSetting.ConnectTimeout == 0 {
|
||||
defaultSetting.ConnectTimeout = 60*time.Second
|
||||
defaultSetting.ConnectTimeout = 60 * time.Second
|
||||
}
|
||||
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
|
||||
req.Method = "GET"
|
||||
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.
|
||||
@ -62,7 +62,7 @@ func Post(url string) *BeegoHttpRequest {
|
||||
var req http.Request
|
||||
req.Method = "POST"
|
||||
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.
|
||||
@ -70,7 +70,7 @@ func Put(url string) *BeegoHttpRequest {
|
||||
var req http.Request
|
||||
req.Method = "PUT"
|
||||
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.
|
||||
@ -78,7 +78,7 @@ func Delete(url string) *BeegoHttpRequest {
|
||||
var req http.Request
|
||||
req.Method = "DELETE"
|
||||
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.
|
||||
@ -86,11 +86,11 @@ func Head(url string) *BeegoHttpRequest {
|
||||
var req http.Request
|
||||
req.Method = "HEAD"
|
||||
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
|
||||
type BeegoHttpSettings struct{
|
||||
type BeegoHttpSettings struct {
|
||||
ShowDebug bool
|
||||
UserAgent string
|
||||
ConnectTimeout time.Duration
|
||||
@ -103,11 +103,11 @@ type BeegoHttpSettings struct{
|
||||
|
||||
// BeegoHttpRequest provides more useful methods for requesting one url than http.Request.
|
||||
type BeegoHttpRequest struct {
|
||||
url string
|
||||
req *http.Request
|
||||
params map[string]string
|
||||
files map[string]string
|
||||
setting BeegoHttpSettings
|
||||
url string
|
||||
req *http.Request
|
||||
params map[string]string
|
||||
files map[string]string
|
||||
setting BeegoHttpSettings
|
||||
}
|
||||
|
||||
// Change request settings
|
||||
@ -238,9 +238,9 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
|
||||
|
||||
if b.req.Method == "GET" && len(paramBody) > 0 {
|
||||
if strings.Index(b.url, "?") != -1 {
|
||||
b.url += "&"+paramBody
|
||||
b.url += "&" + paramBody
|
||||
} else {
|
||||
b.url = b.url+"?"+paramBody
|
||||
b.url = b.url + "?" + paramBody
|
||||
}
|
||||
} else if b.req.Method == "POST" && b.req.Body == nil && len(paramBody) > 0 {
|
||||
if len(b.files) > 0 {
|
||||
@ -278,7 +278,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
|
||||
|
||||
url, err := url.Parse(b.url)
|
||||
if url.Scheme == "" {
|
||||
b.url = "http://"+b.url
|
||||
b.url = "http://" + b.url
|
||||
url, err = url.Parse(b.url)
|
||||
}
|
||||
if err != nil {
|
||||
@ -324,13 +324,13 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) {
|
||||
createDefaultCookie()
|
||||
}
|
||||
jar = defaultCookieJar
|
||||
}else {
|
||||
} else {
|
||||
jar = nil
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Transport: trans,
|
||||
Jar:jar,
|
||||
Jar: jar,
|
||||
}
|
||||
|
||||
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.
|
||||
// @link http://github.com/astaxie/beego for the canonical source repository
|
||||
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
||||
@ -7,13 +7,13 @@
|
||||
package httplib
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
)
|
||||
|
||||
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 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -29,7 +29,7 @@ func TestGetUrl(t *testing.T) {
|
||||
t.Fatal("data is no")
|
||||
}
|
||||
|
||||
str, err := Get("http://localhost/httplib_test.php").String()
|
||||
str, err := Get("http://beego.me").String()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -39,7 +39,7 @@ func TestGetUrl(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("password", "hello")
|
||||
b.PostFile("uploadfile", "httplib_test.php")
|
||||
@ -52,7 +52,7 @@ func TestPost(t *testing.T) {
|
||||
|
||||
func TestSimpleGetString(t *testing.T) {
|
||||
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 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -62,12 +62,12 @@ func TestSimpleGetString(t *testing.T) {
|
||||
|
||||
func TestSimpleGetStringWithDefaultCookie(t *testing.T) {
|
||||
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 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
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 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -83,15 +83,15 @@ func TestDefaultSetting(t *testing.T) {
|
||||
def.UserAgent = "UserAgent"
|
||||
//def.ConnectTimeout = 60*time.Second
|
||||
//def.ReadWriteTimeout = 60*time.Second
|
||||
def.Transport = nil//http.DefaultTransport
|
||||
def.Transport = nil //http.DefaultTransport
|
||||
SetDefaultSetting(def)
|
||||
|
||||
html, err := Get("http://localhost/httplib_test.php").String()
|
||||
html, err := Get("http://httpbin.org/headers").String()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Println(html)
|
||||
html, err = Get("http://localhost/httplib_test.php").String()
|
||||
html, err = Get("http://httpbin.org/headers").String()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user