-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathclient_test.go
82 lines (75 loc) · 1.69 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright © 2020 Mike Berezin
//
// Use of this source code is governed by an MIT license.
// Details in the LICENSE file.
package airtable
import (
"errors"
"log"
"net/http"
"strings"
"testing"
)
func testClient() *Client {
c := NewClient("apiKey")
c.SetRateLimit(1000)
return c
}
func TestClient_do(t *testing.T) {
c := testClient()
url := mockErrorResponse(404).URL
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
err = c.do(req, nil)
var e *HTTPClientError
if errors.Is(err, e) {
t.Errorf("should be an http error, but was not: %v", err)
}
err = c.do(nil, nil)
if err == nil {
t.Errorf("there should be an error, but was nil")
}
}
func TestClient_SetBaseURL(t *testing.T) {
testCases := []struct {
description string
url string
expectedError string
}{
{
description: "accepts a valid URL",
url: "http://localhost:3000",
},
{
description: "accepts a valid HTTPS URL",
url: "https://example.com",
},
{
description: "rejects non http/https scheme url",
url: "ftp://example.com",
expectedError: "http or https baseURL must be used",
},
{
description: "rejects url without scheme",
url: "example.com",
expectedError: "scheme of http or https must be specified",
},
}
c := testClient()
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
err := c.SetBaseURL(testCase.url)
if err != nil {
if testCase.expectedError != "" {
if !strings.Contains(err.Error(), testCase.expectedError) {
t.Fatalf("unexpected error: %s", err)
}
} else {
t.Fatalf("unexpected error: %s", err)
}
}
})
}
}