You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
3.6 KiB

  1. // Copyright 2013 The Beego Authors. All rights reserved.
  2. // Copyright 2014 The Gogs Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package httplib
  6. import (
  7. "io/ioutil"
  8. "os"
  9. "strings"
  10. "testing"
  11. )
  12. func TestResponse(t *testing.T) {
  13. req := Get("http://httpbin.org/get")
  14. resp, err := req.Response()
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. t.Log(resp)
  19. }
  20. func TestGet(t *testing.T) {
  21. req := Get("http://httpbin.org/get")
  22. b, err := req.Bytes()
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. t.Log(b)
  27. s, err := req.String()
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. t.Log(s)
  32. if string(b) != s {
  33. t.Fatal("request data not match")
  34. }
  35. }
  36. func TestSimplePost(t *testing.T) {
  37. v := "smallfish"
  38. req := Post("http://httpbin.org/post")
  39. req.Param("username", v)
  40. str, err := req.String()
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. t.Log(str)
  45. n := strings.Index(str, v)
  46. if n == -1 {
  47. t.Fatal(v + " not found in post")
  48. }
  49. }
  50. func TestPostFile(t *testing.T) {
  51. v := "smallfish"
  52. req := Post("http://httpbin.org/post")
  53. req.Param("username", v)
  54. req.PostFile("uploadfile", "httplib_test.go")
  55. str, err := req.String()
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. t.Log(str)
  60. n := strings.Index(str, v)
  61. if n == -1 {
  62. t.Fatal(v + " not found in post")
  63. }
  64. }
  65. func TestSimplePut(t *testing.T) {
  66. str, err := Put("http://httpbin.org/put").String()
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. t.Log(str)
  71. }
  72. func TestSimpleDelete(t *testing.T) {
  73. str, err := Delete("http://httpbin.org/delete").String()
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. t.Log(str)
  78. }
  79. func TestWithCookie(t *testing.T) {
  80. v := "smallfish"
  81. str, err := Get("http://httpbin.org/cookies/set?k1=" + v).SetEnableCookie(true).String()
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. t.Log(str)
  86. str, err = Get("http://httpbin.org/cookies").SetEnableCookie(true).String()
  87. if err != nil {
  88. t.Fatal(err)
  89. }
  90. t.Log(str)
  91. n := strings.Index(str, v)
  92. if n == -1 {
  93. t.Fatal(v + " not found in cookie")
  94. }
  95. }
  96. func TestWithBasicAuth(t *testing.T) {
  97. str, err := Get("http://httpbin.org/basic-auth/user/passwd").SetBasicAuth("user", "passwd").String()
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. t.Log(str)
  102. n := strings.Index(str, "authenticated")
  103. if n == -1 {
  104. t.Fatal("authenticated not found in response")
  105. }
  106. }
  107. func TestWithUserAgent(t *testing.T) {
  108. v := "beego"
  109. str, err := Get("http://httpbin.org/headers").SetUserAgent(v).String()
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. t.Log(str)
  114. n := strings.Index(str, v)
  115. if n == -1 {
  116. t.Fatal(v + " not found in user-agent")
  117. }
  118. }
  119. func TestWithSetting(t *testing.T) {
  120. v := "beego"
  121. var setting BeegoHttpSettings
  122. setting.EnableCookie = true
  123. setting.UserAgent = v
  124. setting.Transport = nil
  125. SetDefaultSetting(setting)
  126. str, err := Get("http://httpbin.org/get").String()
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. t.Log(str)
  131. n := strings.Index(str, v)
  132. if n == -1 {
  133. t.Fatal(v + " not found in user-agent")
  134. }
  135. }
  136. func TestToJson(t *testing.T) {
  137. req := Get("http://httpbin.org/ip")
  138. resp, err := req.Response()
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. t.Log(resp)
  143. // httpbin will return http remote addr
  144. type Ip struct {
  145. Origin string `json:"origin"`
  146. }
  147. var ip Ip
  148. err = req.ToJson(&ip)
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. t.Log(ip.Origin)
  153. if n := strings.Count(ip.Origin, "."); n != 3 {
  154. t.Fatal("response is not valid ip")
  155. }
  156. }
  157. func TestToFile(t *testing.T) {
  158. f := "beego_testfile"
  159. req := Get("http://httpbin.org/ip")
  160. err := req.ToFile(f)
  161. if err != nil {
  162. t.Fatal(err)
  163. }
  164. defer os.Remove(f)
  165. b, err := ioutil.ReadFile(f)
  166. if n := strings.Index(string(b), "origin"); n == -1 {
  167. t.Fatal(err)
  168. }
  169. }