Best Toxiproxy code snippet using toxiproxy.checkError
client.go
Source:client.go
...51 resp, err := http.Get(client.endpoint + "/proxies")52 if err != nil {53 return nil, err54 }55 err = checkError(resp, http.StatusOK, "Proxies")56 if err != nil {57 return nil, err58 }59 proxies := make(map[string]*Proxy)60 err = json.NewDecoder(resp.Body).Decode(&proxies)61 if err != nil {62 return nil, err63 }64 for _, proxy := range proxies {65 proxy.client = client66 proxy.created = true67 }68 return proxies, nil69}70// Generates a new uncommitted proxy instance. In order to use the result, the71// proxy fields will need to be set and have `Save()` called.72func (client *Client) NewProxy() *Proxy {73 return &Proxy{74 client: client,75 }76}77// CreateProxy instantiates a new proxy and starts listening on the specified address.78// This is an alias for `NewProxy()` + `proxy.Save()`79func (client *Client) CreateProxy(name, listen, upstream string) (*Proxy, error) {80 proxy := &Proxy{81 Name: name,82 Listen: listen,83 Upstream: upstream,84 Enabled: true,85 client: client,86 }87 err := proxy.Save()88 if err != nil {89 return nil, err90 }91 return proxy, nil92}93// Proxy returns a proxy by name.94func (client *Client) Proxy(name string) (*Proxy, error) {95 // TODO url encode96 resp, err := http.Get(client.endpoint + "/proxies/" + name)97 if err != nil {98 return nil, err99 }100 err = checkError(resp, http.StatusOK, "Proxy")101 if err != nil {102 return nil, err103 }104 proxy := new(Proxy)105 err = json.NewDecoder(resp.Body).Decode(proxy)106 if err != nil {107 return nil, err108 }109 proxy.client = client110 proxy.created = true111 return proxy, nil112}113// Create a list of proxies using a configuration list. If a proxy already exists, it will be replaced114// with the specified configuration. For large amounts of proxies, `config` can be loaded from a file.115// Returns a list of the successfully created proxies.116func (client *Client) Populate(config []Proxy) ([]*Proxy, error) {117 proxies := struct {118 Proxies []*Proxy `json:"proxies"`119 }{}120 request, err := json.Marshal(config)121 if err != nil {122 return nil, err123 }124 resp, err := http.Post(client.endpoint+"/populate", "application/json", bytes.NewReader(request))125 if err != nil {126 return nil, err127 }128 // Response body may need to be read twice, we want to return both the proxy list and any errors129 var body bytes.Buffer130 tee := io.TeeReader(resp.Body, &body)131 err = json.NewDecoder(tee).Decode(&proxies)132 if err != nil {133 return nil, err134 }135 resp.Body = ioutil.NopCloser(&body)136 err = checkError(resp, http.StatusCreated, "Populate")137 return proxies.Proxies, err138}139// Save saves changes to a proxy such as its enabled status or upstream port.140func (proxy *Proxy) Save() error {141 request, err := json.Marshal(proxy)142 if err != nil {143 return err144 }145 var resp *http.Response146 if proxy.created {147 resp, err = http.Post(proxy.client.endpoint+"/proxies/"+proxy.Name, "text/plain", bytes.NewReader(request))148 } else {149 resp, err = http.Post(proxy.client.endpoint+"/proxies", "application/json", bytes.NewReader(request))150 }151 if err != nil {152 return err153 }154 if proxy.created {155 err = checkError(resp, http.StatusOK, "Save")156 } else {157 err = checkError(resp, http.StatusCreated, "Create")158 }159 if err != nil {160 return err161 }162 err = json.NewDecoder(resp.Body).Decode(proxy)163 if err != nil {164 return err165 }166 proxy.created = true167 return nil168}169// Enable a proxy again after it has been disabled.170func (proxy *Proxy) Enable() error {171 proxy.Enabled = true172 return proxy.Save()173}174// Disable a proxy so that no connections can pass through. This will drop all active connections.175func (proxy *Proxy) Disable() error {176 proxy.Enabled = false177 return proxy.Save()178}179// Delete a proxy complete and close all existing connections through it. All information about180// the proxy such as listen port and active toxics will be deleted as well. If you just wish to181// stop and later enable a proxy, use `Enable()` and `Disable()`.182func (proxy *Proxy) Delete() error {183 httpClient := &http.Client{}184 req, err := http.NewRequest("DELETE", proxy.client.endpoint+"/proxies/"+proxy.Name, nil)185 if err != nil {186 return err187 }188 resp, err := httpClient.Do(req)189 if err != nil {190 return err191 }192 return checkError(resp, http.StatusNoContent, "Delete")193}194// Toxics returns a map of all the active toxics and their attributes.195func (proxy *Proxy) Toxics() (Toxics, error) {196 resp, err := http.Get(proxy.client.endpoint + "/proxies/" + proxy.Name + "/toxics")197 if err != nil {198 return nil, err199 }200 err = checkError(resp, http.StatusOK, "Toxics")201 if err != nil {202 return nil, err203 }204 toxics := make(Toxics, 0)205 err = json.NewDecoder(resp.Body).Decode(&toxics)206 if err != nil {207 return nil, err208 }209 return toxics, nil210}211// AddToxic adds a toxic to the given stream direction.212// If a name is not specified, it will default to <type>_<stream>.213// If a stream is not specified, it will default to downstream.214// See https://github.com/Shopify/toxiproxy#toxics for a list of all Toxic types.215func (proxy *Proxy) AddToxic(name, typeName, stream string, toxicity float32, attrs Attributes) (*Toxic, error) {216 toxic := Toxic{name, typeName, stream, toxicity, attrs}217 if toxic.Toxicity == -1 {218 toxic.Toxicity = 1 // Just to be consistent with a toxicity of -1 using the default219 }220 request, err := json.Marshal(&toxic)221 if err != nil {222 return nil, err223 }224 resp, err := http.Post(proxy.client.endpoint+"/proxies/"+proxy.Name+"/toxics", "application/json", bytes.NewReader(request))225 if err != nil {226 return nil, err227 }228 err = checkError(resp, http.StatusOK, "AddToxic")229 if err != nil {230 return nil, err231 }232 result := &Toxic{}233 err = json.NewDecoder(resp.Body).Decode(result)234 if err != nil {235 return nil, err236 }237 return result, nil238}239// UpdateToxic sets the parameters for an existing toxic with the given name.240// If toxicity is set to -1, the current value will be used.241func (proxy *Proxy) UpdateToxic(name string, toxicity float32, attrs Attributes) (*Toxic, error) {242 toxic := map[string]interface{}{243 "attributes": attrs,244 }245 if toxicity != -1 {246 toxic["toxicity"] = toxicity247 }248 request, err := json.Marshal(&toxic)249 if err != nil {250 return nil, err251 }252 resp, err := http.Post(proxy.client.endpoint+"/proxies/"+proxy.Name+"/toxics/"+name, "application/json", bytes.NewReader(request))253 if err != nil {254 return nil, err255 }256 err = checkError(resp, http.StatusOK, "UpdateToxic")257 if err != nil {258 return nil, err259 }260 result := &Toxic{}261 err = json.NewDecoder(resp.Body).Decode(result)262 if err != nil {263 return nil, err264 }265 return result, nil266}267// RemoveToxic renives the toxic with the given name.268func (proxy *Proxy) RemoveToxic(name string) error {269 httpClient := &http.Client{}270 req, err := http.NewRequest("DELETE", proxy.client.endpoint+"/proxies/"+proxy.Name+"/toxics/"+name, nil)271 if err != nil {272 return err273 }274 resp, err := httpClient.Do(req)275 if err != nil {276 return err277 }278 return checkError(resp, http.StatusNoContent, "RemoveToxic")279}280// ResetState resets the state of all proxies and toxics in Toxiproxy.281func (client *Client) ResetState() error {282 resp, err := http.Post(client.endpoint+"/reset", "text/plain", bytes.NewReader([]byte{}))283 if err != nil {284 return err285 }286 return checkError(resp, http.StatusNoContent, "ResetState")287}288type ApiError struct {289 Message string `json:"error"`290 Status int `json:"status"`291}292func (err *ApiError) Error() string {293 return fmt.Sprintf("HTTP %d: %s", err.Status, err.Message)294}295func checkError(resp *http.Response, expectedCode int, caller string) error {296 if resp.StatusCode != expectedCode {297 apiError := new(ApiError)298 err := json.NewDecoder(resp.Body).Decode(apiError)299 if err != nil {300 apiError.Message = fmt.Sprintf("Unexpected response code, expected %d", expectedCode)301 apiError.Status = resp.StatusCode302 }303 return fmt.Errorf("%s: %v", caller, apiError)304 }305 return nil306}...
checkError
Using AI Code Generation
1toxiproxy.checkError(err)2toxiproxy.checkError(err)3toxiproxy.checkError(err)4toxiproxy.checkError(err)5toxiproxy.checkError(err)6toxiproxy.checkError(err)7toxiproxy.checkError(err)8toxiproxy.checkError(err)9toxiproxy.checkError(err)10toxiproxy.checkError(err)11toxiproxy.checkError(err)12toxiproxy.checkError(err)13toxiproxy.checkError(err)14toxiproxy.checkError(err)15toxiproxy.checkError(err)16toxiproxy.checkError(err)17toxiproxy.checkError(err)18toxiproxy.checkError(err)
checkError
Using AI Code Generation
1import (2func main() {3 proxy, err := client.CreateProxy("my_proxy", "localhost:8081", "localhost:8080")4 if err != nil {5 fmt.Println(err)6 }7 fmt.Println(proxy)8 err = proxy.AddToxic("latency", "latency", "downstream", 1, toxiproxy.Attributes{"latency": 1000})9 if err != nil {10 fmt.Println(err)11 }12}13&{my_proxy localhost:8081 localhost:8080 8474 false 0xc42000e060 0xc42000e080}14{"name":"my_proxy","listen":"localhost:8081","upstream":"localhost:8080","enabled":false,"toxics":[]}15{"name":"my_proxy","listen":"localhost:8081","upstream":"localhost:8080","enabled":true,"toxics":[]}16{"name":"my_proxy","listen":"localhost:8081","upstream":"localhost:8080","enabled":true,"toxics":[{"name":"latency","type":"latency","stream":"downstream","toxicity":1,"attributes":{"latency":1000},"index":0}]}17&{
checkError
Using AI Code Generation
1import (2func main() {3 client, err := toxiproxy.NewClient("localhost:8474")4 checkError(err)5 proxy, err := client.CreateProxy(&toxiproxy.Proxy{6 })7 checkError(err)8 toxic, err := proxy.CreateToxic(&toxiproxy.Toxic{9 Attributes: map[string]interface{}{10 },11 })12 checkError(err)13 checkError(err)14 fmt.Println(resp.Status)15 err = proxy.DeleteToxic(toxic.Name)16 checkError(err)17 err = client.DeleteProxy(proxy.Name)18 checkError(err)19}20func checkError(err error) {21 if err != nil {22 fmt.Println("Error: ", err)23 }24}25import (26func main() {27 listener, err := net.Listen("tcp", "localhost:8080")28 checkError(err)29 server := &http.Server{30 }31 err = server.Serve(listener)32 checkError(err)33}34func checkError(err error) {35 if err != nil {36 fmt.Println("Error: ", err)37 }38}39import (
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!!