| 1 |
package dnsimple |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"errors" |
| 6 |
"io/ioutil" |
| 7 |
"net/http" |
| 8 |
"net/http/httptest" |
| 9 |
"net/url" |
| 10 |
"reflect" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
var ( |
| 15 |
// mux is the HTTP request multiplexer used with the test server. |
| 16 |
mux *http.ServeMux |
| 17 |
|
| 18 |
// client is the DNSimple client being tested. |
| 19 |
client *Client |
| 20 |
|
| 21 |
// server is a test HTTP server used to provide mock API responses. |
| 22 |
server *httptest.Server |
| 23 |
) |
| 24 |
|
| 25 |
// This method of testing http client APIs is borrowed from |
| 26 |
// Will Norris's work in go-github @ https://github.com/google/go-github |
| 27 |
func setup() { |
| 28 |
mux = http.NewServeMux() |
| 29 |
server = httptest.NewServer(mux) |
| 30 |
|
| 31 |
client = NewClient("mytoken", "[email protected]") |
| 32 |
client.BaseURL = server.URL + "/" |
| 33 |
} |
| 34 |
|
| 35 |
func teardown() { |
| 36 |
server.Close() |
| 37 |
} |
| 38 |
|
| 39 |
func testMethod(t *testing.T, r *http.Request, want string) { |
| 40 |
if want != r.Method { |
| 41 |
t.Errorf("Request method = %v, want %v", r.Method, want) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func testRequestJSON(t *testing.T, r *http.Request, values map[string]interface{}) { |
| 46 |
var dat map[string]interface{} |
| 47 |
|
| 48 |
body, _ := ioutil.ReadAll(r.Body) |
| 49 |
|
| 50 |
if err := json.Unmarshal(body, &dat); err != nil { |
| 51 |
t.Errorf("Could not decode json body: %v", err) |
| 52 |
} |
| 53 |
|
| 54 |
if !reflect.DeepEqual(values, dat) { |
| 55 |
t.Errorf("Request parameters = %v, want %v", dat, values) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
type values map[string]string |
| 60 |
|
| 61 |
func testFormValues(t *testing.T, r *http.Request, values values) { |
| 62 |
want := url.Values{} |
| 63 |
for k, v := range values { |
| 64 |
want.Add(k, v) |
| 65 |
} |
| 66 |
|
| 67 |
r.ParseForm() |
| 68 |
if !reflect.DeepEqual(want, r.Form) { |
| 69 |
t.Errorf("Request parameters = %v, want %v", r.Form, want) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
func testString(t *testing.T, test, value, want string) { |
| 74 |
if value != want { |
| 75 |
t.Errorf("%s returned %+v, want %+v", test, value, want) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
func TestNewClient(t *testing.T) { |
| 80 |
c := NewClient("mytoken", "[email protected]") |
| 81 |
|
| 82 |
if c.BaseURL != baseURL { |
| 83 |
t.Errorf("NewClient BaseURL = %v, want %v", c.BaseURL, baseURL) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func TestNewAuthenticatedClient(t *testing.T) { |
| 88 |
c := NewAuthenticatedClient(NewApiTokenCredentials("[email protected]", "mytoken")) |
| 89 |
|
| 90 |
if c.BaseURL != baseURL { |
| 91 |
t.Errorf("NewClient BaseURL = %v, want %v", c.BaseURL, baseURL) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func TestNewRequest(t *testing.T) { |
| 96 |
c := NewClient("mytoken", "[email protected]") |
| 97 |
c.BaseURL = "https://go.example.com/" |
| 98 |
|
| 99 |
inURL, outURL := "foo", "https://go.example.com/v1/foo" |
| 100 |
req, _ := c.NewRequest("GET", inURL, nil) |
| 101 |
|
| 102 |
// test that relative URL was expanded with the proper BaseURL |
| 103 |
if req.URL.String() != outURL { |
| 104 |
t.Errorf("makeRequest(%v) URL = %v, want %v", inURL, req.URL, outURL) |
| 105 |
} |
| 106 |
|
| 107 |
// test that default user-agent is attached to the request |
| 108 |
userAgent := req.Header.Get("User-Agent") |
| 109 |
if c.UserAgent != userAgent { |
| 110 |
t.Errorf("makeRequest() User-Agent = %v, want %v", userAgent, c.UserAgent) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
type badObject struct { |
| 115 |
} |
| 116 |
|
| 117 |
func (o *badObject) MarshalJSON() ([]byte, error) { |
| 118 |
return nil, errors.New("Bad object is bad") |
| 119 |
} |
| 120 |
|
| 121 |
func TestNewRequestWithBody(t *testing.T) { |
| 122 |
c := NewClient("mytoken", "[email protected]") |
| 123 |
c.BaseURL = "https://go.example.com/" |
| 124 |
|
| 125 |
inURL, _ := "foo", "https://go.example.com/v1/foo" |
| 126 |
badObject := badObject{} |
| 127 |
_, err := c.NewRequest("GET", inURL, &badObject) |
| 128 |
|
| 129 |
if err == nil { |
| 130 |
t.Errorf("NewRequest with body expected error with blank string") |
| 131 |
} |
| 132 |
} |