From 3c721ea3298a0fb26b8cdfb0e00de9a0a06a3ab2 Mon Sep 17 00:00:00 2001 From: yamasaki Date: Tue, 20 Jun 2023 17:43:53 +0900 Subject: [PATCH] feat : testcode of monitors.go --- monitors_test.go | 132 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/monitors_test.go b/monitors_test.go index 678d1b6..51a10dd 100644 --- a/monitors_test.go +++ b/monitors_test.go @@ -237,6 +237,138 @@ func TestGetMonitor(t *testing.T) { t.Error("request sends json including memo but: ", m) } } +func TestCreateMonitor(t *testing.T) { + var ( + monitorID = "2cSZzK3XfmG" + ) + + ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + if req.URL.Path != "/api/v0/monitors" { + t.Errorf("request URL should be /api/v0/monitors but got: %s", req.URL.Path) + } + if req.Method != http.MethodPost { + t.Errorf("request method should be POST but got: %s", req.Method) + } + + respJSON, _ := json.Marshal(map[string]interface{}{ + "id": monitorID, + "type": "connectivity", + "memo": "connectivity monitor", + "scopes": []string{}, + "excludeScopes": []string{}, + }) + + res.Header()["Content-Type"] = []string{"application/json"} + fmt.Fprint(res, string(respJSON)) + })) + defer ts.Close() + + client, _ := NewClientWithOptions("dummy-key", ts.URL, false) + monitor, err := client.CreateMonitor(&MonitorConnectivity{ + Type: "connectivity", + Memo: "connectivity monitor", + }) + if err != nil { + t.Error("err should be nil but: ", err) + } + + m, ok := monitor.(*MonitorConnectivity) + if !ok || m.Type != "connectivity" { + t.Error("request sends json including type but: ", m) + } + if m.Memo != "connectivity monitor" { + t.Error("request sends json including memo but: ", m) + } +} + +func TestUpdateMonitor(t *testing.T) { + var ( + monitorID = "2cSZzK3XfmG" + ) + + ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + u := fmt.Sprintf("/api/v0/monitors/%s", monitorID) + if req.URL.Path != u { + t.Errorf("request URL should be %v but %v:", u, req.URL.Path) + } + + if req.Method != http.MethodPut { + t.Error("request method should be PUT but: ", req.Method) + } + + respJSON, _ := json.Marshal(map[string]interface{}{ + "id": monitorID, + "type": "connectivity", + "memo": "connectivity monitor", + "scopes": []string{}, + "excludeScopes": []string{}, + }) + + res.Header()["Content-Type"] = []string{"application/json"} + fmt.Fprint(res, string(respJSON)) + })) + defer ts.Close() + + client, _ := NewClientWithOptions("dummy-key", ts.URL, false) + monitor, err := client.UpdateMonitor(monitorID, &MonitorConnectivity{ + Type: "connectivity", + Memo: "connectivity monitor", + }) + if err != nil { + t.Error("err should be nil but: ", err) + } + + m, ok := monitor.(*MonitorConnectivity) + if !ok || m.Type != "connectivity" { + t.Error("request sends json including type but: ", m) + } + if m.Memo != "connectivity monitor" { + t.Error("request sends json including memo but: ", m) + } +} + +func TestDeleteMonitor(t *testing.T) { + var ( + monitorID = "2cSZzK3XfmG" + ) + + ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { + u := fmt.Sprintf("/api/v0/monitors/%s", monitorID) + if req.URL.Path != u { + t.Errorf("request URL should be %v but %v:", u, req.URL.Path) + } + + if req.Method != http.MethodDelete { + t.Error("request method should be DELETE but: ", req.Method) + } + + respJSON, _ := json.Marshal(map[string]interface{}{ + "id": monitorID, + "type": "connectivity", + "memo": "connectivity monitor", + "scopes": []string{}, + "excludeScopes": []string{}, + }) + + res.Header()["Content-Type"] = []string{"application/json"} + fmt.Fprint(res, string(respJSON)) + })) + defer ts.Close() + + client, _ := NewClientWithOptions("dummy-key", ts.URL, false) + monitor, err := client.DeleteMonitor(monitorID) + if err != nil { + t.Error("err should be nil but: ", err) + } + + m, ok := monitor.(*MonitorConnectivity) + if !ok || m.Type != "connectivity" { + t.Error("request sends json including type but: ", m) + } + if m.Memo != "connectivity monitor" { + t.Error("request sends json including memo but: ", m) + } +} // ensure that it supports `"headers":[]` and headers must be nil by default. func TestMonitorExternalHTTP_headers(t *testing.T) {