func TestServerAbort(t *testing.T) { t.Parallel() for _, code := range []int{200, 500} { server := httptest.NewServer( http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Length", "4000") w.WriteHeader(code) w.Write(bytes.Repeat([]byte("a"), 3000)) }, ), ) u, err := url.Parse(server.URL) ensure.Nil(t, err) c := &fbapi.Client{ Transport: defaultHTTPTransport, BaseURL: u, } res := make(map[string]interface{}) _, err = c.Do(&http.Request{Method: "GET"}, res) ensure.NotNil(t, err) ensure.StringContains(t, err.Error(), fmt.Sprintf(`GET %s`, server.URL)) server.CloseClientConnections() server.Close() } }
func TestServerAbort(t *testing.T) { t.Parallel() for _, code := range []int{200, 500} { server := httptest.NewServer( http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Length", "4000") w.WriteHeader(code) w.Write(bytes.Repeat([]byte("a"), 3000)) }, ), ) u, err := url.Parse(server.URL) ensure.Nil(t, err) c := &parse.Client{ Credentials: defaultRestAPIKey, BaseURL: u, } res := make(map[string]interface{}) _, err = c.Get(nil, res) ensure.NotNil(t, err) server.CloseClientConnections() server.Close() } }
func TestMissingCredentials(t *testing.T) { t.Parallel() var c parse.Client req := http.Request{Method: "GET", URL: &url.URL{Path: "classes/Foo/Bar"}} _, err := c.Do(&req, nil, nil) ensure.NotNil(t, err) ensure.Err(t, err, regexp.MustCompile(`parse: api error with message="unauthorized"`)) }
func TestNonNil(t *testing.T) { var c capture ensure.NotNil(&c, nil) c.Equal(t, `ensure_test.go:137: expected a value but got nil`) }
func TestErrorCases(t *testing.T) { cases := []struct { Request *http.Request Body interface{} Error string StatusCode int Transport http.RoundTripper }{ { Request: &http.Request{ Method: "GET", URL: &url.URL{ Scheme: "https", Host: "api.parse.com", Path: "/1/classes/Foo/Bar", }, }, Error: `parse: api error with code=101 and message="object not found for get"`, StatusCode: http.StatusNotFound, Transport: transportFunc(func(r *http.Request) (*http.Response, error) { j := jsonB(t, parse.Error{ Code: 101, Message: "object not found for get", }) return &http.Response{ StatusCode: http.StatusNotFound, Status: "404 Not Found", Body: ioutil.NopCloser(bytes.NewReader(j)), }, nil }), }, { Request: &http.Request{ Method: "GET", URL: &url.URL{ Scheme: "https", Host: "api.parse.com", Path: "/1/classes/Foo/Bar", }, }, Body: map[int]int{}, Error: "unsupported type: map[int]int", Transport: transportFunc(func(r *http.Request) (*http.Response, error) { panic("not reached") }), }, { Request: &http.Request{ Method: "GET", URL: &url.URL{Path: "/"}, }, Error: `error with status=404 and body="<html>`, StatusCode: 404, Transport: transportFunc(func(r *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusNotFound, Status: "404 Not Found", Body: ioutil.NopCloser(strings.NewReader("<html>")), }, nil }), }, } t.Parallel() for _, ec := range cases { c := &parse.Client{ Credentials: defaultRestAPIKey, } if !realTransport { c.Transport = ec.Transport } res, err := c.Do(ec.Request, ec.Body, nil) ensure.NotNil(t, err) ensure.StringContains(t, err.Error(), ec.Error, ec) if ec.StatusCode != 0 { ensure.False(t, res == nil, ec) ensure.DeepEqual(t, res.StatusCode, ec.StatusCode, ec) } } }