45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package as213905
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestOrganizationAndHeaders(t *testing.T) {
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if got := r.Header.Get("Authorization"); got != "Bearer secret" {
|
|
t.Errorf("authorization = %q", got)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"organization":{"id":"o","asn":213905,"name":"ISPLABS","status":"active","commit_kbps":50000}}`))
|
|
}))
|
|
defer s.Close()
|
|
c, err := NewWithOptions("secret", Options{BaseURL: s.URL})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
org, err := c.Organization(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if org.ASN != 213905 {
|
|
t.Fatalf("asn = %d", org.ASN)
|
|
}
|
|
}
|
|
|
|
func TestAPIError(t *testing.T) {
|
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(403)
|
|
_, _ = w.Write([]byte(`{"error":"scope_required","message":"need traffic:read"}`))
|
|
}))
|
|
defer s.Close()
|
|
c, _ := NewWithOptions("secret", Options{BaseURL: s.URL})
|
|
_, err := c.Traffic(context.Background(), nil)
|
|
e, ok := err.(*APIError)
|
|
if !ok || e.Code != "scope_required" || e.Status != 403 {
|
|
t.Fatalf("error = %#v", err)
|
|
}
|
|
}
|