File: api_test.go

package info (click to toggle)
golang-github-pearkes-cloudflare 0.0~git20160103.765ac18-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 136 kB
  • sloc: makefile: 2
file content (101 lines) | stat: -rw-r--r-- 1,956 bytes parent folder | download
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
package cloudflare

import (
	"os"
	"testing"

	. "github.com/go-check/check"
	"github.com/pearkes/cloudflare/testutil"
)

type S struct {
	client *Client
}

var _ = Suite(&S{})

var testServer = testutil.NewHTTPServer()

func (s *S) SetUpSuite(c *C) {
	testServer.Start()
	var err error
	s.client, err = NewClient("foobar", "foobar")
	s.client.URL = "http://localhost:4444"
	if err != nil {
		panic(err)
	}
}

func (s *S) TearDownTest(c *C) {
	testServer.Flush()
}

func makeClient(t *testing.T) *Client {
	client, err := NewClient("foobaremail", "foobartoken")

	if err != nil {
		t.Fatalf("err: %v", err)
	}

	if client.Token != "foobartoken" {
		t.Fatalf("token not set on client: %s", client.Token)
	}

	if client.Email != "foobaremail" {
		t.Fatalf("email not set on client: %s", client.Token)
	}

	return client
}

func Test_NewClient_env(t *testing.T) {
	os.Setenv("CLOUDFLARE_TOKEN", "bar")
	os.Setenv("CLOUDFLARE_EMAIL", "bar")
	client, err := NewClient("", "")

	if err != nil {
		t.Fatalf("err: %v", err)
	}

	if client.Token != "bar" {
		t.Fatalf("token not set on client: %s", client.Token)
	}

	if client.Email != "bar" {
		t.Fatalf("email not set on client: %s", client.Email)
	}
}

func TestClient_NewRequest(t *testing.T) {
	c := makeClient(t)

	params := map[string]string{
		"foo": "bar",
		"baz": "bar",
	}
	req, err := c.NewRequest(params, "POST", "bar")
	if err != nil {
		t.Fatalf("bad: %v", err)
	}

	encoded := req.URL.Query()
	if encoded.Get("foo") != "bar" {
		t.Fatalf("bad: %v", encoded)
	}

	if encoded.Get("baz") != "bar" {
		t.Fatalf("bad: %v", encoded)
	}

	if encoded.Get("a") != "bar" {
		t.Fatalf("bad: %v", encoded)
	}
	expected := "https://www.cloudflare.com/api_json.html?a=bar&baz=bar&email=foobaremail&foo=bar&tkn=foobartoken"
	if req.URL.String() != expected {
		t.Fatalf("bad base url: %v\n\nexpected: %v", req.URL.String(), expected)
	}

	if req.Method != "POST" {
		t.Fatalf("bad method: %v", req.Method)
	}
}