Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
7668c54d
authored
2014-08-18 15:03:34 +0800
by
smallfish
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
update testcase for httplib
1 parent
86752a55
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
54 deletions
httplib/httplib_test.go
httplib/httplib_test.go
View file @
7668c54
...
...
@@ -11,94 +11,95 @@
package
httplib
import
(
"fmt"
"io/ioutil"
"strings"
"testing"
)
func
Test
GetUrl
(
t
*
testing
.
T
)
{
resp
,
err
:=
Get
(
"http://beego.me"
)
.
Debug
(
true
)
.
Response
()
func
Test
SimpleGet
(
t
*
testing
.
T
)
{
str
,
err
:=
Get
(
"http://httpbin.org/get"
)
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
resp
.
Body
==
nil
{
t
.
Fatal
(
"body is nil"
)
}
data
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
defer
resp
.
Body
.
Close
()
t
.
Log
(
str
)
}
func
TestSimplePost
(
t
*
testing
.
T
)
{
v
:=
"smallfish"
req
:=
Post
(
"http://httpbin.org/post"
)
req
.
Param
(
"username"
,
v
)
str
,
err
:=
req
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
len
(
data
)
==
0
{
t
.
Fatal
(
"data is no"
)
t
.
Log
(
str
)
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
{
t
.
Fatal
(
err
)
}
if
len
(
str
)
==
0
{
t
.
Fatal
(
"has no info"
)
t
.
Log
(
str
)
n
:=
strings
.
Index
(
str
,
v
)
if
n
==
-
1
{
t
.
Fatal
(
v
+
" not found in post"
)
}
}
func
ExamplePost
(
t
*
testing
.
T
)
{
b
:=
Post
(
"http://beego.me/"
)
.
Debug
(
true
)
b
.
Param
(
"username"
,
"astaxie"
)
b
.
Param
(
"password"
,
"hello"
)
b
.
PostFile
(
"uploadfile"
,
"httplib_test.go"
)
str
,
err
:=
b
.
String
()
func
TestWithCookie
(
t
*
testing
.
T
)
{
v
:=
"smallfish"
str
,
err
:=
Get
(
"http://httpbin.org/cookies/set?k1="
+
v
)
.
SetEnableCookie
(
true
)
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
fmt
.
Println
(
str
)
}
func
TestSimpleGetString
(
t
*
testing
.
T
)
{
fmt
.
Println
(
"TestSimpleGetString=========================================="
)
html
,
err
:=
Get
(
"http://httpbin.org/headers"
)
.
SetAgent
(
"beegoooooo"
)
.
String
()
t
.
Log
(
str
)
str
,
err
=
Get
(
"http://httpbin.org/cookies"
)
.
SetEnableCookie
(
true
)
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
fmt
.
Println
(
html
)
fmt
.
Println
(
"TestSimpleGetString=========================================="
)
t
.
Log
(
str
)
n
:=
strings
.
Index
(
str
,
v
)
if
n
==
-
1
{
t
.
Fatal
(
v
+
" not found in cookie"
)
}
}
func
Test
SimpleGetStringWithDefaultCookie
(
t
*
testing
.
T
)
{
fmt
.
Println
(
"TestSimpleGetStringWithDefaultCookie=========================================="
)
html
,
err
:=
Get
(
"http://httpbin.org/cookies/set?k1=v1"
)
.
SetEnableCookie
(
true
)
.
String
()
func
Test
WithUserAgent
(
t
*
testing
.
T
)
{
v
:=
"beego"
str
,
err
:=
Get
(
"http://httpbin.org/headers"
)
.
SetUserAgent
(
v
)
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
fmt
.
Println
(
html
)
html
,
err
=
Get
(
"http://httpbin.org/cookies"
)
.
SetEnableCookie
(
true
)
.
String
(
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Log
(
str
)
n
:=
strings
.
Index
(
str
,
v
)
if
n
==
-
1
{
t
.
Fatal
(
v
+
" not found in user-agent"
)
}
fmt
.
Println
(
html
)
fmt
.
Println
(
"TestSimpleGetStringWithDefaultCookie=========================================="
)
}
func
TestDefaultSetting
(
t
*
testing
.
T
)
{
fmt
.
Println
(
"TestDefaultSetting=========================================="
)
var
def
BeegoHttpSettings
def
.
EnableCookie
=
true
//def.ShowDebug = true
def
.
UserAgent
=
"UserAgent"
//def.ConnectTimeout = 60*time.Second
//def.ReadWriteTimeout = 60*time.Second
def
.
Transport
=
nil
//http.DefaultTransport
SetDefaultSetting
(
def
)
func
TestWithSetting
(
t
*
testing
.
T
)
{
v
:=
"beego"
var
setting
BeegoHttpSettings
setting
.
EnableCookie
=
true
setting
.
UserAgent
=
v
setting
.
Transport
=
nil
SetDefaultSetting
(
setting
)
html
,
err
:=
Get
(
"http://httpbin.org/headers
"
)
.
String
()
str
,
err
:=
Get
(
"http://httpbin.org/get
"
)
.
String
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
fmt
.
Println
(
html
)
html
,
err
=
Get
(
"http://httpbin.org/headers"
)
.
String
(
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Log
(
str
)
n
:=
strings
.
Index
(
str
,
v
)
if
n
==
-
1
{
t
.
Fatal
(
v
+
" not found in user-agent"
)
}
fmt
.
Println
(
html
)
fmt
.
Println
(
"TestDefaultSetting=========================================="
)
}
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
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 post a comment