-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosrm_test.go
111 lines (86 loc) · 2.47 KB
/
gosrm_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package gosrm
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
const invalidURL string = "postgres://user:abc{DEf1=ghi@example.com:5432/db?sslmode=require"
func newOSRMClient() OSRMClient {
return OSRMClient{client: NewHTTPClient(HTTPClientConfig{})}
}
func TestNew(t *testing.T) {
testCases := []struct {
name, baseURL string
ok bool
}{
{name: "ok", baseURL: "http://www.test.com:5000", ok: true},
{name: "not_ok", baseURL: invalidURL, ok: false},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
cli, err := New(testCase.baseURL)
if testCase.ok {
assert.NoError(t, err)
assert.Equal(t, "http", cli.baseURL.Scheme)
assert.Equal(t, "www.test.com:5000", cli.baseURL.Host)
} else {
assert.NotNil(t, err)
assert.Error(t, err)
}
})
}
}
func TestOSRMClient_SetHTTPClient(t *testing.T) {
osrm := newOSRMClient()
assert.PanicsWithValue(t, "http client can't be nil", func() {
osrm.SetHTTPClient(nil)
})
client := NewHTTPClient(HTTPClientConfig{MaxConcurrency: 10})
osrm.SetHTTPClient(client)
assert.Equal(t, client, osrm.client)
}
func TestOSRMClient_get(t *testing.T) {
osrm := newOSRMClient()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{\"message\": \"Ok\"}"))
}))
defer srv.Close()
res := make(map[string]any)
err := osrm.get(context.Background(), invalidURL, &res)
assert.Error(t, err)
err = osrm.get(context.Background(), "http://invalid_host", &res)
assert.Error(t, err)
err = osrm.get(context.Background(), srv.URL, &res)
assert.NoError(t, err)
assert.Equal(t, "Ok", res["message"])
}
func TestOSRMClient_applyOpts(t *testing.T) {
osrm := newOSRMClient()
u := url.URL{}
osrm.applyOpts(&u, []Option{
optionImpl(func(u *url.URL) {
u.Path = "test"
}),
optionImpl(func(u *url.URL) {
u.Path += "/test2"
}),
})
assert.Equal(t, "test/test2", u.Path)
}
func TestRequest_buildURLPath(t *testing.T) {
osrm, err := New("http://127.0.0.1:5000")
assert.NoError(t, err)
req := Request{
Coordinates: []Coordinate{{13.388860, 52.517037}, {13.397634, 52.529407}, {13.428555, 52.523219}},
Profile: ProfileCar,
}
u := req.buildURLPath(*osrm.baseURL, tripServiceURL)
assert.Equal(t, "/trip/v1/car/13.388860,52.517037;13.397634,52.529407;13.428555,52.523219.json", u.Path)
}
func getOSRMAddress() string {
return os.Getenv("OSRM_ADDRESS")
}