github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/client/http_client_test.go (about) 1 package client 2 3 import ( 4 "net/http" 5 "net/url" 6 "testing" 7 ) 8 9 func TestGetRequest(t *testing.T) { 10 client, err := NewHttpClient( 11 WithPrintRequest(), 12 WithJa3( 13 "771,4866-4867-4865-49196-49200-159-52393-52392-52394-49195-49199-158-49188-49192-107-49187-49191-103-49162-49172-57-49161-49171-51-157-156-61-60-53-47-255,0-11-10-35-16-22-23-13-43-45-51-21,29-23-30-25-24,0-1-2", 14 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36", 15 ), 16 WithHttpOptions( 17 WithUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36"), 18 ), 19 ) 20 if err != nil { 21 panic(err) 22 } 23 24 //data := []byte("你好!") 25 _url := "" 26 _url = "https://httpbin.org/ip" // FIXME 27 //_url = "https://www.baidu.com/"// FIXME 28 req, err := client.NewRequest("get", _url, nil) 29 if err != nil { 30 panic(err) 31 } 32 33 resp, err := client.Call(req) 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 log.Dbg(string(resp.Body().AsBytes())) 39 } 40 41 func TestPostRequest(t *testing.T) { 42 client, err := NewHttpClient( 43 WithPrintRequest(), 44 WithJa3( 45 "771,4866-4867-4865-49196-49200-159-52393-52392-52394-49195-49199-158-49188-49192-107-49187-49191-103-49162-49172-57-49161-49171-51-157-156-61-60-53-47-255,0-11-10-35-16-22-23-13-43-45-51-21,29-23-1035-25-24,0-1-2", 46 "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4896.140 Safari/537.36 OPR/79.0.2135.111", 47 ), 48 WithHttpOptions( 49 WithUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4896.140 Safari/537.36 OPR/79.0.2135.111"), 50 ), 51 ) 52 if err != nil { 53 panic(err) 54 } 55 56 _url := "https://go.dev/_/compile?backend=" 57 _context := `version=2&body=%2F%2F+You+can+edit+this+code!%0A%2F%2F+Click+here+and+start+typing.%0Apackage+main%0A%0Aimport+%22fmt%22%0A%0Afunc+main()+%7B%0A%09fmt.Println(%22Hello%2C+%E4%B8%96%E7%95%8C%22)%0A%7D%0A` 58 req, err := client.NewRequest("POST", _url, _context) 59 if err != nil { 60 panic(err) 61 } 62 63 req.Header().Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4896.140 Safari/537.36 OPR/79.0.2135.111") 64 req.Header().Set("Content-Type", "application/x-www-form-urlencoded") 65 req.AddCookie(&http.Cookie{Name: "ABC", Value: "U"}) 66 resp, err := client.Call(req) 67 if err != nil { 68 t.Fatal(err) 69 } 70 71 log.Dbg(resp.StatusCode, string(resp.Body().AsBytes())) 72 } 73 74 func TestOrderHeader(t *testing.T) { 75 //urladdr := "https://www.baidu.com/" 76 urladdr := "https://www.ti.com/secure-link-forward/" 77 client, err := NewHttpClient( 78 WithPrintRequest(), 79 WithJa3( 80 "771,4866-4867-4865-49196-49200-159-52393-52392-52394-49195-49199-158-49188-49192-107-49187-49191-103-49162-49172-57-49161-49171-51-157-156-61-60-53-47-255,0-11-10-35-16-22-23-13-43-45-51-21,29-23-1035-25-24,0-1-2", 81 "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4896.140 Safari/537.36 OPR/79.0.2135.111", 82 ), 83 WithHttpOptions( 84 WithUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4896.140 Safari/537.36 OPR/79.0.2135.111"), 85 ), 86 ) 87 if err != nil { 88 panic(err) 89 } 90 91 req, err := client.NewRequest("get", urladdr, nil) 92 if err != nil { 93 panic(err) 94 } 95 //set our Host header 96 u, err := url.Parse(urladdr) 97 if err != nil { 98 panic(err) 99 } 100 req.Header().Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4896.140 Safari/537.36 OPR/79.0.2135.111") 101 req.Header().Set("Accept", "*/*") 102 req.Header().Set("Host", u.Host) 103 resp, err := client.Call(req) 104 if err != nil { 105 t.Fatal(err) 106 } 107 defer resp.Body().Reset() 108 109 for v, k := range resp.Header() { 110 log.Dbg(v, k) 111 } 112 113 for _, k := range client.CookiesManager().Cookies(u) { 114 log.Dbg(k.Name, k.Value) 115 } 116 117 log.Dbg(string(resp.Body().AsBytes())) 118 }