1.gofmt httplib.go httplib_test.go
2.replace test url to http://httpbin.org functions
Showing
3 changed files
with
33 additions
and
26 deletions
.idea/go_settings.xml
0 → 100644
| ... | @@ -16,13 +16,13 @@ import ( | ... | @@ -16,13 +16,13 @@ import ( |
| 16 | "mime/multipart" | 16 | "mime/multipart" |
| 17 | "net" | 17 | "net" |
| 18 | "net/http" | 18 | "net/http" |
| 19 | "net/http/httputil" | ||
| 20 | "net/http/cookiejar" | 19 | "net/http/cookiejar" |
| 20 | "net/http/httputil" | ||
| 21 | "net/url" | 21 | "net/url" |
| 22 | "os" | 22 | "os" |
| 23 | "strings" | 23 | "strings" |
| 24 | "time" | ||
| 25 | "sync" | 24 | "sync" |
| 25 | "time" | ||
| 26 | ) | 26 | ) |
| 27 | 27 | ||
| 28 | var defaultSetting = BeegoHttpSettings{false, "beegoServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false} | 28 | var defaultSetting = BeegoHttpSettings{false, "beegoServer", 60 * time.Second, 60 * time.Second, nil, nil, nil, false} |
| ... | @@ -42,10 +42,10 @@ func SetDefaultSetting(setting BeegoHttpSettings) { | ... | @@ -42,10 +42,10 @@ func SetDefaultSetting(setting BeegoHttpSettings) { |
| 42 | defer settingMutex.Unlock() | 42 | defer settingMutex.Unlock() |
| 43 | defaultSetting = setting | 43 | defaultSetting = setting |
| 44 | if defaultSetting.ConnectTimeout == 0 { | 44 | if defaultSetting.ConnectTimeout == 0 { |
| 45 | defaultSetting.ConnectTimeout = 60*time.Second | 45 | defaultSetting.ConnectTimeout = 60 * time.Second |
| 46 | } | 46 | } |
| 47 | if defaultSetting.ReadWriteTimeout == 0 { | 47 | if defaultSetting.ReadWriteTimeout == 0 { |
| 48 | defaultSetting.ReadWriteTimeout = 60*time.Second | 48 | defaultSetting.ReadWriteTimeout = 60 * time.Second |
| 49 | } | 49 | } |
| 50 | } | 50 | } |
| 51 | 51 | ||
| ... | @@ -54,7 +54,7 @@ func Get(url string) *BeegoHttpRequest { | ... | @@ -54,7 +54,7 @@ func Get(url string) *BeegoHttpRequest { |
| 54 | var req http.Request | 54 | var req http.Request |
| 55 | req.Method = "GET" | 55 | req.Method = "GET" |
| 56 | req.Header = http.Header{} | 56 | req.Header = http.Header{} |
| 57 | return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting } | 57 | return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | // Post returns *BeegoHttpRequest with POST method. | 60 | // Post returns *BeegoHttpRequest with POST method. |
| ... | @@ -62,7 +62,7 @@ func Post(url string) *BeegoHttpRequest { | ... | @@ -62,7 +62,7 @@ func Post(url string) *BeegoHttpRequest { |
| 62 | var req http.Request | 62 | var req http.Request |
| 63 | req.Method = "POST" | 63 | req.Method = "POST" |
| 64 | req.Header = http.Header{} | 64 | req.Header = http.Header{} |
| 65 | return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting } | 65 | return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} |
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | // Put returns *BeegoHttpRequest with PUT method. | 68 | // Put returns *BeegoHttpRequest with PUT method. |
| ... | @@ -70,7 +70,7 @@ func Put(url string) *BeegoHttpRequest { | ... | @@ -70,7 +70,7 @@ func Put(url string) *BeegoHttpRequest { |
| 70 | var req http.Request | 70 | var req http.Request |
| 71 | req.Method = "PUT" | 71 | req.Method = "PUT" |
| 72 | req.Header = http.Header{} | 72 | req.Header = http.Header{} |
| 73 | return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting } | 73 | return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | // Delete returns *BeegoHttpRequest DELETE GET method. | 76 | // Delete returns *BeegoHttpRequest DELETE GET method. |
| ... | @@ -78,7 +78,7 @@ func Delete(url string) *BeegoHttpRequest { | ... | @@ -78,7 +78,7 @@ func Delete(url string) *BeegoHttpRequest { |
| 78 | var req http.Request | 78 | var req http.Request |
| 79 | req.Method = "DELETE" | 79 | req.Method = "DELETE" |
| 80 | req.Header = http.Header{} | 80 | req.Header = http.Header{} |
| 81 | return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting } | 81 | return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | // Head returns *BeegoHttpRequest with HEAD method. | 84 | // Head returns *BeegoHttpRequest with HEAD method. |
| ... | @@ -86,11 +86,11 @@ func Head(url string) *BeegoHttpRequest { | ... | @@ -86,11 +86,11 @@ func Head(url string) *BeegoHttpRequest { |
| 86 | var req http.Request | 86 | var req http.Request |
| 87 | req.Method = "HEAD" | 87 | req.Method = "HEAD" |
| 88 | req.Header = http.Header{} | 88 | req.Header = http.Header{} |
| 89 | return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting } | 89 | return &BeegoHttpRequest{url, &req, map[string]string{}, map[string]string{}, defaultSetting} |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | // BeegoHttpSettings | 92 | // BeegoHttpSettings |
| 93 | type BeegoHttpSettings struct{ | 93 | type BeegoHttpSettings struct { |
| 94 | ShowDebug bool | 94 | ShowDebug bool |
| 95 | UserAgent string | 95 | UserAgent string |
| 96 | ConnectTimeout time.Duration | 96 | ConnectTimeout time.Duration |
| ... | @@ -238,9 +238,9 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ... | @@ -238,9 +238,9 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { |
| 238 | 238 | ||
| 239 | if b.req.Method == "GET" && len(paramBody) > 0 { | 239 | if b.req.Method == "GET" && len(paramBody) > 0 { |
| 240 | if strings.Index(b.url, "?") != -1 { | 240 | if strings.Index(b.url, "?") != -1 { |
| 241 | b.url += "&"+paramBody | 241 | b.url += "&" + paramBody |
| 242 | } else { | 242 | } else { |
| 243 | b.url = b.url+"?"+paramBody | 243 | b.url = b.url + "?" + paramBody |
| 244 | } | 244 | } |
| 245 | } else if b.req.Method == "POST" && b.req.Body == nil && len(paramBody) > 0 { | 245 | } else if b.req.Method == "POST" && b.req.Body == nil && len(paramBody) > 0 { |
| 246 | if len(b.files) > 0 { | 246 | if len(b.files) > 0 { |
| ... | @@ -278,7 +278,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ... | @@ -278,7 +278,7 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { |
| 278 | 278 | ||
| 279 | url, err := url.Parse(b.url) | 279 | url, err := url.Parse(b.url) |
| 280 | if url.Scheme == "" { | 280 | if url.Scheme == "" { |
| 281 | b.url = "http://"+b.url | 281 | b.url = "http://" + b.url |
| 282 | url, err = url.Parse(b.url) | 282 | url, err = url.Parse(b.url) |
| 283 | } | 283 | } |
| 284 | if err != nil { | 284 | if err != nil { |
| ... | @@ -324,13 +324,13 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { | ... | @@ -324,13 +324,13 @@ func (b *BeegoHttpRequest) getResponse() (*http.Response, error) { |
| 324 | createDefaultCookie() | 324 | createDefaultCookie() |
| 325 | } | 325 | } |
| 326 | jar = defaultCookieJar | 326 | jar = defaultCookieJar |
| 327 | }else { | 327 | } else { |
| 328 | jar = nil | 328 | jar = nil |
| 329 | } | 329 | } |
| 330 | 330 | ||
| 331 | client := &http.Client{ | 331 | client := &http.Client{ |
| 332 | Transport: trans, | 332 | Transport: trans, |
| 333 | Jar:jar, | 333 | Jar: jar, |
| 334 | } | 334 | } |
| 335 | 335 | ||
| 336 | if b.setting.UserAgent != "" { | 336 | if b.setting.UserAgent != "" { | ... | ... |
| 1 | // Beego (http://localhost/httplib_test.php) | 1 | // Beego (http://beego.me) |
| 2 | // @description beego is an open-source, high-performance web framework for the Go programming language. | 2 | // @description beego is an open-source, high-performance web framework for the Go programming language. |
| 3 | // @link http://github.com/astaxie/beego for the canonical source repository | 3 | // @link http://github.com/astaxie/beego for the canonical source repository |
| 4 | // @license http://github.com/astaxie/beego/blob/master/LICENSE | 4 | // @license http://github.com/astaxie/beego/blob/master/LICENSE |
| ... | @@ -7,13 +7,13 @@ | ... | @@ -7,13 +7,13 @@ |
| 7 | package httplib | 7 | package httplib |
| 8 | 8 | ||
| 9 | import ( | 9 | import ( |
| 10 | "testing" | ||
| 11 | "fmt" | 10 | "fmt" |
| 12 | "io/ioutil" | 11 | "io/ioutil" |
| 12 | "testing" | ||
| 13 | ) | 13 | ) |
| 14 | 14 | ||
| 15 | func TestGetUrl(t *testing.T) { | 15 | func TestGetUrl(t *testing.T) { |
| 16 | resp, err := Get("http://localhost/httplib_test.php").Debug(true).Response() | 16 | resp, err := Get("http://beego.me").Debug(true).Response() |
| 17 | if err != nil { | 17 | if err != nil { |
| 18 | t.Fatal(err) | 18 | t.Fatal(err) |
| 19 | } | 19 | } |
| ... | @@ -29,7 +29,7 @@ func TestGetUrl(t *testing.T) { | ... | @@ -29,7 +29,7 @@ func TestGetUrl(t *testing.T) { |
| 29 | t.Fatal("data is no") | 29 | t.Fatal("data is no") |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | str, err := Get("http://localhost/httplib_test.php").String() | 32 | str, err := Get("http://beego.me").String() |
| 33 | if err != nil { | 33 | if err != nil { |
| 34 | t.Fatal(err) | 34 | t.Fatal(err) |
| 35 | } | 35 | } |
| ... | @@ -39,7 +39,7 @@ func TestGetUrl(t *testing.T) { | ... | @@ -39,7 +39,7 @@ func TestGetUrl(t *testing.T) { |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | func TestPost(t *testing.T) { | 41 | func TestPost(t *testing.T) { |
| 42 | b := Post("http://localhost/httplib_test.php").Debug(true) | 42 | b := Post("http://beego.me").Debug(true) |
| 43 | b.Param("username", "astaxie") | 43 | b.Param("username", "astaxie") |
| 44 | b.Param("password", "hello") | 44 | b.Param("password", "hello") |
| 45 | b.PostFile("uploadfile", "httplib_test.php") | 45 | b.PostFile("uploadfile", "httplib_test.php") |
| ... | @@ -52,7 +52,7 @@ func TestPost(t *testing.T) { | ... | @@ -52,7 +52,7 @@ func TestPost(t *testing.T) { |
| 52 | 52 | ||
| 53 | func TestSimpleGetString(t *testing.T) { | 53 | func TestSimpleGetString(t *testing.T) { |
| 54 | fmt.Println("TestSimpleGetString==========================================") | 54 | fmt.Println("TestSimpleGetString==========================================") |
| 55 | html, err := Get("http://localhost/httplib_test.php").SetAgent("beegoooooo").String() | 55 | html, err := Get("http://httpbin.org/headers").SetAgent("beegoooooo").String() |
| 56 | if err != nil { | 56 | if err != nil { |
| 57 | t.Fatal(err) | 57 | t.Fatal(err) |
| 58 | } | 58 | } |
| ... | @@ -62,12 +62,12 @@ func TestSimpleGetString(t *testing.T) { | ... | @@ -62,12 +62,12 @@ func TestSimpleGetString(t *testing.T) { |
| 62 | 62 | ||
| 63 | func TestSimpleGetStringWithDefaultCookie(t *testing.T) { | 63 | func TestSimpleGetStringWithDefaultCookie(t *testing.T) { |
| 64 | fmt.Println("TestSimpleGetStringWithDefaultCookie==========================================") | 64 | fmt.Println("TestSimpleGetStringWithDefaultCookie==========================================") |
| 65 | html, err := Get("http://localhost/httplib_test.php").SetEnableCookie(true).String() | 65 | html, err := Get("http://httpbin.org/cookies/set?k1=v1").SetEnableCookie(true).String() |
| 66 | if err != nil { | 66 | if err != nil { |
| 67 | t.Fatal(err) | 67 | t.Fatal(err) |
| 68 | } | 68 | } |
| 69 | fmt.Println(html) | 69 | fmt.Println(html) |
| 70 | html, err = Get("http://localhost/httplib_test.php").SetEnableCookie(true).String() | 70 | html, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String() |
| 71 | if err != nil { | 71 | if err != nil { |
| 72 | t.Fatal(err) | 72 | t.Fatal(err) |
| 73 | } | 73 | } |
| ... | @@ -83,15 +83,15 @@ func TestDefaultSetting(t *testing.T) { | ... | @@ -83,15 +83,15 @@ func TestDefaultSetting(t *testing.T) { |
| 83 | def.UserAgent = "UserAgent" | 83 | def.UserAgent = "UserAgent" |
| 84 | //def.ConnectTimeout = 60*time.Second | 84 | //def.ConnectTimeout = 60*time.Second |
| 85 | //def.ReadWriteTimeout = 60*time.Second | 85 | //def.ReadWriteTimeout = 60*time.Second |
| 86 | def.Transport = nil//http.DefaultTransport | 86 | def.Transport = nil //http.DefaultTransport |
| 87 | SetDefaultSetting(def) | 87 | SetDefaultSetting(def) |
| 88 | 88 | ||
| 89 | html, err := Get("http://localhost/httplib_test.php").String() | 89 | html, err := Get("http://httpbin.org/headers").String() |
| 90 | if err != nil { | 90 | if err != nil { |
| 91 | t.Fatal(err) | 91 | t.Fatal(err) |
| 92 | } | 92 | } |
| 93 | fmt.Println(html) | 93 | fmt.Println(html) |
| 94 | html, err = Get("http://localhost/httplib_test.php").String() | 94 | html, err = Get("http://httpbin.org/headers").String() |
| 95 | if err != nil { | 95 | if err != nil { |
| 96 | t.Fatal(err) | 96 | t.Fatal(err) |
| 97 | } | 97 | } | ... | ... |
-
Please register or sign in to post a comment