func TestInvalidParams(t *testing.T) { s, ln, srverr := startServer() defer stopServer(s, ln, srverr) response := fasthttp.AcquireResponse() request := fasthttp.AcquireRequest() base := fmt.Sprintf("http://localhost:%d/", port) + "testimages/server/01.jpg" cases := []string{ "?width=abc&height=200", "?width=200&height=abc", "?width=200?height=200", "?width=200&height=200&mode=fit&something=x", "?width=200&height=200&mode=y", "?format=abc"} for _, c := range cases { uri := base + c request.SetRequestURI(uri) fasthttp.Do(request, response) if response.Header.StatusCode() != 400 { t.Fatal("Server did not return 400 to request: " + uri) } request.Reset() response.Reset() } fasthttp.ReleaseRequest(request) fasthttp.ReleaseResponse(response) }
// Test for right content-type request header func TestMIMEtype(t *testing.T) { s, ln, srverr := startServer() defer stopServer(s, ln, srverr) response := fasthttp.AcquireResponse() request := fasthttp.AcquireRequest() cases := []string{"", "?format=jpeg", "?format=png", "?format=webp", "?format=tiff", "?format=bmp", "?format=gif"} folder := "testimages/server/" for _, c := range cases { request.SetRequestURI(fmt.Sprintf("http://localhost:%d/", port) + folder + "01.jpg" + c) fasthttp.Do(request, response) MIME := string(response.Header.ContentType()) img := images.NewImage() img.FromBlob(response.Body()) expected := "image/" + strings.ToLower(img.GetImageFormat()) if MIME != expected { t.Fatal(fmt.Sprintf("Server returned: %s, image is %s", MIME, expected)) } request.Reset() response.Reset() img.Destroy() } fasthttp.ReleaseRequest(request) fasthttp.ReleaseResponse(response) }
// KeepAlive for support connect, session key will available for 20 minutes func (b *Betting) KeepAlive() error { var keepAlive *KeepAlive = &KeepAlive{} req, resp := fasthttp.AcquireRequest(), fasthttp.AcquireResponse() req.SetRequestURI(KeepAliveURL) req.Header.Set("Accept", "application/json") req.Header.Set("X-Application", b.ApiKey) req.Header.Set("X-Authentication", b.SessionKey) req.Header.SetMethod("POST") err := fasthttp.Do(req, resp) if err != nil { return err } err = json.Unmarshal(resp.Body(), keepAlive) if err != nil { return err } switch keepAlive.Status { case "SUCCESS": b.SessionKey = keepAlive.Token default: err = errors.New(keepAlive.Error) } return err }
// Request function for send requests to betfair via REST JSON func (b *Betting) Request(reqStruct interface{}, url BetURL, method string, filter *Filter) error { req, resp := fasthttp.AcquireRequest(), fasthttp.AcquireResponse() urlBuild := bytes.NewBuffer([]byte{}) urlBuild.WriteString(string(url)) urlBuild.WriteString("/") urlBuild.WriteString(method) urlBuild.WriteString("/") req.SetRequestURI(urlBuild.String()) req.Header.Set("Accept", "application/json") req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Application", b.ApiKey) req.Header.Set("X-Authentication", b.SessionKey) req.Header.SetMethod("POST") if filter != nil { filterBody, err := ffjson.Marshal(&filter) if err != nil { return err } req.SetBody(filterBody) } err := fasthttp.Do(req, resp) if err != nil { return err } if resp.StatusCode() == 400 { err = ffjson.Unmarshal(resp.Body(), &bettingError) if err != nil { return err } return fmt.Errorf("Error with code - %s and string - %s", bettingError.Faultcode, bettingError.Faultstring) } err = ffjson.Unmarshal(resp.Body(), reqStruct) if err != nil { return err } return nil }