From 17229665f959df4ed3028e5a07ce17a88bd1bb82 Mon Sep 17 00:00:00 2001 From: Mikhail Krivoshein Date: Thu, 30 Apr 2020 15:44:46 +0100 Subject: [PATCH] tests for creating HTTPS, IMAP and custom port monitors Fixes bitfield/uptimerobot#21 --- pkg/testdata/requestNewCustomPortMonitor.json | 10 + ...onitor.json => requestNewHttpMonitor.json} | 1 - pkg/testdata/requestNewHttpsMonitor.json | 8 + pkg/testdata/requestNewImapPortMonitor.json | 9 + pkg/uptimerobot_test.go | 172 ++++++++++++------ 5 files changed, 140 insertions(+), 60 deletions(-) create mode 100644 pkg/testdata/requestNewCustomPortMonitor.json rename pkg/testdata/{requestNewMonitor.json => requestNewHttpMonitor.json} (92%) create mode 100644 pkg/testdata/requestNewHttpsMonitor.json create mode 100644 pkg/testdata/requestNewImapPortMonitor.json diff --git a/pkg/testdata/requestNewCustomPortMonitor.json b/pkg/testdata/requestNewCustomPortMonitor.json new file mode 100644 index 0000000..0ae2dc8 --- /dev/null +++ b/pkg/testdata/requestNewCustomPortMonitor.json @@ -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" +} \ No newline at end of file diff --git a/pkg/testdata/requestNewMonitor.json b/pkg/testdata/requestNewHttpMonitor.json similarity index 92% rename from pkg/testdata/requestNewMonitor.json rename to pkg/testdata/requestNewHttpMonitor.json index fc19e8c..dbe6463 100644 --- a/pkg/testdata/requestNewMonitor.json +++ b/pkg/testdata/requestNewHttpMonitor.json @@ -4,6 +4,5 @@ "friendly_name": "My test monitor", "url": "http://example.com", "type": 1, - "port": 80, "alert_contacts": "3_0_0-5_0_0-7_0_0" } \ No newline at end of file diff --git a/pkg/testdata/requestNewHttpsMonitor.json b/pkg/testdata/requestNewHttpsMonitor.json new file mode 100644 index 0000000..55dba1b --- /dev/null +++ b/pkg/testdata/requestNewHttpsMonitor.json @@ -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" +} \ No newline at end of file diff --git a/pkg/testdata/requestNewImapPortMonitor.json b/pkg/testdata/requestNewImapPortMonitor.json new file mode 100644 index 0000000..8b2121f --- /dev/null +++ b/pkg/testdata/requestNewImapPortMonitor.json @@ -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" +} \ No newline at end of file diff --git a/pkg/uptimerobot_test.go b/pkg/uptimerobot_test.go index 28490ae..7c7a21d 100644 --- a/pkg/uptimerobot_test.go +++ b/pkg/uptimerobot_test.go @@ -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() + 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(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)) + } + }) } }