-
Notifications
You must be signed in to change notification settings - Fork 17
/
client_test.go
51 lines (44 loc) · 1.22 KB
/
client_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
package osrm
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func Test_getWithError(t *testing.T) {
c := newClient("/", &http.Client{})
b, err := c.get(context.Background(), "/")
require.Nil(t, b)
require.NotNil(t, err)
}
func Test_doRequestWithBadHTTPCode(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
fmt.Fprint(w, "<html><head>")
}))
defer ts.Close()
c := newClient(ts.URL, &http.Client{})
req := request{
profile: "something",
coords: geometry,
service: "foobar",
}
err := c.doRequest(context.Background(), &req, nil)
require.EqualError(t, err, "unexpected http status code 500 with body \"<html><head>\"")
}
func Test_doRequestWithBodyUnmarshalFailure(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}))
defer ts.Close()
c := newClient(ts.URL, &http.Client{})
req := request{
profile: "something",
coords: geometry,
service: "foobar",
}
err := c.doRequest(context.Background(), &req, nil)
require.EqualError(t, err, "failed to unmarshal body \"\": unexpected end of JSON input")
}