| 1 | package dnsmadeeasy |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func makeClient(t *testing.T) *Client { |
| 12 | client, err := NewClient("aaaaaa1a-11a1-1aa1-a101-11a1a11aa1aa", |
| 13 | "11a0a11a-a1a1-111a-a11a-a11110a11111") |
| 14 | |
| 15 | if err != nil { |
| 16 | t.Fatalf("err: %v", err) |
| 17 | } |
| 18 | |
| 19 | if client.AKey != "aaaaaa1a-11a1-1aa1-a101-11a1a11aa1aa" { |
| 20 | t.Fatalf("api key not set on client: %s", client.AKey) |
| 21 | } |
| 22 | |
| 23 | if client.SKey != "11a0a11a-a1a1-111a-a11a-a11110a11111" { |
| 24 | t.Fatalf("secret key not set on client: %s", client.SKey) |
| 25 | } |
| 26 | |
| 27 | return client |
| 28 | } |
| 29 | |
| 30 | func TestClient_NewRequest(t *testing.T) { |
| 31 | c := makeClient(t) |
| 32 | |
| 33 | body := bytes.NewBuffer(nil) |
| 34 | req, err := c.NewRequest("POST", "/bar", body, "Thu, 04 Dec 2014 11:02:57 GMT") |
| 35 | if err != nil { |
| 36 | t.Fatalf("bad: %v", err) |
| 37 | } |
| 38 | |
| 39 | if req.URL.String() != "https://api.dnsmadeeasy.com/V2.0/bar" { |
| 40 | t.Fatalf("bad base url: %v", req.URL.String()) |
| 41 | } |
| 42 | |
| 43 | if req.Header.Get("X-Dnsme-Apikey") != "aaaaaa1a-11a1-1aa1-a101-11a1a11aa1aa" { |
| 44 | t.Fatalf("bad auth header: %v", req.Header) |
| 45 | } |
| 46 | |
| 47 | if req.Header.Get("X-Dnsme-Requestdate") != "Thu, 04 Dec 2014 11:02:57 GMT" { |
| 48 | t.Fatalf("bad auth header: %v", req.Header) |
| 49 | } |
| 50 | |
| 51 | if req.Header.Get("X-Dnsme-Hmac") != "7a8c517d5eab84e524a537ce3a73e565cabf8f6a" { |
| 52 | t.Fatalf("bad auth header: %v", req.Header) |
| 53 | } |
| 54 | |
| 55 | if req.Method != "POST" { |
| 56 | t.Fatalf("bad method: %v", req.Method) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | type ClosingBuffer struct { |
| 61 | *bytes.Buffer |
| 62 | } |
| 63 | |
| 64 | func (cb *ClosingBuffer) Close() error { |
| 65 | return nil |
| 66 | } |
| 67 | |
| 68 | var errExample = &http.Response{ |
| 69 | Body: &ClosingBuffer{bytes.NewBufferString(`{"error":["Record with this type (A), name (test), and value (1.1.1.9) already exists."]}`)}, |
| 70 | } |
| 71 | |
| 72 | func Test_ParseError(t *testing.T) { |
| 73 | should := errors.New("API Error (0): Record with this type (A), name (test), and value (1.1.1.9) already exists.") |
| 74 | actual := parseError(errExample) |
| 75 | |
| 76 | if fmt.Sprintf("%v", should) != fmt.Sprintf("%v", actual) { |
| 77 | t.Fatalf("parseError\nshould: |%v|\nactual: |%v|\n", should, actual) |
| 78 | } |
| 79 | } |