// define testcases using teststeps func tc1(m *gogrinder.Meta, s gogrinder.Settings) { var mm *req.HttpMetric c := req.NewDefaultClient() form := url.Values{} form.Add("username", "gogrinder") b := gg.NewBracket("01_01_login") { r, err := http.NewRequest("POST", "http://localhost:3001/login", strings.NewReader(form.Encode())) r.Header.Add("Content-Type", "application/x-www-form-urlencoded") if err != nil { m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } _, _, mm = req.DoRaw(c, r, m) } b.End(mm) b = gg.NewBracket("01_02_get") { r, err := http.NewRequest("GET", "http://localhost:3001/get_private", nil) if err != nil { m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } _, _, mm = req.DoRaw(c, r, m) } b.End(mm) }
func supercars_03_create(m *gogrinder.Meta, s gogrinder.Settings) { var mm *req.HttpMetric var resp map[string]interface{} c := req.NewDefaultClient() base := s["supercars_url"].(string) newCar := map[string]interface{}{"name": "Ferrari Enzo", "country": "Italy", "top_speed": "218", "0-60": "3.4", "power": "650", "engine": "5998", "weight": "1365", "description": "The Enzo Ferrari is a 12 cylinder " + "mid-engine berlinetta named after the company's founder, Enzo Ferrari.", "image": "050.png"} b := gg.NewBracket("03_01_supercars_create") r, err := req.NewPostJsonRequest(base+"/rest/supercars/", newCar) if err != nil { m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } else { resp, _, mm = req.DoJson(c, r, m) id := resp["_id"].(string) if i, err := strconv.Atoi(id); err != nil || i <= RECORDS { m.Error += "Error: something went wrong during new record creation!" } else { redis, err := goredis.Dial( &goredis.DialConfig{Address: s["redis_srv"].(string)}) if err != nil { // is the redis server running? correct address? m.Error += err.Error() } else { redis.SAdd("supercars", id) // no way this can go wrong! } } } b.End(mm) }
func supercars_05_delete(m *gogrinder.Meta, s gogrinder.Settings) { var mm *req.HttpMetric c := req.NewDefaultClient() base := s["supercars_url"].(string) b := gg.NewBracket("05_01_supercars_delete") redis, err := goredis.Dial(&goredis.DialConfig{Address: s["redis_srv"].(string)}) if err != nil { // is the redis server running? correct address? m.Error += err.Error() return } id, err := redis.SPop("supercars") if err != nil { // probably run out of data - so it does not make sense to continue m.Error += err.Error() return } r, err := http.NewRequest("DELETE", base+"/rest/supercars/"+string(id), nil) if err != nil { m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } else { _, _, mm = req.DoRaw(c, r, m) } b.End(mm) }
// JSON func DoJson(c *http.Client, r *http.Request, m *gogrinder.Meta) (map[string]interface{}, http.Header, *HttpMetric) { hm := &HttpMetric{*m, gogrinder.Elapsed(0), 0, 421} // http status Misdirected Request resp, err := c.Do(r) if err != nil { hm.Error += err.Error() } if resp != nil { defer resp.Body.Close() mr := newMetricReader(resp.Body) // read the response body and parse as json raw, err := ioutil.ReadAll(mr) if err != nil { m.Error += err.Error() } doc := make(map[string]interface{}) if len(raw) > 0 { if raw[0] == '[' { // a REST service response to be an array does not seem to be a good idea: // http://stackoverflow.com/questions/12293979/how-do-i-return-a-json-array-with-bottle // many applications do this anyway... // so for now we need a workaround: var array []interface{} err = json.Unmarshal(raw, &array) doc["data"] = array } else { err = json.Unmarshal(raw, &doc) } if err != nil { m.Error += err.Error() } } hm.FirstByte = mr.firstByteAfter hm.Bytes = mr.bytes hm.Code = resp.StatusCode return doc, resp.Header, hm } return nil, nil, hm }
func supercars_02_read(m *gogrinder.Meta, s gogrinder.Settings) { var mm *req.HttpMetric var resp map[string]interface{} c := req.NewDefaultClient() b := gg.NewBracket("02_01_supercars_read") id := rand.Intn(RECORDS-1) + 1 url := fmt.Sprintf("%s/rest/supercars/%05d", s["supercars_url"].(string), id) r, err := http.NewRequest("GET", url, nil) if err != nil { m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } else { resp, _, mm = req.DoJson(c, r, m) // assert record id i, err := strconv.Atoi(resp["_id"].(string)) if err != nil || i != id { m.Error += "Error: retrived wrong record!" } } b.End(mm) }
func supercars_04_update(m *gogrinder.Meta, s gogrinder.Settings) { var mm *req.HttpMetric //var resp map[string]interface{} c := req.NewDefaultClient() base := s["supercars_url"].(string) change := map[string]interface{}{"cylinders": "12", "name": "Ferrari Enzo", "country": "Italy", "top_speed": "218", "0-60": "3.4", "power": "650", "engine": "5998", "weight": "1365", "description": "The Enzo Ferrari " + "is a 12 cylinder mid-engine berlinetta named after the company's " + "founder, Enzo Ferrari.", "image": "050.png"} b := gg.NewBracket("04_01_supercars_update") redis, err := goredis.Dial(&goredis.DialConfig{Address: s["redis_srv"].(string)}) if err != nil { // is the redis server running? correct address? m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } else { id, err := redis.SPop("supercars") if err != nil { // probably run out of data - so it does not make sense to continue m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } else { r, err := req.NewPutJsonRequest(base+"/rest/supercars/"+string(id), change) if err != nil { m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } else { _, _, mm = req.DoJson(c, r, m) //tsUpdate(m, c, base + "/rest/supercars/" + string(id), change) // add the record back! redis.SAdd("supercars", string(id)) // no way this can go wrong! } } } b.End(mm) }
// define testcases using teststeps func tc1(m *gogrinder.Meta, s gogrinder.Settings) { var mm *HttpMetric c := NewDefaultClient() b := gg.NewBracket("01_01_teststep") { r, err := http.NewRequest("GET", "http://localhost:3001/get_stuff", nil) if err != nil { m.Error += err.Error() mm = &HttpMetric{*m, 0, 0, 400} } _, _, mm = DoRaw(c, r, m) } b.End(mm) }
func tc2(m *gogrinder.Meta, s gogrinder.Settings) { var mm *req.HttpMetric c := req.NewDefaultClient() b := gg.NewBracket("02_01_teststep") { r, err := http.NewRequest("POST", "http://localhost:3001/post_stuff", util.NewRandReader(2000)) if err != nil { m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } _, _, mm = req.DoRaw(c, r, m) } b.End(mm) }
// define testcases using teststeps func supercars_01_list(m *gogrinder.Meta, s gogrinder.Settings) { var mm *req.HttpMetric var resp map[string]interface{} c := req.NewDefaultClient() base := s["supercars_url"].(string) b := gg.NewBracket("01_01_supercars_list") r, err := http.NewRequest("GET", base+"/rest/supercars/", nil) if err != nil { m.Error += err.Error() mm = &req.HttpMetric{*m, 0, 0, 400} } else { resp, _, mm = req.DoJson(c, r, m) // assert record count count := len(resp["data"].([]interface{})) if count < RECORDS { mm.Error += "Error: less then 30 records in list response!" } } b.End(mm) }
// DOC func Do(c *http.Client, r *http.Request, m *gogrinder.Meta) (*goquery.Document, http.Header, *HttpMetric) { hm := &HttpMetric{*m, gogrinder.Elapsed(0), 0, 421} // http status Misdirected Request resp, err := c.Do(r) if err != nil { hm.Error += err.Error() } if resp != nil { defer resp.Body.Close() mr := newMetricReader(resp.Body) // read the response body and parse into document doc, err := goquery.NewDocumentFromReader(mr) if err != nil { m.Error += err.Error() } hm.FirstByte = mr.firstByteAfter hm.Bytes = mr.bytes hm.Code = resp.StatusCode return doc, resp.Header, hm } return nil, nil, hm }