Browse Source

Rework/fix curl error, add tests

Volodymyr Tkach 2 years ago
parent
commit
701e06e1af
2 changed files with 37 additions and 15 deletions
  1. 15 15
      utils/http/helpers/curl.go
  2. 22 0
      utils/http/helpers/helpers_test.go

+ 15 - 15
utils/http/helpers/curl.go

@@ -9,23 +9,23 @@ import (
 )
 )
 
 
 var (
 var (
-	ErrCurlGetStatus = errCurlGetStatus(0, 0)
+	ErrCurlGetStatus = error(&CurlGetStatusError{})
 )
 )
 
 
-func errCurlGetStatus(e, r int) error {
-	return &curlGetStatusError{
-		expected: e,
-		received: r,
-	}
+type CurlGetStatusError struct {
+	Expected int
+	Received int
 }
 }
 
 
-type curlGetStatusError struct {
-	expected int
-	received int
+func (e *CurlGetStatusError) Is(err error) bool {
+	if _, ok := err.(*CurlGetStatusError); !ok {
+		return false
+	}
+	return true
 }
 }
 
 
-func (c *curlGetStatusError) Error() string {
-	return fmt.Sprintf("CurlGet: expected %d, received %d", c.received, c.expected)
+func (e *CurlGetStatusError) Error() string {
+	return fmt.Sprintf("CurlGet: expected %d, received %d", e.Expected, e.Received)
 }
 }
 
 
 func CurlGet(ctx context.Context, url string, timeout time.Duration) ([]byte, error) {
 func CurlGet(ctx context.Context, url string, timeout time.Duration) ([]byte, error) {
@@ -48,10 +48,10 @@ func CurlGet(ctx context.Context, url string, timeout time.Duration) ([]byte, er
 	defer resp.Body.Close()
 	defer resp.Body.Close()
 
 
 	if resp.StatusCode != http.StatusOK {
 	if resp.StatusCode != http.StatusOK {
-		return b, errCurlGetStatus(
-			http.StatusOK,
-			resp.StatusCode,
-		)
+		return b, error(&CurlGetStatusError{
+			Expected: http.StatusOK,
+			Received: resp.StatusCode,
+		})
 	}
 	}
 
 
 	b, err = io.ReadAll(resp.Body)
 	b, err = io.ReadAll(resp.Body)

+ 22 - 0
utils/http/helpers/helpers_test.go

@@ -1,8 +1,10 @@
 package helpers_test
 package helpers_test
 
 
 import (
 import (
+	"errors"
 	"fmt"
 	"fmt"
 	"io"
 	"io"
+	"io/fs"
 	"net/http"
 	"net/http"
 	"net/http/httptest"
 	"net/http/httptest"
 	"testing"
 	"testing"
@@ -13,6 +15,26 @@ import (
 )
 )
 
 
 var _ = Describe("helpers", func() {
 var _ = Describe("helpers", func() {
+	Context("CurlGetStatusError", func() {
+		It("recognize error", func() {
+			err := error(&helpers.CurlGetStatusError{Expected: http.StatusOK, Received: http.StatusServiceUnavailable})
+			Expect(errors.Is(err, helpers.ErrCurlGetStatus)).To(BeTrue())
+
+			err = error(&helpers.CurlGetStatusError{Expected: http.StatusOK, Received: http.StatusBadGateway})
+			Expect(errors.Is(err, helpers.ErrCurlGetStatus)).To(BeTrue())
+
+			err = fmt.Errorf("Some error")
+			Expect(errors.Is(err, helpers.ErrCurlGetStatus)).To(BeFalse())
+
+			Expect(errors.Is(fs.ErrNotExist, helpers.ErrCurlGetStatus)).To(BeFalse())
+		})
+
+		It("generate error message", func() {
+			err := error(&helpers.CurlGetStatusError{Expected: http.StatusOK, Received: http.StatusBadGateway})
+			Expect(err.Error()).To(Equal("CurlGet: expected 200, received 502"))
+		})
+	})
+
 	Context("ClientIP", func() {
 	Context("ClientIP", func() {
 		It("return client IP", func() {
 		It("return client IP", func() {
 			Expect(helpers.ClientIP(&http.Request{
 			Expect(helpers.ClientIP(&http.Request{