| 1 |
package proton_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"encoding/json" |
| 6 |
"errors" |
| 7 |
"net" |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"net/url" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/henrybear327/go-proton-api" |
| 14 |
"github.com/henrybear327/go-proton-api/server" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
) |
| 17 |
|
| 18 |
func TestNetError_DropOnWrite(t *testing.T) { |
| 19 |
l, err := net.Listen("tcp", ":0") |
| 20 |
require.NoError(t, err) |
| 21 |
|
| 22 |
dropListener := proton.NewListener(l, proton.NewDropConn) |
| 23 |
|
| 24 |
// Use a custom listener that drops all writes. |
| 25 |
dropListener.SetCanWrite(false) |
| 26 |
|
| 27 |
// Simulate a server that refuses to write. |
| 28 |
s := server.New(server.WithListener(dropListener)) |
| 29 |
defer s.Close() |
| 30 |
|
| 31 |
m := proton.New(proton.WithHostURL(s.GetHostURL())) |
| 32 |
defer m.Close() |
| 33 |
|
| 34 |
// This should fail with a URL error. |
| 35 |
pingErr := m.Ping(context.Background()) |
| 36 |
|
| 37 |
if urlErr := new(url.Error); !errors.As(pingErr, &urlErr) { |
| 38 |
t.Fatalf("expected a url.Error, got %T: %v", pingErr, pingErr) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
func TestAPIError_DeserializeWithoutDetails(t *testing.T) { |
| 43 |
errJson := ` |
| 44 |
{ |
| 45 |
"Status": 400, |
| 46 |
"Code": 1000, |
| 47 |
"Error": "Foo Bar" |
| 48 |
} |
| 49 |
` |
| 50 |
var err proton.APIError |
| 51 |
|
| 52 |
require.NoError(t, json.Unmarshal([]byte(errJson), &err)) |
| 53 |
require.Nil(t, err.Details) |
| 54 |
} |
| 55 |
|
| 56 |
func TestAPIError_DeserializeWithoutDetailsValue(t *testing.T) { |
| 57 |
errJson := ` |
| 58 |
{ |
| 59 |
"Status": 400, |
| 60 |
"Code": 1000, |
| 61 |
"Error": "Foo Bar", |
| 62 |
"Details": 20 |
| 63 |
} |
| 64 |
` |
| 65 |
var err proton.APIError |
| 66 |
|
| 67 |
require.NoError(t, json.Unmarshal([]byte(errJson), &err)) |
| 68 |
require.NotNil(t, err.Details) |
| 69 |
require.Equal(t, `20`, err.DetailsToString()) |
| 70 |
} |
| 71 |
|
| 72 |
func TestAPIError_DeserializeWithDetailsObject(t *testing.T) { |
| 73 |
errJson := ` |
| 74 |
{ |
| 75 |
"Status": 400, |
| 76 |
"Code": 1000, |
| 77 |
"Error": "Foo Bar", |
| 78 |
"Details": { |
| 79 |
"object2": { |
| 80 |
"v": 20 |
| 81 |
}, |
| 82 |
"foo": "bar" |
| 83 |
} |
| 84 |
} |
| 85 |
` |
| 86 |
var err proton.APIError |
| 87 |
|
| 88 |
require.NoError(t, json.Unmarshal([]byte(errJson), &err)) |
| 89 |
require.NotNil(t, err.Details) |
| 90 |
require.Equal(t, `{"foo":"bar","object2":{"v":20}}`, err.DetailsToString()) |
| 91 |
} |
| 92 |
|
| 93 |
func TestAPIError_DeserializeWithDetailsArray(t *testing.T) { |
| 94 |
errJson := ` |
| 95 |
{ |
| 96 |
"Status": 400, |
| 97 |
"Code": 1000, |
| 98 |
"Error": "Foo Bar", |
| 99 |
"Details": [ |
| 100 |
{ |
| 101 |
"object2": { |
| 102 |
"v": 20 |
| 103 |
}, |
| 104 |
"foo": "bar" |
| 105 |
}, |
| 106 |
499, |
| 107 |
"hello" |
| 108 |
] |
| 109 |
} |
| 110 |
` |
| 111 |
var err proton.APIError |
| 112 |
|
| 113 |
require.NoError(t, json.Unmarshal([]byte(errJson), &err)) |
| 114 |
require.NotNil(t, err.Details) |
| 115 |
require.Equal(t, `[{"foo":"bar","object2":{"v":20}},499,"hello"]`, err.DetailsToString()) |
| 116 |
} |
| 117 |
|
| 118 |
func TestNetError_RouteInErrorMessage(t *testing.T) { |
| 119 |
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 120 |
w.WriteHeader(http.StatusBadRequest) |
| 121 |
})) |
| 122 |
defer s.Close() |
| 123 |
|
| 124 |
m := proton.New(proton.WithHostURL(s.URL)) |
| 125 |
defer m.Close() |
| 126 |
|
| 127 |
pingErr := m.Quark(context.Background(), "test/ping") |
| 128 |
|
| 129 |
require.Error(t, pingErr) |
| 130 |
require.Contains(t, pingErr.Error(), "GET") |
| 131 |
require.Contains(t, pingErr.Error(), "/test/ping") |
| 132 |
} |