1
0
mirror of synced 2024-11-22 13:06:05 +03:00
mg-transport-api-client-go/v1/errors_test.go

67 lines
1.7 KiB
Go
Raw Normal View History

2022-10-25 17:32:46 +03:00
package v1
import (
2022-10-26 10:54:54 +03:00
"bytes"
2022-10-25 17:32:46 +03:00
"errors"
2022-10-26 10:54:54 +03:00
"fmt"
2022-10-28 10:05:11 +03:00
"io/ioutil"
2022-10-26 10:54:54 +03:00
"net/http"
2022-10-25 17:32:46 +03:00
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewCriticalHTTPError(t *testing.T) {
err := &url.Error{Op: "Get", URL: "http//example.com", Err: errors.New("EOF")}
httpErr := NewCriticalHTTPError(err)
2022-10-27 09:28:07 +03:00
assert.IsType(t, new(HTTPClientError), httpErr)
2022-10-25 17:32:46 +03:00
assert.IsType(t, new(url.Error), errors.Unwrap(httpErr))
assert.IsType(t, new(url.Error), errors.Unwrap(httpErr))
2022-10-26 18:07:04 +03:00
assert.Equal(t, httpErr.Error(), fmt.Sprintf("%s: %s", defaultErrorMessage, err.Error()))
2022-10-25 17:32:46 +03:00
}
func TestNewApiClientError(t *testing.T) {
body := []byte(`{"errors" : ["Channel not found"]}`)
httpErr := NewAPIClientError(body)
2022-10-27 09:28:07 +03:00
assert.IsType(t, new(HTTPClientError), httpErr)
2022-10-25 17:32:46 +03:00
assert.Equal(t, httpErr.Error(), "Channel not found")
body = []byte{}
httpErr = NewAPIClientError(body)
2022-10-27 09:28:07 +03:00
assert.IsType(t, new(HTTPClientError), httpErr)
2022-10-25 17:32:46 +03:00
assert.Equal(t, httpErr.Error(), internalServerError)
2022-10-26 10:54:54 +03:00
}
func TestNewServerError(t *testing.T) {
body := []byte(`{"errors" : ["Something went wrong"]}`)
response := new(http.Response)
2022-10-28 10:05:11 +03:00
response.Body = ioutil.NopCloser(bytes.NewReader(body))
2022-10-26 10:54:54 +03:00
serverErr := NewServerError(response)
2022-10-27 09:28:07 +03:00
assert.IsType(t, new(HTTPClientError), serverErr)
2022-10-26 10:54:54 +03:00
assert.Equal(t, serverErr.Error(), "Something went wrong")
2022-10-27 09:28:07 +03:00
var err *HTTPClientError
2022-10-26 10:54:54 +03:00
if errors.As(serverErr, &err) {
2022-10-26 18:07:04 +03:00
assert.NotNil(t, err.Response)
2022-10-26 10:54:54 +03:00
} else {
t.Fatal("Unexpected type of error")
}
2022-10-26 18:07:04 +03:00
body = []byte(`{"invalid_json"`)
response = new(http.Response)
2022-10-28 10:05:11 +03:00
response.Body = ioutil.NopCloser(bytes.NewReader(body))
2022-10-26 18:07:04 +03:00
serverErr = NewServerError(response)
2022-10-27 09:28:07 +03:00
assert.IsType(t, new(HTTPClientError), serverErr)
2022-10-26 18:07:04 +03:00
assert.Equal(t, serverErr.Error(), marshalError)
if errors.As(serverErr, &err) {
assert.NotNil(t, err.Response)
}
2022-10-26 10:54:54 +03:00
}