1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/api/errors/errors_test.go
chchliang f157b54606 add testcase with api/errors/errors_test.go
Signed-off-by: chchliang <chen.chuanliang@zte.com.cn>
2017-05-02 17:03:31 +08:00

64 lines
1.5 KiB
Go

package errors
import (
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
func newError(errorname string) error {
return fmt.Errorf("test%v", errorname)
}
func TestErrors(t *testing.T) {
errmsg := newError("apiError")
err := apiError{
error: errmsg,
statusCode: 0,
}
assert.Equal(t, err.HTTPErrorStatusCode(), err.statusCode)
errmsg = newError("ErrorWithStatusCode")
errcode := 1
serr := NewErrorWithStatusCode(errmsg, errcode)
apierr, ok := serr.(apiError)
if !ok {
t.Fatal("excepted err is apiError type")
}
assert.Equal(t, errcode, apierr.statusCode)
errmsg = newError("NewBadRequestError")
baderr := NewBadRequestError(errmsg)
apierr, ok = baderr.(apiError)
if !ok {
t.Fatal("excepted err is apiError type")
}
assert.Equal(t, http.StatusBadRequest, apierr.statusCode)
errmsg = newError("RequestForbiddenError")
ferr := NewRequestForbiddenError(errmsg)
apierr, ok = ferr.(apiError)
if !ok {
t.Fatal("excepted err is apiError type")
}
assert.Equal(t, http.StatusForbidden, apierr.statusCode)
errmsg = newError("RequestNotFoundError")
nerr := NewRequestNotFoundError(errmsg)
apierr, ok = nerr.(apiError)
if !ok {
t.Fatal("excepted err is apiError type")
}
assert.Equal(t, http.StatusNotFound, apierr.statusCode)
errmsg = newError("RequestConflictError")
cerr := NewRequestConflictError(errmsg)
apierr, ok = cerr.(apiError)
if !ok {
t.Fatal("excepted err is apiError type")
}
assert.Equal(t, http.StatusConflict, apierr.statusCode)
}