-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
60 lines (49 loc) · 1.44 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
52
53
54
55
56
57
58
59
60
package trustpilot_test
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/dietdoctor/go-trustpilot"
"github.com/stretchr/testify/assert"
)
func setup() (*http.ServeMux, *trustpilot.Client, func()) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
u, _ := url.Parse(server.URL)
client, _ := trustpilot.NewClient(trustpilot.APIBaseURL(u), trustpilot.InvitationAPIBaseURL(u), trustpilot.Debug(true))
teardown := func() {
server.Close()
}
return mux, client, teardown
}
func fixture(path string) string {
b, err := ioutil.ReadFile("testdata/fixtures/" + path)
if err != nil {
panic(err)
}
return string(b)
}
func TestCreateInvitation(t *testing.T) {
mux, client, teardown := setup()
defer teardown()
businessUnitID := "123"
mux.HandleFunc(fmt.Sprintf("/private/business-units/%s/email-invitations", businessUnitID),
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, fixture("error_response.json"))
},
)
_, err := client.CreateInvitation(context.Background(), &trustpilot.CreateInvitationRequest{
BusinessUnitID: businessUnitID,
ConsumerEmail: "foobar@invalid",
ReferenceNumber: "users/12344",
ConsumerName: "John Doe",
Locale: "en-US",
})
assert.EqualError(t, err, "INVALID_ARGUMENT: 'Consumer Email' is not in the correct format.")
}