| 1 |
package fastly |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io/ioutil" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
var testEventID = "3OMewexIMbzrQj77xxxxxx" |
| 10 |
|
| 11 |
func TestClient_APIEvents(t *testing.T) { |
| 12 |
t.Parallel() |
| 13 |
|
| 14 |
var err error |
| 15 |
var events GetAPIEventsResponse |
| 16 |
record(t, "events/get_events", func(c *Client) { |
| 17 |
events, err = c.GetAPIEvents(&GetAPIEventsFilterInput{ |
| 18 |
PageNumber: 1, |
| 19 |
MaxResults: 1, |
| 20 |
}) |
| 21 |
}) |
| 22 |
if err != nil { |
| 23 |
t.Fatal(err) |
| 24 |
} |
| 25 |
if len(events.Events) < 1 { |
| 26 |
t.Errorf("bad events: %v", events.Events) |
| 27 |
} |
| 28 |
|
| 29 |
var event *Event |
| 30 |
record(t, "events/get_event", func(c *Client) { |
| 31 |
event, err = c.GetAPIEvent(&GetAPIEventInput{ |
| 32 |
EventID: events.Events[0].ID, |
| 33 |
}) |
| 34 |
}) |
| 35 |
if err != nil { |
| 36 |
t.Fatal(err) |
| 37 |
} |
| 38 |
if len(event.ID) < 1 { |
| 39 |
t.Errorf("bad event: %v", event) |
| 40 |
} |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
func TestClient_GetAPIEvent_validation(t *testing.T) { |
| 45 |
var err error |
| 46 |
_, err = testClient.GetAPIEvent(&GetAPIEventInput{ |
| 47 |
EventID: "", |
| 48 |
}) |
| 49 |
if err != ErrMissingEventID { |
| 50 |
t.Errorf("bad error: %s", err) |
| 51 |
} |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
func TestGetAPIEventsFilterInput_formatFilters(t *testing.T) { |
| 56 |
tests := []struct { |
| 57 |
description string |
| 58 |
filters GetAPIEventsFilterInput |
| 59 |
expected map[string]string |
| 60 |
}{ |
| 61 |
{ |
| 62 |
description: "converts both strings and ints to strings", |
| 63 |
filters: GetAPIEventsFilterInput{ |
| 64 |
CustomerID: "65135846153687547", |
| 65 |
ServiceID: "5343548168357658", |
| 66 |
EventType: "version.activate", |
| 67 |
UserID: "654681384354746951", |
| 68 |
MaxResults: 1, |
| 69 |
PageNumber: 2, |
| 70 |
}, |
| 71 |
expected: map[string]string{ |
| 72 |
"filter[customer_id]": "65135846153687547", |
| 73 |
"filter[service_id]": "5343548168357658", |
| 74 |
"filter[event_type]": "version.activate", |
| 75 |
"filter[user_id]": "654681384354746951", |
| 76 |
"page[size]": "1", |
| 77 |
"page[number]": "2", |
| 78 |
}, |
| 79 |
}, |
| 80 |
} |
| 81 |
for _, testcase := range tests { |
| 82 |
answer := testcase.filters.formatEventFilters() |
| 83 |
if len(answer) != len(testcase.expected) { |
| 84 |
t.Errorf("In test %s: Expected map with %d entries,got one with %d", testcase.description, len(testcase.expected), len(answer)) |
| 85 |
} |
| 86 |
for key, value := range testcase.expected { |
| 87 |
if answer[key] != value { |
| 88 |
t.Errorf("In test %s: Expected %s key to have value %s, got %s", testcase.description, key, value, answer[key]) |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestGetEventsPages(t *testing.T) { |
| 95 |
tests := []struct { |
| 96 |
description string |
| 97 |
input string |
| 98 |
expectedPages EventsPaginationInfo |
| 99 |
expectedErr error |
| 100 |
}{ |
| 101 |
{ |
| 102 |
description: "returns the next page", |
| 103 |
input: `{"links": {"next": "https://google.com/2"}, "data": []}`, |
| 104 |
expectedPages: EventsPaginationInfo{ |
| 105 |
Next: "https://google.com/2", |
| 106 |
}, |
| 107 |
}, |
| 108 |
{ |
| 109 |
description: "returns multiple pages", |
| 110 |
input: `{"links": {"next": "https://google.com/2", "first": "https://google.com/1"}, "data": []}`, |
| 111 |
expectedPages: EventsPaginationInfo{ |
| 112 |
First: "https://google.com/1", |
| 113 |
Next: "https://google.com/2", |
| 114 |
}, |
| 115 |
}, |
| 116 |
{ |
| 117 |
description: "returns no pages", |
| 118 |
input: `{"data": []}`, |
| 119 |
expectedPages: EventsPaginationInfo{}, |
| 120 |
}, |
| 121 |
} |
| 122 |
for _, testcase := range tests { |
| 123 |
pages, reader, err := getEventsPages(bytes.NewReader([]byte(testcase.input))) |
| 124 |
if pages != testcase.expectedPages { |
| 125 |
t.Errorf("Test %s: Expected pages %+v, got %+v", testcase.description, testcase.expectedPages, pages) |
| 126 |
} |
| 127 |
|
| 128 |
// we expect to be able to get the original input out again |
| 129 |
resultBytes, _ := ioutil.ReadAll(reader) |
| 130 |
if string(resultBytes) != testcase.input { |
| 131 |
t.Errorf("Test %s: Expected body %s, got %s", testcase.description, testcase.input, string(resultBytes)) |
| 132 |
} |
| 133 |
if err != testcase.expectedErr { |
| 134 |
t.Errorf("Test %s: Expected error %v, got %v", testcase.description, testcase.expectedErr, err) |
| 135 |
} |
| 136 |
} |
| 137 |
} |