-
Notifications
You must be signed in to change notification settings - Fork 25
/
error_test.go
41 lines (36 loc) · 1.2 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package midtrans
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"testing"
assert "github.com/stretchr/testify/require"
)
func TestErrorStruct(t *testing.T) {
err := &Error{
Message: "Error Test Message",
StatusCode: 200,
RawError: errors.New("TEST FROM GO ERROR"),
RawApiResponse: nil,
}
var midError *Error
assert.Error(t, err)
assert.True(t, true, errors.Is(err, err))
assert.True(t, true, errors.As(err, &midError))
assert.Equal(t, `Error Test Message`, err.GetMessage())
assert.Equal(t, 200, err.GetStatusCode())
assert.Equal(t, "Error Test Message: TEST FROM GO ERROR", err.Error())
}
func TestErrorResponse(t *testing.T) {
serverKey := "dummy"
c := GetHttpClient(Environment)
jsonReq, _ := json.Marshal("{\"transaction_details\": {\"order_id\": \"TEST-1648108994111\", \"gross_amount\": 10000}}")
err := c.Call(http.MethodPost, "https://app.midtrans.com/snap/v1/transactions", &serverKey, nil, bytes.NewBuffer(jsonReq), nil)
var midError *Error
assert.True(t, true, errors.Is(err, err))
assert.True(t, true, errors.As(err, &midError))
assert.Error(t, err)
assert.Equal(t, 401, err.StatusCode)
assert.Equal(t, "app.midtrans.com", err.RawApiResponse.Request.Host)
}