File: api_test.go

package info (click to toggle)
golang-github-circonus-labs-circonus-gometrics 0.0~git20160822.0.2f5be48-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 236 kB
  • ctags: 202
  • sloc: makefile: 2
file content (87 lines) | stat: -rw-r--r-- 2,227 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
// Copyright 2016 Circonus, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package api

import (
	"errors"
	"os"
	"strings"
	"testing"
)

func TestNewAPI(t *testing.T) {
	t.Log("Testing correct error return when no API config supplied")

	expectedError := errors.New("Invalid API configuration (nil)")

	_, err := NewAPI(nil)

	if err == nil || err.Error() != expectedError.Error() {
		t.Errorf("Expected an '%#v' error, got '%#v'", expectedError, err)
	}

	t.Log("Testing correct error return when no API Token supplied")

	expectedError = errors.New("API Token is required")

	ac := &Config{}
	ac.TokenKey = ""
	_, err = NewAPI(ac)

	if err == nil || err.Error() != expectedError.Error() {
		t.Errorf("Expected an '%#v' error, got '%#v'", expectedError, err)
	}

	t.Log("Testing correct error return when INVALID API Token supplied")

	ac = &Config{}
	ac.TokenKey = "abc-123"
	apih, err := NewAPI(ac)
	if err != nil {
		t.Errorf("Expected no error, got '%v'", err)
	}

	_, err = apih.Get("/user/current")
	if err == nil || !strings.Contains(err.Error(), "The authentication token you supplied is invalid") {
		t.Errorf("Expected an error containing 'The authentication token you supplied is invalid' error, got '%#v'", err)
	}
}

func TestApiGet(t *testing.T) {
	if os.Getenv("CIRCONUS_API_TOKEN") == "" {
		t.Skip("skipping test; $CIRCONUS_API_TOKEN not set")
	}

	t.Log("Testing correct API call to /user/current [defaults]")

	ac := &Config{}
	ac.TokenKey = os.Getenv("CIRCONUS_API_TOKEN")
	ac.TokenApp = os.Getenv("CIRCONUS_API_APP")

	apih, err := NewAPI(ac)
	if err != nil {
		t.Errorf("Expected no error, got '%v'", err)
	}

	if _, err := apih.Get("/user/current"); err != nil {
		t.Errorf("Expected no error, got '%v'", err)
	}

	t.Log("Testing correct API call to /user/current [url=hostname]")

	ac = &Config{}
	ac.TokenKey = os.Getenv("CIRCONUS_API_TOKEN")
	ac.TokenApp = os.Getenv("CIRCONUS_API_APP")
	ac.URL = "api.circonus.com"
	apih, err = NewAPI(ac)
	if err != nil {
		t.Errorf("Expected no error, got '%v'", err)
	}

	if _, err := apih.Get("/user/current"); err != nil {
		t.Errorf("Expected no error, got '%v'", err)
	}

}