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