2020-04-12 13:05:28 +03:00
|
|
|
// 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"
|
2021-09-08 20:47:08 +01:00
|
|
|
"strings"
|
2020-04-12 13:05:28 +03:00
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func testClient(t *testing.T) *Client {
|
|
|
|
|
c := NewClient("apiKey")
|
|
|
|
|
c.SetRateLimit(1000)
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestClient_do(t *testing.T) {
|
|
|
|
|
c := testClient(t)
|
|
|
|
|
url := mockErrorResponse(404).URL
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2020-04-17 23:12:07 +03:00
|
|
|
err = c.do(req, nil)
|
2020-04-12 13:05:28 +03:00
|
|
|
var e *HTTPClientError
|
|
|
|
|
if errors.Is(err, e) {
|
|
|
|
|
t.Errorf("should be an http error, but was not: %v", err)
|
|
|
|
|
}
|
2020-04-17 23:12:07 +03:00
|
|
|
err = c.do(nil, nil)
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Errorf("there should be an error, but was nil")
|
|
|
|
|
}
|
2020-04-12 13:05:28 +03:00
|
|
|
}
|
2021-09-08 20:47:08 +01:00
|
|
|
|
|
|
|
|
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(t)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|