-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_test.go
63 lines (57 loc) · 1.39 KB
/
api_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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package np_shopping
import (
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFixturesTrackingDocument(t *testing.T) {
testCases := []struct {
name string
file string
documentID string
errorContains string
}{
{
name: "Tracked by number",
file: "fixtures/tracked_by_number.json",
documentID: "EV00000000000543NPI",
},
{
name: "Invalid number",
file: "fixtures/invalid_number.json",
errorContains: "document number is not correct",
},
{
name: "Internal error",
file: "fixtures/invalid_response.json",
errorContains: `something went wrong: {"invalid_field" : "invalid_value"}`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := os.ReadFile(tc.file)
if err != nil {
t.Fatal(err)
}
_, err = w.Write(b)
assert.NoError(t, err)
}))
defer server.Close()
np := Api{url: server.URL}
res, err := np.TrackByTrackingNumber(tc.documentID)
if tc.errorContains != "" {
assert.ErrorContains(t, err, tc.errorContains)
} else {
assert.NoError(t, err)
}
if tc.documentID == "" {
assert.Nil(t, res)
} else {
assert.Equal(t, tc.documentID, res.WaybillNumber)
}
})
}
}