-
-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests for creating HTTPS, IMAP and custom port monitors #22
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"api_key": "dummy", | ||
"format": "json", | ||
"friendly_name": "My custom port monitor", | ||
"url": "example.com", | ||
"type": 4, | ||
"sub_type": 99, | ||
"port": 8443, | ||
"alert_contacts": "3_0_0-5_0_0-7_0_0" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"api_key": "dummy", | ||
"format": "json", | ||
"friendly_name": "My HTTPS test monitor", | ||
"url": "https://example.com", | ||
"type": 1, | ||
"alert_contacts": "3_0_0-5_0_0-7_0_0" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"api_key": "dummy", | ||
"format": "json", | ||
"friendly_name": "My IMAP port monitor", | ||
"url": "example.com", | ||
"type": 4, | ||
"sub_type": 6, | ||
"alert_contacts": "3_0_0-5_0_0-7_0_0" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,66 +75,120 @@ func TestUnmarshalMonitor(t *testing.T) { | |
|
||
func TestCreate(t *testing.T) { | ||
t.Parallel() | ||
client := New("dummy") | ||
// force test coverage of the client's dump functionality | ||
client.Debug = ioutil.Discard | ||
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodPost { | ||
t.Errorf("want POST request, got %q", r.Method) | ||
} | ||
wantURL := "/v2/newMonitor" | ||
if r.URL.EscapedPath() != wantURL { | ||
t.Errorf("want %q, got %q", wantURL, r.URL.EscapedPath()) | ||
} | ||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
r.Body.Close() | ||
want, err := ioutil.ReadFile("testdata/requestNewMonitor.json") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// Convert the received body and the expected body to maps, for | ||
// ease of comparison | ||
wantMap := map[string]interface{}{} | ||
err = json.Unmarshal(want, &wantMap) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
bodyMap := map[string]interface{}{} | ||
err = json.Unmarshal(body, &bodyMap) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !cmp.Equal(wantMap, bodyMap) { | ||
t.Error(cmp.Diff(wantMap, bodyMap)) | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
data, err := os.Open("testdata/newMonitor.json") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer data.Close() | ||
io.Copy(w, data) | ||
})) | ||
defer ts.Close() | ||
client.HTTPClient = ts.Client() | ||
client.URL = ts.URL | ||
create := Monitor{ | ||
FriendlyName: "My test monitor", | ||
URL: "http://example.com", | ||
Type: TypeHTTP, | ||
Port: 80, | ||
AlertContacts: []string{"3", "5", "7"}, | ||
} | ||
got, err := client.CreateMonitor(create) | ||
if err != nil { | ||
t.Error(err) | ||
|
||
tcs := []struct { | ||
name string | ||
input Monitor | ||
requestFile string | ||
responseFile string | ||
}{ | ||
{ | ||
name: "Simple HTTP", | ||
input: Monitor{ | ||
FriendlyName: "My test monitor", | ||
URL: "http://example.com", | ||
Type: TypeHTTP, | ||
AlertContacts: []string{"3", "5", "7"}, | ||
}, | ||
requestFile: "testdata/requestNewHttpMonitor.json", | ||
responseFile: "testdata/newMonitor.json", | ||
}, | ||
{ | ||
name: "Simple HTTPS", | ||
input: Monitor{ | ||
FriendlyName: "My HTTPS test monitor", | ||
URL: "https://example.com", | ||
Type: TypeHTTP, | ||
AlertContacts: []string{"3", "5", "7"}, | ||
}, | ||
requestFile: "testdata/requestNewHttpsMonitor.json", | ||
responseFile: "testdata/newMonitor.json", | ||
}, | ||
{ | ||
name: "IMAP port", | ||
input: Monitor{ | ||
FriendlyName: "My IMAP port monitor", | ||
URL: "example.com", | ||
Type: TypePort, | ||
SubType: SubTypeIMAP, | ||
AlertContacts: []string{"3", "5", "7"}, | ||
}, | ||
requestFile: "testdata/requestNewImapPortMonitor.json", | ||
responseFile: "testdata/newMonitor.json", | ||
}, | ||
{ | ||
name: "Custom port", | ||
input: Monitor{ | ||
FriendlyName: "My custom port monitor", | ||
URL: "example.com", | ||
Type: TypePort, | ||
SubType: SubTypeCustomPort, | ||
Port: 8443, | ||
AlertContacts: []string{"3", "5", "7"}, | ||
}, | ||
requestFile: "testdata/requestNewCustomPortMonitor.json", | ||
responseFile: "testdata/newMonitor.json", | ||
}, | ||
} | ||
var want int64 = 777810874 | ||
if !cmp.Equal(want, got) { | ||
t.Error(cmp.Diff(want, got)) | ||
|
||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Watch out, there's a subtle bug here: https://gist.github.com/posener/92a55c4cd441fc5e5e85f27bca008721 In fact, we just end up running the |
||
client := New("dummy") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably move the client setup outside the |
||
// force test coverage of the client's dump functionality | ||
client.Debug = ioutil.Discard | ||
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodPost { | ||
t.Errorf("want POST request, got %q", r.Method) | ||
} | ||
wantURL := "/v2/newMonitor" | ||
if r.URL.EscapedPath() != wantURL { | ||
t.Errorf("want %q, got %q", wantURL, r.URL.EscapedPath()) | ||
} | ||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
r.Body.Close() | ||
want, err := ioutil.ReadFile(tc.requestFile) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// Convert the received body and the expected body to maps, for | ||
// ease of comparison | ||
wantMap := map[string]interface{}{} | ||
err = json.Unmarshal(want, &wantMap) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
bodyMap := map[string]interface{}{} | ||
err = json.Unmarshal(body, &bodyMap) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !cmp.Equal(wantMap, bodyMap) { | ||
t.Error(cmp.Diff(wantMap, bodyMap)) | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
data, err := os.Open(tc.responseFile) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer data.Close() | ||
io.Copy(w, data) | ||
})) | ||
defer ts.Close() | ||
client.HTTPClient = ts.Client() | ||
client.URL = ts.URL | ||
got, err := client.CreateMonitor(tc.input) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
var want int64 = 777810874 | ||
if !cmp.Equal(want, got) { | ||
t.Error(cmp.Diff(want, got)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a house style tip; I don't use blank lines within functions in this project.