How to use AssertProxyUp method of toxiproxy_test Package

Best Toxiproxy code snippet using toxiproxy_test.AssertProxyUp

proxy_test.go

Source:proxy_test.go Github

copy

Full Screen

...77func WithTCPProxy(t *testing.T, f func(proxy net.Conn, response chan []byte, proxyServer *toxiproxy.Proxy)) {78 WithTCPServer(t, func(upstream string, response chan []byte) {79 proxy := NewTestProxy("test", upstream)80 proxy.Start()81 conn := AssertProxyUp(t, proxy.Listen, true)82 f(conn, response, proxy)83 proxy.Stop()84 })85}86func TestProxySimpleMessage(t *testing.T) {87 WithTCPProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) {88 msg := []byte("hello world")89 _, err := conn.Write(msg)90 if err != nil {91 t.Error("Failed writing to TCP server", err)92 }93 err = conn.Close()94 if err != nil {95 t.Error("Failed to close TCP connection", err)96 }97 resp := <-response98 if !bytes.Equal(resp, msg) {99 t.Error("Server didn't read correct bytes from client", resp)100 }101 })102}103func TestProxyToDownUpstream(t *testing.T) {104 proxy := NewTestProxy("test", "localhost:20009")105 proxy.Start()106 conn := AssertProxyUp(t, proxy.Listen, true)107 // Check to make sure the connection is closed108 conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))109 _, err := conn.Read(make([]byte, 1))110 if err != io.EOF {111 t.Error("Proxy did not close connection when upstream down", err)112 }113 proxy.Stop()114}115func TestProxyBigMessage(t *testing.T) {116 WithTCPProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) {117 buf := make([]byte, 32*1024)118 msg := make([]byte, len(buf)*2)119 hex.Encode(msg, buf)120 _, err := conn.Write(msg)121 if err != nil {122 t.Error("Failed writing to TCP server", err)123 }124 err = conn.Close()125 if err != nil {126 t.Error("Failed to close TCP connection", err)127 }128 resp := <-response129 if !bytes.Equal(resp, msg) {130 t.Error("Server didn't read correct bytes from client", resp)131 }132 })133}134func TestProxyTwoPartMessage(t *testing.T) {135 WithTCPProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) {136 msg1 := []byte("hello world")137 msg2 := []byte("hello world")138 _, err := conn.Write(msg1)139 if err != nil {140 t.Error("Failed writing to TCP server", err)141 }142 _, err = conn.Write(msg2)143 if err != nil {144 t.Error("Failed writing to TCP server", err)145 }146 err = conn.Close()147 if err != nil {148 t.Error("Failed to close TCP connection", err)149 }150 msg1 = append(msg1, msg2...)151 resp := <-response152 if !bytes.Equal(resp, msg1) {153 t.Error("Server didn't read correct bytes from client", resp)154 }155 })156}157func TestClosingProxyMultipleTimes(t *testing.T) {158 WithTCPProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) {159 proxy.Stop()160 proxy.Stop()161 proxy.Stop()162 })163}164func TestStartTwoProxiesOnSameAddress(t *testing.T) {165 WithTCPProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) {166 proxy2 := NewTestProxy("proxy_2", "localhost:3306")167 proxy2.Listen = proxy.Listen168 if err := proxy2.Start(); err == nil {169 t.Fatal("Expected an err back from start")170 }171 })172}173func TestStopProxyBeforeStarting(t *testing.T) {174 WithTCPServer(t, func(upstream string, response chan []byte) {175 proxy := NewTestProxy("test", upstream)176 AssertProxyUp(t, proxy.Listen, false)177 proxy.Stop()178 err := proxy.Start()179 if err != nil {180 t.Error("Proxy failed to start", err)181 }182 err = proxy.Start()183 if err != toxiproxy.ErrProxyAlreadyStarted {184 t.Error("Proxy did not fail to start when already started", err)185 }186 AssertProxyUp(t, proxy.Listen, true)187 proxy.Stop()188 AssertProxyUp(t, proxy.Listen, false)189 })190}191func TestProxyUpdate(t *testing.T) {192 WithTCPServer(t, func(upstream string, response chan []byte) {193 proxy := NewTestProxy("test", upstream)194 err := proxy.Start()195 if err != nil {196 t.Error("Proxy failed to start", err)197 }198 AssertProxyUp(t, proxy.Listen, true)199 before := proxy.Listen200 input := &toxiproxy.Proxy{Listen: "localhost:0", Upstream: proxy.Upstream, Enabled: true}201 err = proxy.Update(input)202 if err != nil {203 t.Error("Failed to update proxy", err)204 }205 if proxy.Listen == before || proxy.Listen == input.Listen {206 t.Errorf("Proxy update didn't change listen address: %s to %s", before, proxy.Listen)207 }208 AssertProxyUp(t, proxy.Listen, true)209 input.Listen = proxy.Listen210 err = proxy.Update(input)211 if err != nil {212 t.Error("Failed to update proxy", err)213 }214 AssertProxyUp(t, proxy.Listen, true)215 input.Enabled = false216 err = proxy.Update(input)217 if err != nil {218 t.Error("Failed to update proxy", err)219 }220 AssertProxyUp(t, proxy.Listen, false)221 })222}223func TestRestartFailedToStartProxy(t *testing.T) {224 WithTCPServer(t, func(upstream string, response chan []byte) {225 proxy := NewTestProxy("test", upstream)226 conflict := NewTestProxy("test2", upstream)227 err := conflict.Start()228 if err != nil {229 t.Error("Proxy failed to start", err)230 }231 AssertProxyUp(t, conflict.Listen, true)232 proxy.Listen = conflict.Listen233 err = proxy.Start()234 if err == nil || err == toxiproxy.ErrProxyAlreadyStarted {235 t.Error("Proxy started when it should have conflicted")236 }237 conflict.Stop()238 AssertProxyUp(t, conflict.Listen, false)239 err = proxy.Start()240 if err != nil {241 t.Error("Proxy failed to start after conflict went away", err)242 }243 AssertProxyUp(t, proxy.Listen, true)244 proxy.Stop()245 AssertProxyUp(t, proxy.Listen, false)246 })247}248func AssertProxyUp(t *testing.T, addr string, up bool) net.Conn {249 conn, err := net.Dial("tcp", addr)250 if err != nil && up {251 t.Error("Expected proxy to be up:", err)252 } else if err == nil && !up {253 t.Error("Expected proxy to be down")254 }255 return conn256}...

Full Screen

Full Screen

toxiproxy_test.go

Source:toxiproxy_test.go Github

copy

Full Screen

...18) {19 testhelper.WithTCPServer(t, func(upstream string, response chan []byte) {20 proxy := NewTestProxy("test", upstream)21 proxy.Start()22 conn := AssertProxyUp(t, proxy.Listen, true)23 f(conn, response, proxy)24 proxy.Stop()25 })26}27func AssertProxyUp(t *testing.T, addr string, up bool) net.Conn {28 conn, err := net.Dial("tcp", addr)29 if err != nil && up {30 t.Error("Expected proxy to be up:", err)31 } else if err == nil && !up {32 t.Error("Expected proxy to be down")33 }34 return conn35}...

Full Screen

Full Screen

AssertProxyUp

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxiproxy_test.AssertProxyUp(t, toxiproxyClient, proxy.Name, 100)4}5import (6func AssertProxyUp(t *testing.T, toxiproxyClient *client.Client, proxyName string, timeout time.Duration) {7 start := time.Now()8 for {9 if time.Since(start) > timeout {10 assert.Fail(t, "Toxiproxy did not start in time")11 }12 _, err := toxiproxyClient.Proxies().Get(proxyName)13 if err == nil {14 }15 time.Sleep(50 * time.Millisecond)16 }17}18func StartToxiproxy(t *testing.T) *client.Client {19 cmd := exec.Command("toxiproxy-server")20 cmd.Env = append(os.Environ(), "TOXIPROXY_DISABLE_API=true")21 err := cmd.Start()22 require.NoError(t, err)23 time.Sleep(100 * time.Millisecond)24 toxiproxyClient := client.NewClient("localhost:8474")25}26func CreateProxy(t *testing.T, toxiproxyClient *client.Client, name string, listenPort int, upstream string) *client.Proxy {27 proxy, err := toxiproxyClient.Proxies().Create(&client.Proxy{Name: name, Listen: fmt.Sprintf("localhost:%d", listenPort), Upstream: upstream})28 require.NoError(t, err)29}30func CreateProxyWithToxics(t *testing.T, toxiproxyClient *client.Client, name string, listenPort int, upstream string

Full Screen

Full Screen

AssertProxyUp

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxiproxy := toxiproxy.NewClient("localhost:8474")4 toxiproxy.AddProxy("test", "localhost:8000", "localhost:9000")5 toxiproxy.AddToxic("test", "test", "timeout", "downstream", 1, toxiproxy.Toxic{Timeout: 1000})6 toxiproxy.AssertProxyUp("test")7 fmt.Println("Proxy is up!")8}9import (10func main() {11 toxiproxy := toxiproxy.NewClient("localhost:8474")12 toxiproxy.AddProxy("test", "localhost:8000", "localhost:9000")13 toxiproxy.AddToxic("test", "test", "timeout", "downstream", 1, toxiproxy.Toxic{Timeout: 1000})14 toxiproxy.AssertProxyDown("test")15 fmt.Println("Proxy is down!")16}17import (18func main() {19 toxiproxy := toxiproxy.NewClient("localhost:8474")20 toxiproxy.AddProxy("test", "localhost:8000", "localhost:9000")21 toxiproxy.AddToxic("test", "test", "timeout", "downstream", 1, toxiproxy.Toxic{Timeout: 1000})22 toxiproxy.AssertToxicExists("test", "test")23 fmt.Println("Toxic exists!")24}25import (26func main() {27 toxiproxy := toxiproxy.NewClient("localhost:8474")28 toxiproxy.AddProxy("test", "localhost:8000", "localhost:9000")

Full Screen

Full Screen

AssertProxyUp

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 err := toxiproxy.AddProxy("test", "localhost:8080", "localhost:8081")4 if err != nil {5 log.Fatal(err)6 }7 toxiproxy.AddProxy("test1", "localhost:8082", "localhost:8083")8 toxiproxyClient.AddProxy("test", "localhost:8080", "localhost:8081")9 toxiproxyClient.AddProxy("test1", "localhost:8082", "localhost:8083")10 toxiproxyClient.AddProxy("test2", "localhost:8084", "localhost:8085")11 toxiproxyClient.AddProxy("test3", "localhost:8086", "localhost:8087")12 toxiproxyClient.AddProxy("test4", "localhost:8088", "localhost:8089")13 toxiproxyClient.AddProxy("test5", "localhost:8090", "localhost:8091")14 toxiproxyClient.AddProxy("test6", "localhost:8092", "localhost:8093")15 toxiproxyClient.AddProxy("test7", "localhost:8094", "localhost:8095")16 toxiproxyClient.AddProxy("test8", "localhost:8096", "localhost:8097")17 toxiproxyClient.AddProxy("test9", "localhost:8098", "localhost:8099")18 toxiproxyClient.AddProxy("test10", "localhost:8100", "localhost:8101")19 toxiproxyClient.AddProxy("test11", "localhost:8102", "localhost:8103")20 toxiproxyClient.AddProxy("test12", "localhost:8104", "localhost:8105")21 toxiproxyClient.AddProxy("test13", "localhost:8106", "localhost:8107")22 toxiproxyClient.AddProxy("test14", "localhost:8108", "localhost:8109")23 toxiproxyClient.AddProxy("test15", "localhost:8110", "localhost:8111")

Full Screen

Full Screen

AssertProxyUp

Using AI Code Generation

copy

Full Screen

1toxiproxy.AssertProxyUp(proxyName)2toxiproxy.AssertProxyDown(proxyName)3toxiproxy.AssertProxyUp(proxyName)4toxiproxy.AssertProxyDown(proxyName)5toxiproxy.AssertProxyUp(proxyName)6toxiproxy.AssertProxyDown(proxyName)7toxiproxy.AssertProxyUp(proxyName)8toxiproxy.AssertProxyDown(proxyName)9toxiproxy.AssertProxyUp(proxyName)10toxiproxy.AssertProxyDown(proxyName)11toxiproxy.AssertProxyUp(proxyName)12toxiproxy.AssertProxyDown(proxyName)13toxiproxy.AssertProxyUp(proxyName)14toxiproxy.AssertProxyDown(proxyName)15toxiproxy.AssertProxyUp(proxyName)16toxiproxy.AssertProxyDown(proxyName)17toxiproxy.AssertProxyUp(proxyName)18toxiproxy.AssertProxyDown(proxyName)

Full Screen

Full Screen

AssertProxyUp

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 toxiproxy := toxiproxy.NewClient("localhost:8474")4 toxiproxyClient := toxiproxy_test.NewClient(toxiproxy)5 toxiproxyClient.AssertProxyUp("test_proxy")6}7import (8type Client struct {9}10func NewClient(toxiproxyClient *toxiproxy.Client) *Client {11 return &Client{toxiproxyClient: toxiproxyClient}12}13func (client *Client) AssertProxyUp(name string) {14 proxy, err := client.toxiproxyClient.Proxy(name)15 if err != nil {16 panic(err)17 }18 if proxy.Enabled == false {19 panic("Proxy is not enabled")20 }21}

Full Screen

Full Screen

AssertProxyUp

Using AI Code Generation

copy

Full Screen

1func TestToxiProxy(t *testing.T) {2 toxiproxy := NewToxiproxy()3 toxiproxy.Start()4 defer toxiproxy.Stop()5 toxiproxy.AssertProxyUp("test_proxy")6}7func (t *ToxiproxyTest) AssertProxyUp(name string) {8 proxy := t.client.Proxy(name)9 if proxy == nil {10 t.t.Fatalf("Proxy %s not found", name)11 }12 if proxy.Enabled {13 t.t.Fatalf("Proxy %s is not enabled", name)14 }15}16func (t *ToxiproxyTest) Stop() {17 if t.client != nil {18 t.client.DeleteAll()19 }20 if t.cmd != nil {21 t.cmd.Process.Kill()22 }23}24func (t *ToxiproxyTest) Start() {25 t.cmd = exec.Command("toxiproxy-server")26 err := t.cmd.Start()27 if err != nil {28 t.t.Fatal(err)29 }30 t.client = NewClient("

Full Screen

Full Screen

AssertProxyUp

Using AI Code Generation

copy

Full Screen

1func TestToxiproxy(t *testing.T) {2 toxiproxyTest := NewToxiproxyTest(t)3 toxiproxyTest.AssertProxyUp("my-proxy")4}5func (t *ToxiproxyTest) AssertProxyUp(proxyName string) {6 proxy, err := t.toxiproxyClient.Proxy(proxyName)7 if err != nil {8 t.t.Fatal(err)9 }10 if proxy.Enabled == false {11 t.t.Fatal("Proxy is not up")12 }13}14func (t *ToxiproxyTest) AssertProxyUp(proxyName string) {15 proxy, err := t.toxiproxyClient.Proxy(proxyName)16 if err != nil {17 t.t.Fatal(err)18 }19 if proxy.Enabled == false {20 t.t.Fatal("Proxy is not up")21 }22}23func (t *ToxiproxyTest) AssertProxyUp(proxyName string) {24 proxy, err := t.toxiproxyClient.Proxy(proxyName)25 if err != nil {26 t.t.Fatal(err)27 }28 if proxy.Enabled == false {29 t.t.Fatal("Proxy is not up")30 }31}32func (t *ToxiproxyTest) AssertProxyUp(proxyName string) {33 proxy, err := t.toxiproxyClient.Proxy(proxyName)34 if err != nil {35 t.t.Fatal(err)36 }37 if proxy.Enabled == false {38 t.t.Fatal("Proxy is not up")39 }40}41func (t *ToxiproxyTest) AssertProxyUp(proxyName string) {42 proxy, err := t.toxiproxyClient.Proxy(proxyName)43 if err != nil {44 t.t.Fatal(err)45 }46 if proxy.Enabled == false {47 t.t.Fatal("Proxy is not up")48 }49}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful