Skip to content

Commit 74d9166

Browse files
authored
Merge pull request #2 from tiny-go/not-acceptable
Refactor tests, introduce NotAcceptable error
2 parents c393741 + f8d6ceb commit 74d9166

13 files changed

Lines changed: 279 additions & 108 deletions

400_bad_request_test.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1-
package errors
1+
package errors_test
22

33
import (
4-
"errors"
54
"net/http"
65
"testing"
6+
7+
"github.com/tiny-go/errors"
78
)
89

910
func TestNewBadRequest(t *testing.T) {
10-
t.Run("Check Bad Request status code", func(t *testing.T) {
11-
var err error
12-
err = NewBadRequest(errors.New("BadRequest"))
13-
typed, ok := err.(BadRequest)
14-
if !ok {
15-
t.Fatalf("error has invalid type: %T", err)
11+
t.Run("NewBadRequest", func(t *testing.T) {
12+
err := errors.NewBadRequest(errors.New("BadRequest"))
13+
14+
if err.Code() != http.StatusBadRequest {
15+
t.Errorf("error has invalid status code: %d", err.Code())
1616
}
17-
if typed.Code() != http.StatusBadRequest {
18-
t.Errorf("error has invalid status code: %d", typed.Code())
17+
18+
if err.Error() != "BadRequest" {
19+
t.Errorf("error has invalid message: %q", err.Error())
1920
}
20-
if typed.Error() != "BadRequest" {
21-
t.Errorf("error has invalid message: %q", typed.Error())
21+
})
22+
}
23+
24+
func TestBadRequestf(t *testing.T) {
25+
t.Run("BadRequestf", func(t *testing.T) {
26+
err := errors.BadRequestf("BadRequest: %d", http.StatusBadRequest)
27+
28+
if err.Code() != http.StatusBadRequest {
29+
t.Errorf("error has invalid status code: %d", err.Code())
30+
}
31+
32+
if err.Error() != "BadRequest: 400" {
33+
t.Errorf("error has invalid message: %q", err.Error())
2234
}
2335
})
2436
}

401_unauthorized_test.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1-
package errors
1+
package errors_test
22

33
import (
4-
"errors"
54
"net/http"
65
"testing"
6+
7+
"github.com/tiny-go/errors"
78
)
89

910
func TestNewUnauthorized(t *testing.T) {
10-
t.Run("Check Unauthorized status code", func(t *testing.T) {
11-
var err error
12-
err = NewUnauthorized(errors.New("Unauthorized"))
13-
typed, ok := err.(Unauthorized)
14-
if !ok {
15-
t.Fatalf("error has invalid type: %T", err)
11+
t.Run("NewUnauthorized", func(t *testing.T) {
12+
err := errors.NewUnauthorized(errors.New("Unauthorized"))
13+
14+
if err.Code() != http.StatusUnauthorized {
15+
t.Errorf("error has invalid status code: %d", err.Code())
1616
}
17-
if typed.Code() != http.StatusUnauthorized {
18-
t.Errorf("error has invalid status code: %d", typed.Code())
17+
18+
if err.Error() != "Unauthorized" {
19+
t.Errorf("error has invalid message: %q", err.Error())
1920
}
20-
if typed.Error() != "Unauthorized" {
21-
t.Errorf("error has invalid message: %q", typed.Error())
21+
})
22+
}
23+
24+
func TestUnauthorizedf(t *testing.T) {
25+
t.Run("Unauthorizedf", func(t *testing.T) {
26+
err := errors.Unauthorizedf("Unauthorized: %d", http.StatusUnauthorized)
27+
28+
if err.Code() != http.StatusUnauthorized {
29+
t.Errorf("error has invalid status code: %d", err.Code())
30+
}
31+
32+
if err.Error() != "Unauthorized: 401" {
33+
t.Errorf("error has invalid message: %q", err.Error())
2234
}
2335
})
2436
}

403_forbidden_test.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1-
package errors
1+
package errors_test
22

33
import (
4-
"errors"
54
"net/http"
65
"testing"
6+
7+
"github.com/tiny-go/errors"
78
)
89

910
func TestNewForbidden(t *testing.T) {
10-
t.Run("Check Forbidden status code", func(t *testing.T) {
11-
var err error
12-
err = NewForbidden(errors.New("Forbidden"))
13-
typed, ok := err.(Forbidden)
14-
if !ok {
15-
t.Fatalf("error has invalid type: %T", err)
11+
t.Run("NewForbidden", func(t *testing.T) {
12+
err := errors.NewForbidden(errors.New("Forbidden"))
13+
14+
if err.Code() != http.StatusForbidden {
15+
t.Errorf("error has invalid status code: %d", err.Code())
1616
}
17-
if typed.Code() != http.StatusForbidden {
18-
t.Errorf("error has invalid status code: %d", typed.Code())
17+
18+
if err.Error() != "Forbidden" {
19+
t.Errorf("error has invalid message: %q", err.Error())
1920
}
20-
if typed.Error() != "Forbidden" {
21-
t.Errorf("error has invalid message: %q", typed.Error())
21+
})
22+
}
23+
24+
func TestForbiddenf(t *testing.T) {
25+
t.Run("Forbiddenf", func(t *testing.T) {
26+
err := errors.Forbiddenf("Forbidden: %d", http.StatusForbidden)
27+
28+
if err.Code() != http.StatusForbidden {
29+
t.Errorf("error has invalid status code: %d", err.Code())
30+
}
31+
32+
if err.Error() != "Forbidden: 403" {
33+
t.Errorf("error has invalid message: %q", err.Error())
2234
}
2335
})
2436
}

404_not_found_test.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1-
package errors
1+
package errors_test
22

33
import (
4-
"errors"
54
"net/http"
65
"testing"
6+
7+
"github.com/tiny-go/errors"
78
)
89

910
func TestNewNotFound(t *testing.T) {
10-
t.Run("Check NotFound status code", func(t *testing.T) {
11-
var err error
12-
err = NewNotFound(errors.New("NotFound"))
13-
typed, ok := err.(NotFound)
14-
if !ok {
15-
t.Fatalf("error has invalid type: %T", err)
11+
t.Run("NewNotFound", func(t *testing.T) {
12+
err := errors.NewNotFound(errors.New("NotFound"))
13+
14+
if err.Code() != http.StatusNotFound {
15+
t.Errorf("error has invalid status code: %d", err.Code())
1616
}
17-
if typed.Code() != http.StatusNotFound {
18-
t.Errorf("error has invalid status code: %d", typed.Code())
17+
18+
if err.Error() != "NotFound" {
19+
t.Errorf("error has invalid message: %q", err.Error())
1920
}
20-
if typed.Error() != "NotFound" {
21-
t.Errorf("error has invalid message: %q", typed.Error())
21+
})
22+
}
23+
24+
func TestNotFoundf(t *testing.T) {
25+
t.Run("NotFoundf", func(t *testing.T) {
26+
err := errors.NotFoundf("NotFound: %d", http.StatusNotFound)
27+
28+
if err.Code() != http.StatusNotFound {
29+
t.Errorf("error has invalid status code: %d", err.Code())
30+
}
31+
32+
if err.Error() != "NotFound: 404" {
33+
t.Errorf("error has invalid message: %q", err.Error())
2234
}
2335
})
2436
}

405_method_not_allowed_test.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1-
package errors
1+
package errors_test
22

33
import (
4-
"errors"
54
"net/http"
65
"testing"
6+
7+
"github.com/tiny-go/errors"
78
)
89

910
func TestNewMethodNotAllowed(t *testing.T) {
10-
t.Run("Check MethodNotAllowed status code", func(t *testing.T) {
11-
var err error
12-
err = NewMethodNotAllowed(errors.New("MethodNotAllowed"))
13-
typed, ok := err.(MethodNotAllowed)
14-
if !ok {
15-
t.Fatalf("error has invalid type: %T", err)
11+
t.Run("NewMethodNotAllowed", func(t *testing.T) {
12+
err := errors.NewMethodNotAllowed(errors.New("MethodNotAllowed"))
13+
14+
if err.Code() != http.StatusMethodNotAllowed {
15+
t.Errorf("error has invalid status code: %d", err.Code())
1616
}
17-
if typed.Code() != http.StatusMethodNotAllowed {
18-
t.Errorf("error has invalid status code: %d", typed.Code())
17+
18+
if err.Error() != "MethodNotAllowed" {
19+
t.Errorf("error has invalid message: %q", err.Error())
1920
}
20-
if typed.Error() != "MethodNotAllowed" {
21-
t.Errorf("error has invalid message: %q", typed.Error())
21+
})
22+
}
23+
24+
func TestMethodNotAllowedf(t *testing.T) {
25+
t.Run("MethodNotAllowedf", func(t *testing.T) {
26+
err := errors.MethodNotAllowedf("MethodNotAllowed: %d", http.StatusMethodNotAllowed)
27+
28+
if err.Code() != http.StatusMethodNotAllowed {
29+
t.Errorf("error has invalid status code: %d", err.Code())
30+
}
31+
32+
if err.Error() != "MethodNotAllowed: 405" {
33+
t.Errorf("error has invalid message: %q", err.Error())
2234
}
2335
})
2436
}

406_not_acceptable.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package errors
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
var _ Error = NotAcceptable("")
9+
10+
// NotAcceptable is a 406 HTTP error.
11+
type NotAcceptable string
12+
13+
func (e NotAcceptable) Error() string { return string(e) }
14+
15+
// Code returns HTTP status code.
16+
func (e NotAcceptable) Code() int { return http.StatusNotAcceptable }
17+
18+
// NewMethodNotAllowed is a constructor func for 406 HTTP error.
19+
func NewNotAcceptable(cause error) NotAcceptable { return NotAcceptable(cause.Error()) }
20+
21+
// NotAcceptablef returns formatted NotAcceptable error.
22+
func NotAcceptablef(format string, args ...interface{}) NotAcceptable {
23+
return NotAcceptable(fmt.Sprintf(format, args...))
24+
}

406_not_acceptable_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package errors_test
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/tiny-go/errors"
8+
)
9+
10+
func TestNewNotAcceptable(t *testing.T) {
11+
t.Run("NewNotAcceptable", func(t *testing.T) {
12+
err := errors.NewNotAcceptable(errors.New("NotAcceptable"))
13+
14+
if err.Code() != http.StatusNotAcceptable {
15+
t.Errorf("error has invalid status code: %d", err.Code())
16+
}
17+
18+
if err.Error() != "NotAcceptable" {
19+
t.Errorf("error has invalid message: %q", err.Error())
20+
}
21+
})
22+
}
23+
24+
func TestNotAcceptablef(t *testing.T) {
25+
t.Run("NotAcceptablef", func(t *testing.T) {
26+
err := errors.NotAcceptablef("NotAcceptable: %d", http.StatusNotAcceptable)
27+
28+
if err.Code() != http.StatusNotAcceptable {
29+
t.Errorf("error has invalid status code: %d", err.Code())
30+
}
31+
32+
if err.Error() != "NotAcceptable: 406" {
33+
t.Errorf("error has invalid message: %q", err.Error())
34+
}
35+
})
36+
}

408_request_timeout_test.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
1-
package errors
1+
package errors_test
22

33
import (
4-
"errors"
54
"net/http"
65
"testing"
6+
7+
"github.com/tiny-go/errors"
78
)
89

910
func TestNewRequestTimeout(t *testing.T) {
10-
t.Run("Check RequestTimeout status code", func(t *testing.T) {
11-
var err error
12-
err = NewRequestTimeout(errors.New("RequestTimeout"))
13-
typed, ok := err.(RequestTimeout)
14-
if !ok {
15-
t.Fatalf("error has invalid type: %T", err)
11+
t.Run("NewRequestTimeout", func(t *testing.T) {
12+
err := errors.NewRequestTimeout(errors.New("RequestTimeout"))
13+
14+
if err.Code() != http.StatusRequestTimeout {
15+
t.Errorf("error has invalid status code: %d", err.Code())
1616
}
17-
if typed.Code() != http.StatusRequestTimeout {
18-
t.Errorf("error has invalid status code: %d", typed.Code())
17+
18+
if err.Error() != "RequestTimeout" {
19+
t.Errorf("error has invalid message: %q", err.Error())
1920
}
20-
if typed.Error() != "RequestTimeout" {
21-
t.Errorf("error has invalid message: %q", typed.Error())
21+
})
22+
}
23+
24+
func TestRequestTimeoutf(t *testing.T) {
25+
t.Run("RequestTimeoutf", func(t *testing.T) {
26+
err := errors.RequestTimeoutf("RequestTimeout: %d", http.StatusRequestTimeout)
27+
28+
if err.Code() != http.StatusRequestTimeout {
29+
t.Errorf("error has invalid status code: %d", err.Code())
30+
}
31+
32+
if err.Error() != "RequestTimeout: 408" {
33+
t.Errorf("error has invalid message: %q", err.Error())
2234
}
2335
})
2436
}

0 commit comments

Comments
 (0)