Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
7668c54d
Commit
7668c54d
authored
Aug 18, 2014
by
smallfish
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update testcase for httplib
parent
86752a55
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
54 deletions
+55
-54
httplib_test.go
httplib/httplib_test.go
+55
-54
No files found.
httplib/httplib_test.go
View file @
7668c54d
...
@@ -11,94 +11,95 @@
...
@@ -11,94 +11,95 @@
package
httplib
package
httplib
import
(
import
(
"fmt"
"strings"
"io/ioutil"
"testing"
"testing"
)
)
func
Test
GetUrl
(
t
*
testing
.
T
)
{
func
Test
SimpleGet
(
t
*
testing
.
T
)
{
resp
,
err
:=
Get
(
"http://beego.me"
)
.
Debug
(
true
)
.
Response
()
str
,
err
:=
Get
(
"http://httpbin.org/get"
)
.
String
()
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
if
resp
.
Body
==
nil
{
t
.
Log
(
str
)
t
.
Fatal
(
"body is nil"
)
}
}
data
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
func
TestSimplePost
(
t
*
testing
.
T
)
{
defer
resp
.
Body
.
Close
()
v
:=
"smallfish"
req
:=
Post
(
"http://httpbin.org/post"
)
req
.
Param
(
"username"
,
v
)
str
,
err
:=
req
.
String
()
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
if
len
(
data
)
==
0
{
t
.
Log
(
str
)
t
.
Fatal
(
"data is no"
)
n
:=
strings
.
Index
(
str
,
v
)
if
n
==
-
1
{
t
.
Fatal
(
v
+
" not found in post"
)
}
}
}
str
,
err
:=
Get
(
"http://beego.me"
)
.
String
()
func
TestPostFile
(
t
*
testing
.
T
)
{
v
:=
"smallfish"
req
:=
Post
(
"http://httpbin.org/post"
)
req
.
Param
(
"username"
,
v
)
req
.
PostFile
(
"uploadfile"
,
"httplib_test.go"
)
str
,
err
:=
req
.
String
()
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
if
len
(
str
)
==
0
{
t
.
Log
(
str
)
t
.
Fatal
(
"has no info"
)
n
:=
strings
.
Index
(
str
,
v
)
if
n
==
-
1
{
t
.
Fatal
(
v
+
" not found in post"
)
}
}
}
}
func
ExamplePost
(
t
*
testing
.
T
)
{
func
TestWithCookie
(
t
*
testing
.
T
)
{
b
:=
Post
(
"http://beego.me/"
)
.
Debug
(
true
)
v
:=
"smallfish"
b
.
Param
(
"username"
,
"astaxie"
)
str
,
err
:=
Get
(
"http://httpbin.org/cookies/set?k1="
+
v
)
.
SetEnableCookie
(
true
)
.
String
()
b
.
Param
(
"password"
,
"hello"
)
b
.
PostFile
(
"uploadfile"
,
"httplib_test.go"
)
str
,
err
:=
b
.
String
()
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
fmt
.
Println
(
str
)
t
.
Log
(
str
)
}
str
,
err
=
Get
(
"http://httpbin.org/cookies"
)
.
SetEnableCookie
(
true
)
.
String
()
func
TestSimpleGetString
(
t
*
testing
.
T
)
{
fmt
.
Println
(
"TestSimpleGetString=========================================="
)
html
,
err
:=
Get
(
"http://httpbin.org/headers"
)
.
SetAgent
(
"beegoooooo"
)
.
String
()
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
fmt
.
Println
(
html
)
t
.
Log
(
str
)
fmt
.
Println
(
"TestSimpleGetString=========================================="
)
n
:=
strings
.
Index
(
str
,
v
)
if
n
==
-
1
{
t
.
Fatal
(
v
+
" not found in cookie"
)
}
}
}
func
Test
SimpleGetStringWithDefaultCookie
(
t
*
testing
.
T
)
{
func
Test
WithUserAgent
(
t
*
testing
.
T
)
{
fmt
.
Println
(
"TestSimpleGetStringWithDefaultCookie=========================================="
)
v
:=
"beego"
html
,
err
:=
Get
(
"http://httpbin.org/cookies/set?k1=v1"
)
.
SetEnableCookie
(
true
)
.
String
()
str
,
err
:=
Get
(
"http://httpbin.org/headers"
)
.
SetUserAgent
(
v
)
.
String
()
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
fmt
.
Println
(
html
)
t
.
Log
(
str
)
html
,
err
=
Get
(
"http://httpbin.org/cookies"
)
.
SetEnableCookie
(
true
)
.
String
(
)
n
:=
strings
.
Index
(
str
,
v
)
if
err
!=
nil
{
if
n
==
-
1
{
t
.
Fatal
(
err
)
t
.
Fatal
(
v
+
" not found in user-agent"
)
}
}
fmt
.
Println
(
html
)
fmt
.
Println
(
"TestSimpleGetStringWithDefaultCookie=========================================="
)
}
}
func
TestDefaultSetting
(
t
*
testing
.
T
)
{
func
TestWithSetting
(
t
*
testing
.
T
)
{
fmt
.
Println
(
"TestDefaultSetting=========================================="
)
v
:=
"beego"
var
def
BeegoHttpSettings
var
setting
BeegoHttpSettings
def
.
EnableCookie
=
true
setting
.
EnableCookie
=
true
//def.ShowDebug = true
setting
.
UserAgent
=
v
def
.
UserAgent
=
"UserAgent"
setting
.
Transport
=
nil
//def.ConnectTimeout = 60*time.Second
SetDefaultSetting
(
setting
)
//def.ReadWriteTimeout = 60*time.Second
def
.
Transport
=
nil
//http.DefaultTransport
SetDefaultSetting
(
def
)
html
,
err
:=
Get
(
"http://httpbin.org/headers
"
)
.
String
()
str
,
err
:=
Get
(
"http://httpbin.org/get
"
)
.
String
()
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
err
)
}
}
fmt
.
Println
(
html
)
t
.
Log
(
str
)
html
,
err
=
Get
(
"http://httpbin.org/headers"
)
.
String
(
)
n
:=
strings
.
Index
(
str
,
v
)
if
err
!=
nil
{
if
n
==
-
1
{
t
.
Fatal
(
err
)
t
.
Fatal
(
v
+
" not found in user-agent"
)
}
}
fmt
.
Println
(
html
)
fmt
.
Println
(
"TestDefaultSetting=========================================="
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment