Best Testkube code snippet using client.RoundTrip
socks_test.go
Source:socks_test.go
1package gost2import (3 "bytes"4 "crypto/rand"5 "fmt"6 "net"7 "net/http/httptest"8 "net/url"9 "testing"10 "time"11)12var socks5ProxyTests = []struct {13 cliUser *url.Userinfo14 srvUsers []*url.Userinfo15 pass bool16}{17 {nil, nil, true},18 {nil, []*url.Userinfo{url.User("admin")}, false},19 {nil, []*url.Userinfo{url.UserPassword("", "123456")}, false},20 {url.User("admin"), []*url.Userinfo{url.User("test")}, false},21 {url.User("admin"), []*url.Userinfo{url.UserPassword("admin", "123456")}, false},22 {url.User("admin"), []*url.Userinfo{url.User("admin")}, true},23 {url.User("admin"), []*url.Userinfo{url.UserPassword("admin", "")}, true},24 {url.UserPassword("admin", "123456"), nil, true},25 {url.UserPassword("admin", "123456"), []*url.Userinfo{url.User("admin")}, true},26 {url.UserPassword("admin", "123456"), []*url.Userinfo{url.UserPassword("", "123456")}, false},27 {url.UserPassword("", "123456"), []*url.Userinfo{url.UserPassword("", "123456")}, true},28 {url.UserPassword("admin", "123456"), []*url.Userinfo{url.UserPassword("admin", "123456")}, true},29 {url.UserPassword("admin", "123456"), []*url.Userinfo{url.UserPassword("user", "pass"), url.UserPassword("admin", "123456")}, true},30}31func socks5ProxyRoundtrip(targetURL string, data []byte, clientInfo *url.Userinfo, serverInfo []*url.Userinfo) error {32 ln, err := TCPListener("")33 if err != nil {34 return err35 }36 client := &Client{37 Connector: SOCKS5Connector(clientInfo),38 Transporter: TCPTransporter(),39 }40 server := &Server{41 Handler: SOCKS5Handler(UsersHandlerOption(serverInfo...)),42 Listener: ln,43 }44 go server.Run()45 defer server.Close()46 return proxyRoundtrip(client, server, targetURL, data)47}48func TestSOCKS5Proxy(t *testing.T) {49 httpSrv := httptest.NewServer(httpTestHandler)50 defer httpSrv.Close()51 sendData := make([]byte, 128)52 rand.Read(sendData)53 for i, tc := range socks5ProxyTests {54 err := socks5ProxyRoundtrip(httpSrv.URL, sendData,55 tc.cliUser,56 tc.srvUsers,57 )58 if err == nil {59 if !tc.pass {60 t.Errorf("#%d should failed", i)61 }62 } else {63 // t.Logf("#%d %v", i, err)64 if tc.pass {65 t.Errorf("#%d got error: %v", i, err)66 }67 }68 }69}70func BenchmarkSOCKS5Proxy(b *testing.B) {71 httpSrv := httptest.NewServer(httpTestHandler)72 defer httpSrv.Close()73 sendData := make([]byte, 128)74 rand.Read(sendData)75 ln, err := TCPListener("")76 if err != nil {77 b.Error(err)78 }79 client := &Client{80 Connector: SOCKS5Connector(url.UserPassword("admin", "123456")),81 Transporter: TCPTransporter(),82 }83 server := &Server{84 Handler: SOCKS5Handler(UsersHandlerOption(url.UserPassword("admin", "123456"))),85 Listener: ln,86 }87 go server.Run()88 defer server.Close()89 for i := 0; i < b.N; i++ {90 if err := proxyRoundtrip(client, server, httpSrv.URL, sendData); err != nil {91 b.Error(err)92 }93 }94}95func BenchmarkSOCKS5ProxyParallel(b *testing.B) {96 httpSrv := httptest.NewServer(httpTestHandler)97 defer httpSrv.Close()98 sendData := make([]byte, 128)99 rand.Read(sendData)100 ln, err := TCPListener("")101 if err != nil {102 b.Error(err)103 }104 client := &Client{105 Connector: SOCKS5Connector(url.UserPassword("admin", "123456")),106 Transporter: TCPTransporter(),107 }108 server := &Server{109 Handler: SOCKS5Handler(UsersHandlerOption(url.UserPassword("admin", "123456"))),110 Listener: ln,111 }112 go server.Run()113 defer server.Close()114 b.RunParallel(func(pb *testing.PB) {115 for pb.Next() {116 if err := proxyRoundtrip(client, server, httpSrv.URL, sendData); err != nil {117 b.Error(err)118 }119 }120 })121}122func socks4ProxyRoundtrip(targetURL string, data []byte) error {123 ln, err := TCPListener("")124 if err != nil {125 return err126 }127 client := &Client{128 Connector: SOCKS4Connector(),129 Transporter: TCPTransporter(),130 }131 server := &Server{132 Listener: ln,133 Handler: SOCKS4Handler(),134 }135 go server.Run()136 defer server.Close()137 return proxyRoundtrip(client, server, targetURL, data)138}139func TestSOCKS4Proxy(t *testing.T) {140 httpSrv := httptest.NewServer(httpTestHandler)141 defer httpSrv.Close()142 sendData := make([]byte, 128)143 rand.Read(sendData)144 err := socks4ProxyRoundtrip(httpSrv.URL, sendData)145 // t.Logf("#%d %v", i, err)146 if err != nil {147 t.Errorf("got error: %v", err)148 }149}150func BenchmarkSOCKS4Proxy(b *testing.B) {151 httpSrv := httptest.NewServer(httpTestHandler)152 defer httpSrv.Close()153 sendData := make([]byte, 128)154 rand.Read(sendData)155 ln, err := TCPListener("")156 if err != nil {157 b.Error(err)158 }159 client := &Client{160 Connector: SOCKS4Connector(),161 Transporter: TCPTransporter(),162 }163 server := &Server{164 Listener: ln,165 Handler: SOCKS4Handler(),166 }167 go server.Run()168 defer server.Close()169 for i := 0; i < b.N; i++ {170 if err := proxyRoundtrip(client, server, httpSrv.URL, sendData); err != nil {171 b.Error(err)172 }173 }174}175func BenchmarkSOCKS4ProxyParallel(b *testing.B) {176 httpSrv := httptest.NewServer(httpTestHandler)177 defer httpSrv.Close()178 sendData := make([]byte, 128)179 rand.Read(sendData)180 ln, err := TCPListener("")181 if err != nil {182 b.Error(err)183 }184 client := &Client{185 Connector: SOCKS4Connector(),186 Transporter: TCPTransporter(),187 }188 server := &Server{189 Listener: ln,190 Handler: SOCKS4Handler(),191 }192 go server.Run()193 defer server.Close()194 b.RunParallel(func(pb *testing.PB) {195 for pb.Next() {196 if err := proxyRoundtrip(client, server, httpSrv.URL, sendData); err != nil {197 b.Error(err)198 }199 }200 })201}202func socks4aProxyRoundtrip(targetURL string, data []byte) error {203 ln, err := TCPListener("")204 if err != nil {205 return err206 }207 client := &Client{208 Connector: SOCKS4AConnector(),209 Transporter: TCPTransporter(),210 }211 server := &Server{212 Listener: ln,213 Handler: SOCKS4Handler(),214 }215 go server.Run()216 defer server.Close()217 return proxyRoundtrip(client, server, targetURL, data)218}219func TestSOCKS4AProxy(t *testing.T) {220 httpSrv := httptest.NewServer(httpTestHandler)221 defer httpSrv.Close()222 sendData := make([]byte, 128)223 rand.Read(sendData)224 err := socks4aProxyRoundtrip(httpSrv.URL, sendData)225 // t.Logf("#%d %v", i, err)226 if err != nil {227 t.Errorf("got error: %v", err)228 }229}230func BenchmarkSOCKS4AProxy(b *testing.B) {231 httpSrv := httptest.NewServer(httpTestHandler)232 defer httpSrv.Close()233 sendData := make([]byte, 128)234 rand.Read(sendData)235 ln, err := TCPListener("")236 if err != nil {237 b.Error(err)238 }239 client := &Client{240 Connector: SOCKS4AConnector(),241 Transporter: TCPTransporter(),242 }243 server := &Server{244 Listener: ln,245 Handler: SOCKS4Handler(),246 }247 go server.Run()248 defer server.Close()249 for i := 0; i < b.N; i++ {250 if err := proxyRoundtrip(client, server, httpSrv.URL, sendData); err != nil {251 b.Error(err)252 }253 }254}255func BenchmarkSOCKS4AProxyParallel(b *testing.B) {256 httpSrv := httptest.NewServer(httpTestHandler)257 defer httpSrv.Close()258 sendData := make([]byte, 128)259 rand.Read(sendData)260 ln, err := TCPListener("")261 if err != nil {262 b.Error(err)263 }264 client := &Client{265 Connector: SOCKS4AConnector(),266 Transporter: TCPTransporter(),267 }268 server := &Server{269 Listener: ln,270 Handler: SOCKS4Handler(),271 }272 go server.Run()273 defer server.Close()274 b.RunParallel(func(pb *testing.PB) {275 for pb.Next() {276 if err := proxyRoundtrip(client, server, httpSrv.URL, sendData); err != nil {277 b.Error(err)278 }279 }280 })281}282func socks5BindRoundtrip(t *testing.T, targetURL string, data []byte) (err error) {283 ln, err := TCPListener("")284 if err != nil {285 return286 }287 client := &Client{288 Connector: SOCKS5BindConnector(url.UserPassword("admin", "123456")),289 Transporter: TCPTransporter(),290 }291 server := &Server{292 Handler: SOCKS5Handler(UsersHandlerOption(url.UserPassword("admin", "123456"))),293 Listener: ln,294 }295 go server.Run()296 defer server.Close()297 conn, err := proxyConn(client, server)298 if err != nil {299 return300 }301 defer conn.Close()302 conn, err = client.Connect(conn, "")303 if err != nil {304 return305 }306 cc, err := net.Dial("tcp", conn.LocalAddr().String())307 if err != nil {308 return309 }310 defer cc.Close()311 if err = conn.(*socks5BindConn).Handshake(); err != nil {312 return313 }314 u, err := url.Parse(targetURL)315 if err != nil {316 return317 }318 hc, err := net.Dial("tcp", u.Host)319 if err != nil {320 return321 }322 go transport(hc, conn)323 return httpRoundtrip(cc, targetURL, data)324}325func TestSOCKS5Bind(t *testing.T) {326 httpSrv := httptest.NewServer(httpTestHandler)327 defer httpSrv.Close()328 sendData := make([]byte, 128)329 rand.Read(sendData)330 if err := socks5BindRoundtrip(t, httpSrv.URL, sendData); err != nil {331 t.Errorf("got error: %v", err)332 }333}334func socks5MuxBindRoundtrip(t *testing.T, targetURL string, data []byte) (err error) {335 ln, err := TCPListener("")336 if err != nil {337 return338 }339 l, err := net.Listen("tcp", "")340 if err != nil {341 return err342 }343 bindAddr := l.Addr().String()344 l.Close()345 client := &Client{346 Connector: Socks5MuxBindConnector(),347 Transporter: SOCKS5MuxBindTransporter(bindAddr),348 }349 server := &Server{350 Handler: SOCKS5Handler(UsersHandlerOption(url.UserPassword("admin", "123456"))),351 Listener: ln,352 }353 go server.Run()354 defer server.Close()355 return muxBindRoundtrip(client, server, bindAddr, targetURL, data)356}357func muxBindRoundtrip(client *Client, server *Server, bindAddr, targetURL string, data []byte) (err error) {358 cn, err := client.Dial(server.Addr().String())359 if err != nil {360 return err361 }362 conn, err := client.Handshake(cn,363 AddrHandshakeOption(server.Addr().String()),364 UserHandshakeOption(url.UserPassword("admin", "123456")),365 )366 if err != nil {367 cn.Close()368 return err369 }370 defer conn.Close()371 cc, err := net.Dial("tcp", bindAddr)372 if err != nil {373 return374 }375 defer cc.Close()376 conn, err = client.Connect(conn, "")377 if err != nil {378 return379 }380 u, err := url.Parse(targetURL)381 if err != nil {382 return383 }384 hc, err := net.Dial("tcp", u.Host)385 if err != nil {386 return387 }388 defer hc.Close()389 go transport(hc, conn)390 return httpRoundtrip(cc, targetURL, data)391}392func TestSOCKS5MuxBind(t *testing.T) {393 httpSrv := httptest.NewServer(httpTestHandler)394 defer httpSrv.Close()395 sendData := make([]byte, 128)396 rand.Read(sendData)397 if err := socks5MuxBindRoundtrip(t, httpSrv.URL, sendData); err != nil {398 t.Errorf("got error: %v", err)399 }400}401func BenchmarkSOCKS5MuxBind(b *testing.B) {402 httpSrv := httptest.NewServer(httpTestHandler)403 defer httpSrv.Close()404 sendData := make([]byte, 128)405 rand.Read(sendData)406 ln, err := TCPListener("")407 if err != nil {408 b.Error(err)409 }410 l, err := net.Listen("tcp", "")411 if err != nil {412 b.Error(err)413 }414 bindAddr := l.Addr().String()415 l.Close()416 client := &Client{417 Connector: Socks5MuxBindConnector(),418 Transporter: SOCKS5MuxBindTransporter(bindAddr),419 }420 server := &Server{421 Handler: SOCKS5Handler(UsersHandlerOption(url.UserPassword("admin", "123456"))),422 Listener: ln,423 }424 go server.Run()425 defer server.Close()426 for i := 0; i < b.N; i++ {427 if err := muxBindRoundtrip(client, server, bindAddr, httpSrv.URL, sendData); err != nil {428 b.Error(err)429 }430 }431}432func socks5UDPRoundtrip(t *testing.T, host string, data []byte) (err error) {433 ln, err := TCPListener("")434 if err != nil {435 return436 }437 client := &Client{438 Connector: SOCKS5UDPConnector(url.UserPassword("admin", "123456")),439 Transporter: TCPTransporter(),440 }441 server := &Server{442 Handler: SOCKS5Handler(UsersHandlerOption(url.UserPassword("admin", "123456"))),443 Listener: ln,444 }445 go server.Run()446 defer server.Close()447 return udpRoundtrip(t, client, server, host, data)448}449func TestSOCKS5UDP(t *testing.T) {450 udpSrv := newUDPTestServer(udpTestHandler)451 udpSrv.Start()452 defer udpSrv.Close()453 sendData := make([]byte, 128)454 rand.Read(sendData)455 if err := socks5UDPRoundtrip(t, udpSrv.Addr(), sendData); err != nil {456 t.Errorf("got error: %v", err)457 }458}459// TODO: fix a probability of timeout.460func BenchmarkSOCKS5UDP(b *testing.B) {461 udpSrv := newUDPTestServer(udpTestHandler)462 udpSrv.Start()463 defer udpSrv.Close()464 sendData := make([]byte, 128)465 rand.Read(sendData)466 ln, err := TCPListener("")467 if err != nil {468 b.Error(err)469 }470 client := &Client{471 Connector: SOCKS5UDPConnector(url.UserPassword("admin", "123456")),472 Transporter: TCPTransporter(),473 }474 server := &Server{475 Handler: SOCKS5Handler(UsersHandlerOption(url.UserPassword("admin", "123456"))),476 Listener: ln,477 }478 go server.Run()479 defer server.Close()480 for i := 0; i < b.N; i++ {481 if err := udpRoundtrip(b, client, server, udpSrv.Addr(), sendData); err != nil {482 b.Error(err)483 }484 }485}486func BenchmarkSOCKS5UDPSingleConn(b *testing.B) {487 udpSrv := newUDPTestServer(udpTestHandler)488 udpSrv.Start()489 defer udpSrv.Close()490 sendData := make([]byte, 128)491 rand.Read(sendData)492 ln, err := TCPListener("")493 if err != nil {494 b.Error(err)495 }496 client := &Client{497 Connector: SOCKS5UDPConnector(url.UserPassword("admin", "123456")),498 Transporter: TCPTransporter(),499 }500 server := &Server{501 Handler: SOCKS5Handler(UsersHandlerOption(url.UserPassword("admin", "123456"))),502 Listener: ln,503 }504 go server.Run()505 defer server.Close()506 conn, err := proxyConn(client, server)507 if err != nil {508 b.Error(err)509 }510 defer conn.Close()511 conn, err = client.Connect(conn, udpSrv.Addr())512 if err != nil {513 b.Error(err)514 }515 roundtrip := func(conn net.Conn, data []byte) error {516 conn.SetDeadline(time.Now().Add(1 * time.Second))517 defer conn.SetDeadline(time.Time{})518 if _, err = conn.Write(data); err != nil {519 return err520 }521 recv := make([]byte, len(data))522 if _, err = conn.Read(recv); err != nil {523 return err524 }525 if !bytes.Equal(data, recv) {526 return fmt.Errorf("data not equal")527 }528 return nil529 }530 for i := 0; i < b.N; i++ {531 if err := roundtrip(conn, sendData); err != nil {532 b.Error(err)533 }534 }535}536func socks5UDPTunRoundtrip(t *testing.T, host string, data []byte) (err error) {537 ln, err := TCPListener("")538 if err != nil {539 return540 }541 client := &Client{542 Connector: SOCKS5UDPTunConnector(url.UserPassword("admin", "123456")),543 Transporter: TCPTransporter(),544 }545 server := &Server{546 Handler: SOCKS5Handler(UsersHandlerOption(url.UserPassword("admin", "123456"))),547 Listener: ln,548 }549 go server.Run()550 defer server.Close()551 return udpRoundtrip(t, client, server, host, data)552}553func TestSOCKS5UDPTun(t *testing.T) {554 udpSrv := newUDPTestServer(udpTestHandler)555 udpSrv.Start()556 defer udpSrv.Close()557 sendData := make([]byte, 128)558 rand.Read(sendData)559 if err := socks5UDPTunRoundtrip(t, udpSrv.Addr(), sendData); err != nil {560 t.Errorf("got error: %v", err)561 }562}563func BenchmarkSOCKS5UDPTun(b *testing.B) {564 udpSrv := newUDPTestServer(udpTestHandler)565 udpSrv.Start()566 defer udpSrv.Close()567 sendData := make([]byte, 128)568 rand.Read(sendData)569 ln, err := TCPListener("")570 if err != nil {571 b.Error(err)572 }573 client := &Client{574 Connector: SOCKS5UDPTunConnector(url.UserPassword("admin", "123456")),575 Transporter: TCPTransporter(),576 }577 server := &Server{578 Handler: SOCKS5Handler(UsersHandlerOption(url.UserPassword("admin", "123456"))),579 Listener: ln,580 }581 go server.Run()582 defer server.Close()583 for i := 0; i < b.N; i++ {584 if err := udpRoundtrip(b, client, server, udpSrv.Addr(), sendData); err != nil {585 b.Error(err)586 }587 }588}589func BenchmarkSOCKS5UDPTunSingleConn(b *testing.B) {590 udpSrv := newUDPTestServer(udpTestHandler)591 udpSrv.Start()592 defer udpSrv.Close()593 sendData := make([]byte, 128)594 rand.Read(sendData)595 ln, err := TCPListener("")596 if err != nil {597 b.Error(err)598 }599 client := &Client{600 Connector: SOCKS5UDPTunConnector(url.UserPassword("admin", "123456")),601 Transporter: TCPTransporter(),602 }603 server := &Server{604 Handler: SOCKS5Handler(UsersHandlerOption(url.UserPassword("admin", "123456"))),605 Listener: ln,606 }607 go server.Run()608 defer server.Close()609 conn, err := proxyConn(client, server)610 if err != nil {611 b.Error(err)612 }613 defer conn.Close()614 conn, err = client.Connect(conn, udpSrv.Addr())615 if err != nil {616 b.Error(err)617 }618 roundtrip := func(conn net.Conn, data []byte) error {619 conn.SetDeadline(time.Now().Add(1 * time.Second))620 defer conn.SetDeadline(time.Time{})621 if _, err = conn.Write(data); err != nil {622 return err623 }624 recv := make([]byte, len(data))625 if _, err = conn.Read(recv); err != nil {626 return err627 }628 if !bytes.Equal(data, recv) {629 return fmt.Errorf("data not equal")630 }631 return nil632 }633 for i := 0; i < b.N; i++ {634 if err := roundtrip(conn, sendData); err != nil {635 b.Error(err)636 }637 }638}...
forward_test.go
Source:forward_test.go
1package gost2import (3 "crypto/rand"4 "net/http/httptest"5 "net/url"6 "testing"7)8func tcpDirectForwardRoundtrip(targetURL string, data []byte) error {9 ln, err := TCPListener("")10 if err != nil {11 return err12 }13 u, err := url.Parse(targetURL)14 if err != nil {15 return err16 }17 client := &Client{18 Connector: ForwardConnector(),19 Transporter: TCPTransporter(),20 }21 h := TCPDirectForwardHandler(u.Host)22 h.Init()23 server := &Server{24 Listener: ln,25 Handler: h,26 }27 go server.Run()28 defer server.Close()29 return proxyRoundtrip(client, server, targetURL, data)30}31func TestTCPDirectForward(t *testing.T) {32 httpSrv := httptest.NewServer(httpTestHandler)33 defer httpSrv.Close()34 sendData := make([]byte, 128)35 rand.Read(sendData)36 err := tcpDirectForwardRoundtrip(httpSrv.URL, sendData)37 if err != nil {38 t.Error(err)39 }40}41func BenchmarkTCPDirectForward(b *testing.B) {42 httpSrv := httptest.NewServer(httpTestHandler)43 defer httpSrv.Close()44 sendData := make([]byte, 128)45 rand.Read(sendData)46 ln, err := TCPListener("")47 if err != nil {48 b.Error(err)49 }50 client := &Client{51 Connector: ForwardConnector(),52 Transporter: TCPTransporter(),53 }54 u, err := url.Parse(httpSrv.URL)55 if err != nil {56 b.Error(err)57 }58 h := TCPDirectForwardHandler(u.Host)59 h.Init()60 server := &Server{61 Listener: ln,62 Handler: h,63 }64 go server.Run()65 defer server.Close()66 for i := 0; i < b.N; i++ {67 if err := proxyRoundtrip(client, server, httpSrv.URL, sendData); err != nil {68 b.Error(err)69 }70 }71}72func BenchmarkTCPDirectForwardParallel(b *testing.B) {73 httpSrv := httptest.NewServer(httpTestHandler)74 defer httpSrv.Close()75 sendData := make([]byte, 128)76 rand.Read(sendData)77 ln, err := TCPListener("")78 if err != nil {79 b.Error(err)80 }81 client := &Client{82 Connector: ForwardConnector(),83 Transporter: TCPTransporter(),84 }85 u, err := url.Parse(httpSrv.URL)86 if err != nil {87 b.Error(err)88 }89 h := TCPDirectForwardHandler(u.Host)90 h.Init()91 server := &Server{92 Listener: ln,93 Handler: h,94 }95 go server.Run()96 defer server.Close()97 b.RunParallel(func(pb *testing.PB) {98 for pb.Next() {99 if err := proxyRoundtrip(client, server, httpSrv.URL, sendData); err != nil {100 b.Error(err)101 }102 }103 })104}105func udpDirectForwardRoundtrip(t *testing.T, host string, data []byte) error {106 ln, err := UDPListener("localhost:0", nil)107 if err != nil {108 return err109 }110 client := &Client{111 Connector: ForwardConnector(),112 Transporter: UDPTransporter(),113 }114 h := UDPDirectForwardHandler(host)115 h.Init()116 server := &Server{117 Listener: ln,118 Handler: h,119 }120 go server.Run()121 defer server.Close()122 return udpRoundtrip(t, client, server, host, data)123}124func TestUDPDirectForward(t *testing.T) {125 udpSrv := newUDPTestServer(udpTestHandler)126 udpSrv.Start()127 defer udpSrv.Close()128 sendData := make([]byte, 128)129 rand.Read(sendData)130 err := udpDirectForwardRoundtrip(t, udpSrv.Addr(), sendData)131 if err != nil {132 t.Error(err)133 }134}135func BenchmarkUDPDirectForward(b *testing.B) {136 udpSrv := newUDPTestServer(udpTestHandler)137 udpSrv.Start()138 defer udpSrv.Close()139 sendData := make([]byte, 128)140 rand.Read(sendData)141 ln, err := UDPListener("localhost:0", nil)142 if err != nil {143 b.Error(err)144 }145 client := &Client{146 Connector: ForwardConnector(),147 Transporter: UDPTransporter(),148 }149 h := UDPDirectForwardHandler(udpSrv.Addr())150 h.Init()151 server := &Server{152 Listener: ln,153 Handler: h,154 }155 go server.Run()156 defer server.Close()157 for i := 0; i < b.N; i++ {158 if err := udpRoundtrip(b, client, server, udpSrv.Addr(), sendData); err != nil {159 b.Error(err)160 }161 }162}163func BenchmarkUDPDirectForwardParallel(b *testing.B) {164 udpSrv := newUDPTestServer(udpTestHandler)165 udpSrv.Start()166 defer udpSrv.Close()167 sendData := make([]byte, 128)168 rand.Read(sendData)169 ln, err := UDPListener("localhost:0", nil)170 if err != nil {171 b.Error(err)172 }173 client := &Client{174 Connector: ForwardConnector(),175 Transporter: UDPTransporter(),176 }177 h := UDPDirectForwardHandler(udpSrv.Addr())178 h.Init()179 server := &Server{180 Listener: ln,181 Handler: h,182 }183 go server.Run()184 defer server.Close()185 b.RunParallel(func(pb *testing.PB) {186 for pb.Next() {187 if err := udpRoundtrip(b, client, server, udpSrv.Addr(), sendData); err != nil {188 b.Error(err)189 }190 }191 })192}193func tcpRemoteForwardRoundtrip(t *testing.T, targetURL string, data []byte) error {194 ln, err := TCPRemoteForwardListener("localhost:0", nil) // listening on localhost195 if err != nil {196 return err197 }198 u, err := url.Parse(targetURL)199 if err != nil {200 return err201 }202 client := &Client{203 Connector: ForwardConnector(),204 Transporter: TCPTransporter(),205 }206 h := TCPRemoteForwardHandler(u.Host) // forward to u.Host207 h.Init()208 server := &Server{209 Listener: ln,210 Handler: h,211 }212 go server.Run()213 defer server.Close()214 return proxyRoundtrip(client, server, targetURL, data)215}216func TestTCPRemoteForward(t *testing.T) {217 httpSrv := httptest.NewServer(httpTestHandler)218 defer httpSrv.Close()219 sendData := make([]byte, 128)220 rand.Read(sendData)221 err := tcpRemoteForwardRoundtrip(t, httpSrv.URL, sendData)222 if err != nil {223 t.Error(err)224 }225}226func udpRemoteForwardRoundtrip(t *testing.T, host string, data []byte) error {227 ln, err := UDPRemoteForwardListener("localhost:0", nil, nil)228 if err != nil {229 return err230 }231 client := &Client{232 Connector: ForwardConnector(),233 Transporter: UDPTransporter(),234 }235 h := UDPRemoteForwardHandler(host)236 h.Init()237 server := &Server{238 Listener: ln,239 Handler: h,240 }241 go server.Run()242 defer server.Close()243 return udpRoundtrip(t, client, server, host, data)244}245func TestUDPRemoteForward(t *testing.T) {246 udpSrv := newUDPTestServer(udpTestHandler)247 udpSrv.Start()248 defer udpSrv.Close()249 sendData := make([]byte, 128)250 rand.Read(sendData)251 err := udpRemoteForwardRoundtrip(t, udpSrv.Addr(), sendData)252 if err != nil {253 t.Error(err)254 }255}...
handler_test.go
Source:handler_test.go
1package gost2import (3 "crypto/rand"4 "crypto/tls"5 "net/http/httptest"6 "net/url"7 "testing"8)9func autoHTTPProxyRoundtrip(targetURL string, data []byte, clientInfo *url.Userinfo, serverInfo []*url.Userinfo) error {10 ln, err := TCPListener("")11 if err != nil {12 return err13 }14 client := &Client{15 Connector: HTTPConnector(clientInfo),16 Transporter: TCPTransporter(),17 }18 server := &Server{19 Listener: ln,20 Handler: AutoHandler(21 UsersHandlerOption(serverInfo...),22 ),23 }24 go server.Run()25 defer server.Close()26 return proxyRoundtrip(client, server, targetURL, data)27}28func TestAutoHTTPProxy(t *testing.T) {29 httpSrv := httptest.NewServer(httpTestHandler)30 defer httpSrv.Close()31 sendData := make([]byte, 128)32 rand.Read(sendData)33 for i, tc := range httpProxyTests {34 err := autoHTTPProxyRoundtrip(httpSrv.URL, sendData, tc.cliUser, tc.srvUsers)35 if err == nil {36 if tc.errStr != "" {37 t.Errorf("#%d should failed with error %s", i, tc.errStr)38 }39 } else {40 if tc.errStr == "" {41 t.Errorf("#%d got error %v", i, err)42 }43 if err.Error() != tc.errStr {44 t.Errorf("#%d got error %v, want %v", i, err, tc.errStr)45 }46 }47 }48}49func autoSocks5ProxyRoundtrip(targetURL string, data []byte, clientInfo *url.Userinfo, serverInfo []*url.Userinfo) error {50 ln, err := TCPListener("")51 if err != nil {52 return err53 }54 client := &Client{55 Connector: SOCKS5Connector(clientInfo),56 Transporter: TCPTransporter(),57 }58 server := &Server{59 Handler: AutoHandler(UsersHandlerOption(serverInfo...)),60 Listener: ln,61 }62 go server.Run()63 defer server.Close()64 return proxyRoundtrip(client, server, targetURL, data)65}66func TestAutoSOCKS5Proxy(t *testing.T) {67 cert, err := GenCertificate()68 if err != nil {69 panic(err)70 }71 DefaultTLSConfig = &tls.Config{72 Certificates: []tls.Certificate{cert},73 }74 httpSrv := httptest.NewServer(httpTestHandler)75 defer httpSrv.Close()76 sendData := make([]byte, 128)77 rand.Read(sendData)78 for i, tc := range socks5ProxyTests {79 err := autoSocks5ProxyRoundtrip(httpSrv.URL, sendData,80 tc.cliUser,81 tc.srvUsers,82 )83 if err == nil {84 if !tc.pass {85 t.Errorf("#%d should failed", i)86 }87 } else {88 // t.Logf("#%d %v", i, err)89 if tc.pass {90 t.Errorf("#%d got error: %v", i, err)91 }92 }93 }94}95func autoSOCKS4ProxyRoundtrip(targetURL string, data []byte, options ...HandlerOption) error {96 ln, err := TCPListener("")97 if err != nil {98 return err99 }100 client := &Client{101 Connector: SOCKS4Connector(),102 Transporter: TCPTransporter(),103 }104 server := &Server{105 Listener: ln,106 Handler: AutoHandler(options...),107 }108 go server.Run()109 defer server.Close()110 return proxyRoundtrip(client, server, targetURL, data)111}112func TestAutoSOCKS4Proxy(t *testing.T) {113 httpSrv := httptest.NewServer(httpTestHandler)114 defer httpSrv.Close()115 sendData := make([]byte, 128)116 rand.Read(sendData)117 if err := autoSOCKS4ProxyRoundtrip(httpSrv.URL, sendData); err != nil {118 t.Errorf("got error: %v", err)119 }120 if err := autoSOCKS4ProxyRoundtrip(httpSrv.URL, sendData,121 UsersHandlerOption(url.UserPassword("admin", "123456"))); err == nil {122 t.Errorf("authentication required auto handler for SOCKS4 should failed")123 }124}125func autoSocks4aProxyRoundtrip(targetURL string, data []byte, options ...HandlerOption) error {126 ln, err := TCPListener("")127 if err != nil {128 return err129 }130 client := &Client{131 Connector: SOCKS4AConnector(),132 Transporter: TCPTransporter(),133 }134 server := &Server{135 Listener: ln,136 Handler: AutoHandler(options...),137 }138 go server.Run()139 defer server.Close()140 return proxyRoundtrip(client, server, targetURL, data)141}142func TestAutoSOCKS4AProxy(t *testing.T) {143 httpSrv := httptest.NewServer(httpTestHandler)144 defer httpSrv.Close()145 sendData := make([]byte, 128)146 rand.Read(sendData)147 if err := autoSocks4aProxyRoundtrip(httpSrv.URL, sendData); err != nil {148 t.Errorf("got error: %v", err)149 }150 if err := autoSocks4aProxyRoundtrip(httpSrv.URL, sendData,151 UsersHandlerOption(url.UserPassword("admin", "123456"))); err == nil {152 t.Errorf("authentication required auto handler for SOCKS4A should failed")153 }154}155func autoSSProxyRoundtrip(targetURL string, data []byte, clientInfo *url.Userinfo, serverInfo *url.Userinfo) error {156 ln, err := TCPListener("")157 if err != nil {158 return err159 }160 client := &Client{161 Connector: ShadowConnector(clientInfo),162 Transporter: TCPTransporter(),163 }164 server := &Server{165 Handler: AutoHandler(UsersHandlerOption(serverInfo)),166 Listener: ln,167 }168 go server.Run()169 defer server.Close()170 return proxyRoundtrip(client, server, targetURL, data)171}172func TestAutoSSProxy(t *testing.T) {173 httpSrv := httptest.NewServer(httpTestHandler)174 defer httpSrv.Close()175 sendData := make([]byte, 128)176 rand.Read(sendData)177 for i, tc := range ssTests {178 err := autoSSProxyRoundtrip(httpSrv.URL, sendData,179 tc.clientCipher,180 tc.serverCipher,181 )182 if err == nil {183 t.Errorf("#%d should failed", i)184 }185 }186}...
RoundTrip
Using AI Code Generation
1import (2func main() {3 client := &http.Client{}4 if err != nil {5 log.Fatal(err)6 }7 req.Header.Add("Content-Type", "application/x-www-form-urlencoded")8 resp, err := client.Do(req)9 if err != nil {10 log.Fatal(err)11 }12 defer resp.Body.Close()13 body, err := ioutil.ReadAll(resp.Body)14 if err != nil {15 log.Fatal(err)16 }17 fmt.Println(string(body))18}19import (20func main() {21 client := &http.Client{}22 if err != nil {23 log.Fatal(err)24 }25 defer resp.Body.Close()26 body, err := ioutil.ReadAll(resp.Body)27 if err != nil {28 log.Fatal(err)29 }30 fmt.Println(string(body))31}32import (33func main() {34 client := &http.Client{}35 if err != nil {36 log.Fatal(err)37 }38 defer resp.Body.Close()39 body, err := ioutil.ReadAll(resp.Body)40 if err != nil {41 log.Fatal(err)42 }43 fmt.Println(string(body))44}45import (46func main() {47 client := &http.Client{}48 if err != nil {49 log.Fatal(err)50 }51 defer resp.Body.Close()52 body, err := ioutil.ReadAll(resp.Body)53 if err != nil {54 log.Fatal(err)55 }56 fmt.Println(string(body))57}
RoundTrip
Using AI Code Generation
1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 client := &http.Client{}7 resp, err := client.Do(req)8 if err != nil {9 log.Fatal(err)10 }11 defer resp.Body.Close()12 body, err := ioutil.ReadAll(resp.Body)13 if err != nil {14 log.Fatal(err)15 }16 fmt.Println(string(body))17}18import (19func handler(w http.ResponseWriter, r *http.Request) {20 fmt.Fprintf(w, "Hello, I am in the server")21}22func main() {23 http.HandleFunc("/", handler)24 log.Fatal(http.ListenAndServe(":8080", nil))25}26import (27type MyHandler struct{}28func (mh *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {29 fmt.Fprintf(w, "Hello, I am in the server")30}31func main() {32 mh := MyHandler{}33 http.Handle("/", &mh)34 log.Fatal(http.ListenAndServe(":8080", nil))35}
RoundTrip
Using AI Code Generation
1import (2func main() {3 if err != nil {4 panic(err)5 }6 client := &http.Client{}7 resp, err := client.Do(req)8 if err != nil {9 panic(err)10 }11 defer resp.Body.Close()12 fmt.Println("Response status:", resp.Status)13}14import (15func main() {16 if err != nil {17 panic(err)18 }19 client := &http.Client{}20 resp, err := client.RoundTrip(req)21 if err != nil {22 panic(err)23 }24 defer resp.Body.Close()25 fmt.Println("Response status:", resp.Status)26}
RoundTrip
Using AI Code Generation
1import (2func main() {3 if err != nil {4 fmt.Println(err)5 }6 client := &http.Client{}7 resp, err := client.Do(req)8 if err != nil {9 fmt.Println(err)10 }11 defer resp.Body.Close()12 body, err := ioutil.ReadAll(resp.Body)13 if err != nil {14 fmt.Println(err)15 }16 fmt.Println(string(body))17}
RoundTrip
Using AI Code Generation
1import (2func main() {3 client := &http.Client{}4 resp, _ := client.Do(req)5 fmt.Println(resp)6}7import (8func main() {9 fmt.Println(resp)10}11import (12func main() {13 fmt.Println(resp)14 io.Copy(os.Stdout, resp.Body)15}16import (17func main() {18 fmt.Println(resp)19 io.Copy(os.Stdout, resp.Body)20}21import (22func main() {23 fmt.Println(resp)24}25import (26func main() {27 fmt.Println(resp)28}29import (30func main() {
RoundTrip
Using AI Code Generation
1import (2func main() {3 client := &http.Client{}4 if err != nil {5 fmt.Println(err)6 }7 resp, err := client.Do(req)8 if err != nil {9 fmt.Println(err)10 }11 defer resp.Body.Close()12 body, err := ioutil.ReadAll(resp.Body)13 if err != nil {14 fmt.Println(err)15 }16 fmt.Println(string(body))17}
RoundTrip
Using AI Code Generation
1import (2func main() {3 data := url.Values{}4 data.Set("name", "Harsh")5 data.Set("address", "India")6 req, err := http.NewRequest("POST", url, bytes.NewBufferString(data.Encode()))7 if err != nil {8 log.Fatalln(err)9 }10 req.Header.Add("Content-Type", "application/x-www-form-urlencoded")11 req.Header.Add("Accept", "application/json")12 client := &http.Client{}13 resp, err := client.Do(req)14 if err != nil {15 log.Fatalln(err)16 }17 defer resp.Body.Close()18 body, err := ioutil.ReadAll(resp.Body)19 if err != nil {20 log.Fatalln(err)21 }22 fmt.Println(string(body))23}24{"name":"Harsh","address":"India"}25import (26func main() {27 client := &http.Client{}28 resp, err := client.Get(url)29 if err != nil {30 log.Fatalln(err)31 }32 defer resp.Body.Close()33 body, err := ioutil.ReadAll(resp.Body)34 if err != nil {35 log.Fatalln(err)36 }37 fmt.Println(string(body))38}39{"name":"Harsh","address":"India"}40import (41func main() {42 data := url.Values{}43 data.Set("name", "Harsh")44 data.Set("address",
RoundTrip
Using AI Code Generation
1import (2func main() {3 client := &http.Client{}4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 response, err := client.Do(request)9 if err != nil {10 fmt.Println(err)11 os.Exit(1)12 }13 defer response.Body.Close()14 body, err := ioutil.ReadAll(response.Body)15 if err != nil {16 fmt.Println(err)17 os.Exit(1)18 }19 fmt.Println(string(body))20}21import (22func main() {23 client := &http.Client{}24 if err != nil {25 fmt.Println(err)26 os.Exit(1)27 }28 defer response.Body.Close()29 body, err := ioutil.ReadAll(response.Body)30 if err != nil {31 fmt.Println(err)32 os.Exit(1)33 }34 fmt.Println(string(body))35}36import (37func main() {38 client := &http.Client{}39 if err != nil {40 fmt.Println(err)41 os.Exit(1)42 }43 defer response.Body.Close()44 body, err := ioutil.ReadAll(response.Body)45 if err != nil {46 fmt.Println(err)47 os.Exit(1)48 }49 fmt.Println(string(body))50}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!