7668c54d by smallfish

update testcase for httplib

1 parent 86752a55
...@@ -11,94 +11,95 @@ ...@@ -11,94 +11,95 @@
11 package httplib 11 package httplib
12 12
13 import ( 13 import (
14 "fmt" 14 "strings"
15 "io/ioutil"
16 "testing" 15 "testing"
17 ) 16 )
18 17
19 func TestGetUrl(t *testing.T) { 18 func TestSimpleGet(t *testing.T) {
20 resp, err := Get("http://beego.me").Debug(true).Response() 19 str, err := Get("http://httpbin.org/get").String()
21 if err != nil { 20 if err != nil {
22 t.Fatal(err) 21 t.Fatal(err)
23 } 22 }
24 if resp.Body == nil { 23 t.Log(str)
25 t.Fatal("body is nil") 24 }
26 } 25
27 data, err := ioutil.ReadAll(resp.Body) 26 func TestSimplePost(t *testing.T) {
28 defer resp.Body.Close() 27 v := "smallfish"
28 req := Post("http://httpbin.org/post")
29 req.Param("username", v)
30 str, err := req.String()
29 if err != nil { 31 if err != nil {
30 t.Fatal(err) 32 t.Fatal(err)
31 } 33 }
32 if len(data) == 0 { 34 t.Log(str)
33 t.Fatal("data is no") 35 n := strings.Index(str, v)
36 if n == -1 {
37 t.Fatal(v + " not found in post")
34 } 38 }
39 }
35 40
36 str, err := Get("http://beego.me").String() 41 func TestPostFile(t *testing.T) {
42 v := "smallfish"
43 req := Post("http://httpbin.org/post")
44 req.Param("username", v)
45 req.PostFile("uploadfile", "httplib_test.go")
46 str, err := req.String()
37 if err != nil { 47 if err != nil {
38 t.Fatal(err) 48 t.Fatal(err)
39 } 49 }
40 if len(str) == 0 { 50 t.Log(str)
41 t.Fatal("has no info") 51 n := strings.Index(str, v)
52 if n == -1 {
53 t.Fatal(v + " not found in post")
42 } 54 }
43 } 55 }
44 56
45 func ExamplePost(t *testing.T) { 57 func TestWithCookie(t *testing.T) {
46 b := Post("http://beego.me/").Debug(true) 58 v := "smallfish"
47 b.Param("username", "astaxie") 59 str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String()
48 b.Param("password", "hello")
49 b.PostFile("uploadfile", "httplib_test.go")
50 str, err := b.String()
51 if err != nil { 60 if err != nil {
52 t.Fatal(err) 61 t.Fatal(err)
53 } 62 }
54 fmt.Println(str) 63 t.Log(str)
55 } 64 str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
56
57 func TestSimpleGetString(t *testing.T) {
58 fmt.Println("TestSimpleGetString==========================================")
59 html, err := Get("http://httpbin.org/headers").SetAgent("beegoooooo").String()
60 if err != nil { 65 if err != nil {
61 t.Fatal(err) 66 t.Fatal(err)
62 } 67 }
63 fmt.Println(html) 68 t.Log(str)
64 fmt.Println("TestSimpleGetString==========================================") 69 n := strings.Index(str, v)
70 if n == -1 {
71 t.Fatal(v + " not found in cookie")
72 }
65 } 73 }
66 74
67 func TestSimpleGetStringWithDefaultCookie(t *testing.T) { 75 func TestWithUserAgent(t *testing.T) {
68 fmt.Println("TestSimpleGetStringWithDefaultCookie==========================================") 76 v := "beego"
69 html, err := Get("http://httpbin.org/cookies/set?k1=v1").SetEnableCookie(true).String() 77 str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String()
70 if err != nil { 78 if err != nil {
71 t.Fatal(err) 79 t.Fatal(err)
72 } 80 }
73 fmt.Println(html) 81 t.Log(str)
74 html, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String() 82 n := strings.Index(str, v)
75 if err != nil { 83 if n == -1 {
76 t.Fatal(err) 84 t.Fatal(v + " not found in user-agent")
77 } 85 }
78 fmt.Println(html)
79 fmt.Println("TestSimpleGetStringWithDefaultCookie==========================================")
80 } 86 }
81 87
82 func TestDefaultSetting(t *testing.T) { 88 func TestWithSetting(t *testing.T) {
83 fmt.Println("TestDefaultSetting==========================================") 89 v := "beego"
84 var def BeegoHttpSettings 90 var setting BeegoHttpSettings
85 def.EnableCookie = true 91 setting.EnableCookie = true
86 //def.ShowDebug = true 92 setting.UserAgent = v
87 def.UserAgent = "UserAgent" 93 setting.Transport = nil
88 //def.ConnectTimeout = 60*time.Second 94 SetDefaultSetting(setting)
89 //def.ReadWriteTimeout = 60*time.Second
90 def.Transport = nil //http.DefaultTransport
91 SetDefaultSetting(def)
92 95
93 html, err := Get("http://httpbin.org/headers").String() 96 str, err := Get("http://httpbin.org/get").String()
94 if err != nil { 97 if err != nil {
95 t.Fatal(err) 98 t.Fatal(err)
96 } 99 }
97 fmt.Println(html) 100 t.Log(str)
98 html, err = Get("http://httpbin.org/headers").String() 101 n := strings.Index(str, v)
99 if err != nil { 102 if n == -1 {
100 t.Fatal(err) 103 t.Fatal(v + " not found in user-agent")
101 } 104 }
102 fmt.Println(html)
103 fmt.Println("TestDefaultSetting==========================================")
104 } 105 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!