httplib_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package httplib
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "strings"
  19. "testing"
  20. )
  21. func TestResponse(t *testing.T) {
  22. req := Get("http://httpbin.org/get")
  23. resp, err := req.Response()
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. t.Log(resp)
  28. }
  29. func TestGet(t *testing.T) {
  30. req := Get("http://httpbin.org/get")
  31. b, err := req.Bytes()
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. t.Log(b)
  36. s, err := req.String()
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. t.Log(s)
  41. if string(b) != s {
  42. t.Fatal("request data not match")
  43. }
  44. }
  45. func TestSimplePost(t *testing.T) {
  46. v := "smallfish"
  47. req := Post("http://httpbin.org/post")
  48. req.Param("username", v)
  49. str, err := req.String()
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. t.Log(str)
  54. n := strings.Index(str, v)
  55. if n == -1 {
  56. t.Fatal(v + " not found in post")
  57. }
  58. }
  59. //func TestPostFile(t *testing.T) {
  60. // v := "smallfish"
  61. // req := Post("http://httpbin.org/post")
  62. // req.Debug(true)
  63. // req.Param("username", v)
  64. // req.PostFile("uploadfile", "httplib_test.go")
  65. // str, err := req.String()
  66. // if err != nil {
  67. // t.Fatal(err)
  68. // }
  69. // t.Log(str)
  70. // n := strings.Index(str, v)
  71. // if n == -1 {
  72. // t.Fatal(v + " not found in post")
  73. // }
  74. //}
  75. func TestSimplePut(t *testing.T) {
  76. str, err := Put("http://httpbin.org/put").String()
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. t.Log(str)
  81. }
  82. func TestSimpleDelete(t *testing.T) {
  83. str, err := Delete("http://httpbin.org/delete").String()
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. t.Log(str)
  88. }
  89. func TestWithCookie(t *testing.T) {
  90. v := "smallfish"
  91. str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String()
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. t.Log(str)
  96. str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. t.Log(str)
  101. n := strings.Index(str, v)
  102. if n == -1 {
  103. t.Fatal(v + " not found in cookie")
  104. }
  105. }
  106. func TestWithBasicAuth(t *testing.T) {
  107. str, err := Get("http://httpbin.org/basic-auth/user/passwd").SetBasicAuth("user", "passwd").String()
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. t.Log(str)
  112. n := strings.Index(str, "authenticated")
  113. if n == -1 {
  114. t.Fatal("authenticated not found in response")
  115. }
  116. }
  117. func TestWithUserAgent(t *testing.T) {
  118. v := "beego"
  119. str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String()
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. t.Log(str)
  124. n := strings.Index(str, v)
  125. if n == -1 {
  126. t.Fatal(v + " not found in user-agent")
  127. }
  128. }
  129. func TestWithSetting(t *testing.T) {
  130. v := "beego"
  131. var setting BeegoHttpSettings
  132. setting.EnableCookie = true
  133. setting.UserAgent = v
  134. setting.Transport = nil
  135. SetDefaultSetting(setting)
  136. str, err := Get("http://httpbin.org/get").String()
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. t.Log(str)
  141. n := strings.Index(str, v)
  142. if n == -1 {
  143. t.Fatal(v + " not found in user-agent")
  144. }
  145. }
  146. func TestToJson(t *testing.T) {
  147. req := Get("http://httpbin.org/ip")
  148. resp, err := req.Response()
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. t.Log(resp)
  153. // httpbin will return http remote addr
  154. type Ip struct {
  155. Origin string `json:"origin"`
  156. }
  157. var ip Ip
  158. err = req.ToJson(&ip)
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. t.Log(ip.Origin)
  163. if n := strings.Count(ip.Origin, "."); n != 3 {
  164. t.Fatal("response is not valid ip")
  165. }
  166. }
  167. func TestToFile(t *testing.T) {
  168. f := "beego_testfile"
  169. req := Get("http://httpbin.org/ip")
  170. err := req.ToFile(f)
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. defer os.Remove(f)
  175. b, err := ioutil.ReadFile(f)
  176. if n := strings.Index(string(b), "origin"); n == -1 {
  177. t.Fatal(err)
  178. }
  179. }
  180. func TestHeader(t *testing.T) {
  181. req := Get("http://httpbin.org/headers")
  182. req.Header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36")
  183. str, err := req.String()
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. t.Log(str)
  188. }