1
0

httplib_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. "time"
  21. )
  22. func TestResponse(t *testing.T) {
  23. req := Get("http://httpbin.org/get")
  24. resp, err := req.Response()
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. t.Log(resp)
  29. }
  30. func TestGet(t *testing.T) {
  31. req := Get("http://httpbin.org/get")
  32. b, err := req.Bytes()
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. t.Log(b)
  37. s, err := req.String()
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. t.Log(s)
  42. if string(b) != s {
  43. t.Fatal("request data not match")
  44. }
  45. }
  46. func TestSimplePost(t *testing.T) {
  47. v := "smallfish"
  48. req := Post("http://httpbin.org/post")
  49. req.Param("username", v)
  50. str, err := req.String()
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. t.Log(str)
  55. n := strings.Index(str, v)
  56. if n == -1 {
  57. t.Fatal(v + " not found in post")
  58. }
  59. }
  60. //func TestPostFile(t *testing.T) {
  61. // v := "smallfish"
  62. // req := Post("http://httpbin.org/post")
  63. // req.Debug(true)
  64. // req.Param("username", v)
  65. // req.PostFile("uploadfile", "httplib_test.go")
  66. // str, err := req.String()
  67. // if err != nil {
  68. // t.Fatal(err)
  69. // }
  70. // t.Log(str)
  71. // n := strings.Index(str, v)
  72. // if n == -1 {
  73. // t.Fatal(v + " not found in post")
  74. // }
  75. //}
  76. func TestSimplePut(t *testing.T) {
  77. str, err := Put("http://httpbin.org/put").String()
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. t.Log(str)
  82. }
  83. func TestSimpleDelete(t *testing.T) {
  84. str, err := Delete("http://httpbin.org/delete").String()
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. t.Log(str)
  89. }
  90. func TestSimpleDeleteParam(t *testing.T) {
  91. str, err := Delete("http://httpbin.org/delete").Param("key", "val").String()
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. t.Log(str)
  96. }
  97. func TestWithCookie(t *testing.T) {
  98. v := "smallfish"
  99. str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String()
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. t.Log(str)
  104. str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. t.Log(str)
  109. n := strings.Index(str, v)
  110. if n == -1 {
  111. t.Fatal(v + " not found in cookie")
  112. }
  113. }
  114. func TestWithBasicAuth(t *testing.T) {
  115. str, err := Get("http://httpbin.org/basic-auth/user/passwd").SetBasicAuth("user", "passwd").String()
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. t.Log(str)
  120. n := strings.Index(str, "authenticated")
  121. if n == -1 {
  122. t.Fatal("authenticated not found in response")
  123. }
  124. }
  125. func TestWithUserAgent(t *testing.T) {
  126. v := "beego"
  127. str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String()
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. t.Log(str)
  132. n := strings.Index(str, v)
  133. if n == -1 {
  134. t.Fatal(v + " not found in user-agent")
  135. }
  136. }
  137. func TestWithSetting(t *testing.T) {
  138. v := "beego"
  139. var setting BeegoHTTPSettings
  140. setting.EnableCookie = true
  141. setting.UserAgent = v
  142. setting.Transport = nil
  143. setting.ReadWriteTimeout = 5 * time.Second
  144. SetDefaultSetting(setting)
  145. str, err := Get("http://httpbin.org/get").String()
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. t.Log(str)
  150. n := strings.Index(str, v)
  151. if n == -1 {
  152. t.Fatal(v + " not found in user-agent")
  153. }
  154. }
  155. func TestToJson(t *testing.T) {
  156. req := Get("http://httpbin.org/ip")
  157. resp, err := req.Response()
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. t.Log(resp)
  162. // httpbin will return http remote addr
  163. type IP struct {
  164. Origin string `json:"origin"`
  165. }
  166. var ip IP
  167. err = req.ToJSON(&ip)
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. t.Log(ip.Origin)
  172. if n := strings.Count(ip.Origin, "."); n != 3 {
  173. t.Fatal("response is not valid ip")
  174. }
  175. }
  176. func TestToFile(t *testing.T) {
  177. f := "beego_testfile"
  178. req := Get("http://httpbin.org/ip")
  179. err := req.ToFile(f)
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. defer os.Remove(f)
  184. b, err := ioutil.ReadFile(f)
  185. if n := strings.Index(string(b), "origin"); n == -1 {
  186. t.Fatal(err)
  187. }
  188. }
  189. func TestHeader(t *testing.T) {
  190. req := Get("http://httpbin.org/headers")
  191. 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")
  192. str, err := req.String()
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. t.Log(str)
  197. }