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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
package dnsimple
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestDomains_domainPath(t *testing.T) {
var pathTests = []struct {
input interface{}
expected string
}{
{nil, "domains"},
{"example.com", "domains/example.com"},
{1, "domains/1"},
}
for _, pt := range pathTests {
actual := domainPath(pt.input)
if actual != pt.expected {
t.Errorf("domainPath(%+v): expected %s, actual %s", pt.input, pt.expected)
}
}
}
func TestDomainsService_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/domains", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"domain":{"id": 1, "name":"example.com"}}]`)
})
domains, _, err := client.Domains.List()
if err != nil {
t.Errorf("Domains.List returned error: %v", err)
}
want := []Domain{{Id: 1, Name: "example.com"}}
if !reflect.DeepEqual(domains, want) {
t.Errorf("Domains.List returned %+v, want %+v", domains, want)
}
}
func TestDomainsService_Create(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/domains", func(w http.ResponseWriter, r *http.Request) {
want := make(map[string]interface{})
want["domain"] = map[string]interface{}{"name": "example.com"}
testMethod(t, r, "POST")
testRequestJSON(t, r, want)
w.WriteHeader(http.StatusCreated)
fmt.Fprintf(w, `{"domain":{"id":1, "name":"example.com"}}`)
})
domainValues := Domain{Name: "example.com"}
domain, _, err := client.Domains.Create(domainValues)
if err != nil {
t.Errorf("Domains.Create returned error: %v", err)
}
want := Domain{Id: 1, Name: "example.com"}
if !reflect.DeepEqual(domain, want) {
t.Fatalf("Domains.Create returned %+v, want %+v", domain, want)
}
}
func TestDomainsService_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/domains/example.com", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"domain": {"id":1, "name":"example.com"}}`)
})
domain, _, err := client.Domains.Get("example.com")
if err != nil {
t.Errorf("Domains.Get returned error: %v", err)
}
want := Domain{Id: 1, Name: "example.com"}
if !reflect.DeepEqual(domain, want) {
t.Fatalf("Domains.Get returned %+v, want %+v", domain, want)
}
}
func TestDomainsService_Delete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v1/domains/example.com", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
// fmt.Fprint(w, `{}`)
})
_, err := client.Domains.Delete("example.com")
if err != nil {
t.Errorf("Domains.Delete returned error: %v", err)
}
}
|