github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/net/http/http_test.go (about) 1 package http 2 3 import ( 4 "net/http" 5 "testing" 6 ) 7 8 func TestNewHTTPClientCert(t *testing.T) { 9 certFile := "/home/champly/openssl/cert.pem" 10 keyFile := "/home/champly/openssl/key.pem" 11 caFile := "/home/champly/openssl/ca.pem" 12 client, err := NewHTTPClientCert(certFile, keyFile, caFile) 13 if err != nil { 14 t.Error(err) 15 } 16 if client == nil { 17 t.Error("test fail") 18 } 19 20 certFile = "/home/champly/openssl/err_cert.pem" 21 keyFile = "/home/champly/openssl/err_key.pem" 22 caFile = "/home/champly/openssl/err_ca.pem" 23 _, err = NewHTTPClientCert(certFile, keyFile, caFile) 24 if err == nil { 25 t.Error("test fail") 26 } 27 } 28 29 func TestGet(t *testing.T) { 30 client := NewHTTPClient() 31 content, status, err := client.Get("http://www.baidu.com", nil...) 32 if err != nil { 33 t.Errorf("Get error: %v", err) 34 } 35 if status != http.StatusOK { 36 t.Errorf("Get error status:%d", status) 37 } 38 if content == "" { 39 t.Error("Get error with not content") 40 } 41 42 content, status, err = client.Get("http://www.google.com", nil...) 43 if err == nil { 44 t.Errorf("Get error: %v", err) 45 } 46 if status != 0 { 47 t.Errorf("Get error status:%d", status) 48 } 49 if content != "" { 50 t.Error("Get error with not content") 51 } 52 53 content, status, err = client.Get("https://github.com/testhttp", nil...) 54 if err != nil { 55 t.Errorf("Get error: %v", err) 56 } 57 if status != http.StatusNotFound { 58 t.Errorf("Get error status:%d", status) 59 } 60 if content == "" { 61 t.Error("Get error with not content") 62 } 63 } 64 65 func TestPost(t *testing.T) { 66 client := NewHTTPClient() 67 content, status, err := client.Post("http://www.baidu.com", "name=bob", "UTF-8") 68 if err != nil { 69 t.Errorf("Post error: %v", err) 70 } 71 if status != http.StatusOK { 72 t.Errorf("Post error status:%d", status) 73 } 74 if content == "" { 75 t.Error("Post error with not content") 76 } 77 78 content, status, err = client.Post("https://github.com/testhttp", "name=bob", "UTF-8") 79 if err != nil { 80 t.Errorf("Post error: %v", err) 81 } 82 if status != http.StatusNotFound { 83 t.Errorf("Post error status:%d", status) 84 } 85 if content == "" { 86 t.Error("Post error with not content") 87 } 88 89 content, status, err = client.Post("http://www.google.com", "name=bob", "UTF-8") 90 if err == nil { 91 t.Errorf("Post error: %v", err) 92 } 93 if status != 0 { 94 t.Errorf("Post error status:%d", status) 95 } 96 if content != "" { 97 t.Error("Post error with not content") 98 } 99 100 content, status, err = client.Post("https://github.com/testhttp", "", "UTF-8") 101 if err != nil { 102 t.Errorf("Post error: %v", err) 103 } 104 if status != http.StatusNotFound { 105 t.Errorf("Post error status:%d", status) 106 } 107 if content == "" { 108 t.Error("Post error with not content") 109 } 110 } 111 112 func TestRequest(t *testing.T) { 113 client := NewHTTPClient() 114 url := "https://github.com" 115 method := "" 116 params := "" 117 charset := "UTF-8" 118 header := make(map[string]string) 119 content, status, err := client.Request(method, url, params, charset, header) 120 if err != nil { 121 t.Errorf("Request error: %v", err) 122 } 123 if status != http.StatusOK { 124 t.Errorf("Request error status:%d", status) 125 } 126 if content == "" { 127 t.Error("Request error with not content") 128 } 129 130 url = "http://www.google.com" 131 method = "" 132 params = "" 133 charset = "UTF-8" 134 header = make(map[string]string) 135 content, status, err = client.Request(method, url, params, charset, header) 136 if err == nil { 137 t.Errorf("Request error: %v", err) 138 } 139 if status != 0 { 140 t.Errorf("Request error status:%d", status) 141 } 142 if content != "" { 143 t.Error("Request error with not content") 144 } 145 146 url = "https://github.com/testhttp" 147 method = "" 148 params = "" 149 charset = "UTF-8" 150 header = make(map[string]string) 151 content, status, err = client.Request(method, url, params, charset, header) 152 if err != nil { 153 t.Error("test error") 154 } 155 if status != http.StatusNotFound { 156 t.Errorf("Request error status:%d", status) 157 } 158 if content == "" { 159 t.Error("Request error with not content") 160 } 161 162 url = "https://github.com" 163 method = "qxnw/lib4go" 164 params = "" 165 charset = "UTF-8" 166 header = make(map[string]string) 167 content, status, err = client.Request(method, url, params, charset, header) 168 if err == nil { 169 t.Error("test error") 170 } 171 if status != 0 { 172 t.Errorf("Request error status:%d", status) 173 } 174 if content != "" { 175 t.Error("Request error with not content") 176 } 177 } 178 179 func TestNewHTTPClientProxy(t *testing.T) { 180 proxy := "192.168.0.100:6666" 181 client := NewHTTPClientProxy(proxy) 182 content, status, err := client.Get("http://www.baidu.com", "UTF-8") 183 if err == nil { 184 t.Errorf("Post error: %v", err) 185 } 186 187 proxy = "58.222.254.11:3128" 188 client = NewHTTPClientProxy(proxy) 189 content, status, err = client.Get("http://www.baidu.com", "UTF-8") 190 if err != nil { 191 t.Errorf("Post error: %v", err) 192 } 193 if status != http.StatusOK { 194 t.Errorf("Post error status:%d", status) 195 } 196 if content == "" { 197 t.Error("Post error with not content") 198 } 199 } 200 201 func TestDownload(t *testing.T) { 202 client := NewHTTPClient() 203 method := "" 204 url := "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" 205 params := "" 206 header := make(map[string]string) 207 body, status, err := client.Download(method, url, params, header) 208 if err != nil { 209 t.Errorf("Download fail %v", err) 210 } 211 if status != http.StatusOK { 212 t.Errorf("Download fail status:%d", status) 213 } 214 if body == nil { 215 t.Error("Download fail") 216 } 217 218 method = "" 219 url = "https://ss0.bdstatic.com/5aV1bCf/static/superman/img/logo/bd_logo1_31bdc765.png" 220 params = "" 221 header = make(map[string]string) 222 body, status, err = client.Download(method, url, params, header) 223 if err != nil { 224 t.Errorf("Download fail %v", err) 225 } 226 if status != http.StatusOK { 227 t.Errorf("Download fail status:%d", status) 228 } 229 if body == nil { 230 t.Error("Download fail") 231 } 232 233 method = "download" 234 url = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" 235 params = "" 236 header = make(map[string]string) 237 body, status, err = client.Download(method, url, params, header) 238 if err != nil { 239 t.Errorf("Download fail %v", err) 240 } 241 if status != 405 { 242 t.Errorf("Download fail status:%d", status) 243 } 244 if body == nil { 245 t.Error("Download fail") 246 } 247 } 248 249 func TestSave(t *testing.T) { 250 client := NewHTTPClient() 251 method := "" 252 url := "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" 253 params := "" 254 header := make(map[string]string) 255 path := "/home/champly/pictue.png" 256 status, err := client.Save(method, url, params, header, path) 257 if err != nil { 258 t.Errorf("Save fail %v", err) 259 } 260 if status != http.StatusOK { 261 t.Errorf("Save fail status:%d", status) 262 } 263 264 method = "" 265 url = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" 266 params = "" 267 header = make(map[string]string) 268 path = "/home/champly" 269 status, err = client.Save(method, url, params, header, path) 270 if err == nil { 271 t.Errorf("Save fail %v", err) 272 } 273 if status != http.StatusOK { 274 t.Errorf("Save fail status:%d", status) 275 } 276 277 method = "save" 278 url = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" 279 params = "" 280 header = make(map[string]string) 281 path = "/home/champly/pictue.png" 282 status, err = client.Save(method, url, params, header, path) 283 if err != nil { 284 t.Errorf("Save fail %v", err) 285 } 286 if status != 405 { 287 t.Errorf("Save fail status:%d", status) 288 } 289 }