httplib_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 TestWithCookie(t *testing.T) {
  91. v := "smallfish"
  92. str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String()
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. t.Log(str)
  97. str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. t.Log(str)
  102. n := strings.Index(str, v)
  103. if n == -1 {
  104. t.Fatal(v + " not found in cookie")
  105. }
  106. }
  107. func TestWithBasicAuth(t *testing.T) {
  108. str, err := Get("http://httpbin.org/basic-auth/user/passwd").SetBasicAuth("user", "passwd").String()
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. t.Log(str)
  113. n := strings.Index(str, "authenticated")
  114. if n == -1 {
  115. t.Fatal("authenticated not found in response")
  116. }
  117. }
  118. func TestWithUserAgent(t *testing.T) {
  119. v := "beego"
  120. str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String()
  121. if err != nil {
  122. t.Fatal(err)
  123. }
  124. t.Log(str)
  125. n := strings.Index(str, v)
  126. if n == -1 {
  127. t.Fatal(v + " not found in user-agent")
  128. }
  129. }
  130. func TestWithSetting(t *testing.T) {
  131. v := "beego"
  132. var setting BeegoHTTPSettings
  133. setting.EnableCookie = true
  134. setting.UserAgent = v
  135. setting.Transport = nil
  136. setting.ReadWriteTimeout = 5 * time.Second
  137. SetDefaultSetting(setting)
  138. str, err := Get("http://httpbin.org/get").String()
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. t.Log(str)
  143. n := strings.Index(str, v)
  144. if n == -1 {
  145. t.Fatal(v + " not found in user-agent")
  146. }
  147. }
  148. func TestToJson(t *testing.T) {
  149. req := Get("http://httpbin.org/ip")
  150. resp, err := req.Response()
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. t.Log(resp)
  155. // httpbin will return http remote addr
  156. type IP struct {
  157. Origin string `json:"origin"`
  158. }
  159. var ip IP
  160. err = req.ToJSON(&ip)
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. t.Log(ip.Origin)
  165. if n := strings.Count(ip.Origin, "."); n != 3 {
  166. t.Fatal("response is not valid ip")
  167. }
  168. }
  169. func TestToFile(t *testing.T) {
  170. f := "beego_testfile"
  171. req := Get("http://httpbin.org/ip")
  172. err := req.ToFile(f)
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. defer os.Remove(f)
  177. b, err := ioutil.ReadFile(f)
  178. if n := strings.Index(string(b), "origin"); n == -1 {
  179. t.Fatal(err)
  180. }
  181. }
  182. func TestHeader(t *testing.T) {
  183. req := Get("http://httpbin.org/headers")
  184. 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")
  185. str, err := req.String()
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. t.Log(str)
  190. }