diff --git a/selvpcclient/resell/v2/floatingips/testing/request_test.go b/selvpcclient/resell/v2/floatingips/testing/request_test.go index d678175..f9ae485 100644 --- a/selvpcclient/resell/v2/floatingips/testing/request_test.go +++ b/selvpcclient/resell/v2/floatingips/testing/request_test.go @@ -2,9 +2,6 @@ package testing import ( "context" - "encoding/json" - "fmt" - "io/ioutil" "net/http" "reflect" "testing" @@ -14,17 +11,13 @@ import ( ) func TestGetFloatingIP(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestGetFloatingIPResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd", + TestGetFloatingIPResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := floatingips.Get(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd") @@ -34,23 +27,22 @@ func TestGetFloatingIP(t *testing.T) { expected := TestGetFloatingIPResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actual, expected) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } func TestListFloatingIPs(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/floatingips", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestListFloatingIPsResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips", + TestListFloatingIPsResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := floatingips.List(ctx, testEnv.Client) @@ -58,6 +50,9 @@ func TestListFloatingIPs(t *testing.T) { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if actual == nil { t.Fatal("didn't get floating ips") } @@ -71,17 +66,13 @@ func TestListFloatingIPs(t *testing.T) { } func TestListFloatingIPsSingle(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/floatingips", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestListFloatingIPsSingleResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips", + TestListFloatingIPsSingleResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := floatingips.List(ctx, testEnv.Client) @@ -91,44 +82,23 @@ func TestListFloatingIPsSingle(t *testing.T) { expected := TestListFloatingIPsSingleResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actual, expected) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } func TestCreateFloatingIPs(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestCreateFloatingIPResponseRaw) - - if r.Method != http.MethodPost { - t.Fatalf("expected %s method but got %s", http.MethodPost, r.Method) - } - - b, err := ioutil.ReadAll(r.Body) - if err != nil { - t.Errorf("unable to read the request body: %v", err) - } - - var actualRequest interface{} - err = json.Unmarshal(b, &actualRequest) - if err != nil { - t.Errorf("unable to unmarshal the request body: %v", err) - } - - var expectedRequest interface{} - err = json.Unmarshal([]byte(TestCreateFloatingIPOptsRaw), &expectedRequest) - if err != nil { - t.Errorf("unable to unmarshal expected raw response: %v", err) - } - - if !reflect.DeepEqual(actualRequest, expectedRequest) { - t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) - } - }) + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/floatingips/projects/49338ac045f448e294b25d013f890317", + TestCreateFloatingIPResponseRaw, TestCreateFloatingIPOptsRaw, http.MethodPost, http.StatusOK, + &endpointCalled, t) ctx := context.Background() createOpts := TestCreateFloatingIPOpts @@ -139,26 +109,29 @@ func TestCreateFloatingIPs(t *testing.T) { expectedResponse := TestCreateFloatingIPResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actualResponse, expectedResponse) { t.Fatalf("expected %#v, but got %#v", actualResponse, expectedResponse) } } func TestDeleteFloatingIP(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - - if r.Method != http.MethodDelete { - t.Fatalf("expected %s method but got %s", http.MethodDelete, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/floatingips/5232d5f3-4950-454b-bd41-78c5295622cd", + "", http.MethodDelete, http.StatusOK, &endpointCalled, t) ctx := context.Background() _, err := floatingips.Delete(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd") if err != nil { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } } diff --git a/selvpcclient/resell/v2/licenses/testing/requests_test.go b/selvpcclient/resell/v2/licenses/testing/requests_test.go index 37dcc6c..dfbdf23 100644 --- a/selvpcclient/resell/v2/licenses/testing/requests_test.go +++ b/selvpcclient/resell/v2/licenses/testing/requests_test.go @@ -2,9 +2,6 @@ package testing import ( "context" - "encoding/json" - "fmt" - "io/ioutil" "net/http" "reflect" "testing" @@ -14,17 +11,13 @@ import ( ) func TestGetLicense(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/licenses/123123", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestGetLicenseResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/licenses/123123", + TestGetLicenseResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := licenses.Get(ctx, testEnv.Client, "123123") @@ -34,23 +27,22 @@ func TestGetLicense(t *testing.T) { expected := TestGetLicenseResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actual, expected) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } func TestListLicenses(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/licenses", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestListLicensesResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/licenses", + TestListLicensesResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := licenses.List(ctx, testEnv.Client) @@ -58,6 +50,9 @@ func TestListLicenses(t *testing.T) { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if actual == nil { t.Fatal("didn't get licenses") } @@ -71,17 +66,13 @@ func TestListLicenses(t *testing.T) { } func TestListLicensesSingle(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/licenses", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestListLicensesSingleResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/licenses", + TestListLicensesSingleResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := licenses.List(ctx, testEnv.Client) @@ -91,44 +82,23 @@ func TestListLicensesSingle(t *testing.T) { expected := TestListLicensesSingleResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actual, expected) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } func TestCreateLicenses(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/licenses/projects/49338ac045f448e294b25d013f890317", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestCreateLicenseResponseRaw) - - if r.Method != http.MethodPost { - t.Fatalf("expected %s method but got %s", http.MethodPost, r.Method) - } - - b, err := ioutil.ReadAll(r.Body) - if err != nil { - t.Errorf("unable to read the request body: %v", err) - } - - var actualRequest interface{} - err = json.Unmarshal(b, &actualRequest) - if err != nil { - t.Errorf("unable to unmarshal the request body: %v", err) - } - - var expectedRequest interface{} - err = json.Unmarshal([]byte(TestCreateLicenseOptsRaw), &expectedRequest) - if err != nil { - t.Errorf("unable to unmarshal expected raw response: %v", err) - } - - if !reflect.DeepEqual(actualRequest, expectedRequest) { - t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) - } - }) + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/licenses/projects/49338ac045f448e294b25d013f890317", + TestCreateLicenseResponseRaw, TestCreateLicenseOptsRaw, http.MethodPost, http.StatusOK, + &endpointCalled, t) ctx := context.Background() createOpts := TestCreateLicenseOpts @@ -139,26 +109,29 @@ func TestCreateLicenses(t *testing.T) { expectedResponse := TestCreateLicenseResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actualResponse, expectedResponse) { t.Fatalf("expected %#v, but got %#v", actualResponse, expectedResponse) } } func TestDeleteLicense(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/licenses/5232d5f3-4950-454b-bd41-78c5295622cd", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - - if r.Method != http.MethodDelete { - t.Fatalf("expected %s method but got %s", http.MethodDelete, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/licenses/5232d5f3-4950-454b-bd41-78c5295622cd", + "", http.MethodDelete, http.StatusOK, &endpointCalled, t) ctx := context.Background() _, err := licenses.Delete(ctx, testEnv.Client, "5232d5f3-4950-454b-bd41-78c5295622cd") if err != nil { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } } diff --git a/selvpcclient/resell/v2/projects/testing/requests_test.go b/selvpcclient/resell/v2/projects/testing/requests_test.go index 75fcbc4..dea4329 100644 --- a/selvpcclient/resell/v2/projects/testing/requests_test.go +++ b/selvpcclient/resell/v2/projects/testing/requests_test.go @@ -2,9 +2,6 @@ package testing import ( "context" - "encoding/json" - "fmt" - "io/ioutil" "net/http" "reflect" "testing" @@ -14,17 +11,13 @@ import ( ) func TestGetProject(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/projects/49338ac045f448e294b25d013f890317", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestGetProjectResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/projects/49338ac045f448e294b25d013f890317", + TestGetProjectResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := projects.Get(ctx, testEnv.Client, "49338ac045f448e294b25d013f890317") @@ -32,6 +25,9 @@ func TestGetProject(t *testing.T) { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if actual == nil { t.Fatal("didn't get project") } @@ -41,17 +37,13 @@ func TestGetProject(t *testing.T) { } func TestGetProjectSingleQuota(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/projects/49338ac045f448e294b25d013f890317", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestGetProjectResponseSingleQuotaRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/projects/49338ac045f448e294b25d013f890317", + TestGetProjectResponseSingleQuotaRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := projects.Get(ctx, testEnv.Client, "49338ac045f448e294b25d013f890317") @@ -61,23 +53,22 @@ func TestGetProjectSingleQuota(t *testing.T) { expected := TestGetProjectSingleQuotaResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actual, expected) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } func TestListProjects(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/projects", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestListProjectsResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/projects", + TestListProjectsResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := projects.List(ctx, testEnv.Client) @@ -85,6 +76,9 @@ func TestListProjects(t *testing.T) { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if actual == nil { t.Fatal("didn't get projects") } @@ -94,17 +88,13 @@ func TestListProjects(t *testing.T) { } func TestListProjectsSingle(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/projects", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestListProjectsResponseSingleRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/projects", + TestListProjectsResponseSingleRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := projects.List(ctx, testEnv.Client) @@ -114,44 +104,23 @@ func TestListProjectsSingle(t *testing.T) { expected := TestListProjectsSingleResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actual, expected) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } func TestCreateProject(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/projects", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestCreateProjectResponseRaw) - - if r.Method != http.MethodPost { - t.Fatalf("expected %s method but got %s", http.MethodPost, r.Method) - } - - b, err := ioutil.ReadAll(r.Body) - if err != nil { - t.Errorf("unable to read the request body: %v", err) - } - - var actualRequest interface{} - err = json.Unmarshal(b, &actualRequest) - if err != nil { - t.Errorf("unable to unmarshal the request body: %v", err) - } - - var expectedRequest interface{} - err = json.Unmarshal([]byte(TestCreateProjectOptsRaw), &expectedRequest) - if err != nil { - t.Errorf("unable to unmarshal expected raw response: %v", err) - } - - if !reflect.DeepEqual(actualRequest, expectedRequest) { - t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) - } - }) + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/projects", + TestCreateProjectResponseRaw, TestCreateProjectOptsRaw, http.MethodPost, http.StatusOK, + &endpointCalled, t) ctx := context.Background() createOpts := TestCreateProjectOpts @@ -162,44 +131,23 @@ func TestCreateProject(t *testing.T) { expectedResponse := TestCreateProjectResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actualResponse, expectedResponse) { t.Fatalf("expected %#v, but got %#v", actualResponse, expectedResponse) } } func TestUpdateProject(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/projects/f9ede488e5f14bac8962d8c53d0af9f4", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestUpdateProjectResponseRaw) - - if r.Method != http.MethodPatch { - t.Fatalf("expected %s method but got %s", http.MethodPatch, r.Method) - } - - b, err := ioutil.ReadAll(r.Body) - if err != nil { - t.Errorf("unable to read the request body: %v", err) - } - - var actualRequest interface{} - err = json.Unmarshal(b, &actualRequest) - if err != nil { - t.Errorf("unable to unmarshal the request body: %v", err) - } - - var expectedRequest interface{} - err = json.Unmarshal([]byte(TestUpdateProjectOptsRaw), &expectedRequest) - if err != nil { - t.Errorf("unable to unmarshal expected raw response: %v", err) - } - - if !reflect.DeepEqual(actualRequest, expectedRequest) { - t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) - } - }) + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/projects/f9ede488e5f14bac8962d8c53d0af9f4", + TestUpdateProjectResponseRaw, TestUpdateProjectOptsRaw, http.MethodPatch, http.StatusOK, + &endpointCalled, t) ctx := context.Background() updateOpts := TestUpdateProjectOpts @@ -210,26 +158,29 @@ func TestUpdateProject(t *testing.T) { expectedResponse := TestUpdateProjectResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actualResponse, expectedResponse) { t.Fatalf("expected %#v, but got %#v", actualResponse, expectedResponse) } } func TestDeleteProject(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/projects/f9ede488e5f14bac8962d8c53d0af9f4", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - - if r.Method != http.MethodDelete { - t.Fatalf("expected %s method but got %s", http.MethodDelete, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/projects/f9ede488e5f14bac8962d8c53d0af9f4", + "", http.MethodDelete, http.StatusOK, &endpointCalled, t) ctx := context.Background() _, err := projects.Delete(ctx, testEnv.Client, "f9ede488e5f14bac8962d8c53d0af9f4") if err != nil { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } } diff --git a/selvpcclient/resell/v2/quotas/testing/requests_test.go b/selvpcclient/resell/v2/quotas/testing/requests_test.go index 20eaa72..bffa143 100644 --- a/selvpcclient/resell/v2/quotas/testing/requests_test.go +++ b/selvpcclient/resell/v2/quotas/testing/requests_test.go @@ -2,9 +2,6 @@ package testing import ( "context" - "encoding/json" - "fmt" - "io/ioutil" "net/http" "reflect" "testing" @@ -14,17 +11,13 @@ import ( ) func TestGetAllQuotas(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/quotas", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestGetAllQuotasResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/quotas", + TestGetAllQuotasResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := quotas.GetAll(ctx, testEnv.Client) @@ -32,6 +25,9 @@ func TestGetAllQuotas(t *testing.T) { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if actual == nil { t.Fatal("didn't get quotas") } @@ -50,17 +46,13 @@ func TestGetAllQuotas(t *testing.T) { } func TestGetAllQuotasSingle(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/quotas", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestGetAllQuotasResponseSingleRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/quotas", + TestGetAllQuotasResponseSingleRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := quotas.GetAll(ctx, testEnv.Client) @@ -70,23 +62,22 @@ func TestGetAllQuotasSingle(t *testing.T) { expected := TestGetAllQuotasResponseSingle + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actual, expected) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } func TestGetFreeQuotas(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/quotas/free", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestGetFreeQuotasResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/quotas/free", + TestGetFreeQuotasResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := quotas.GetFree(ctx, testEnv.Client) @@ -94,6 +85,9 @@ func TestGetFreeQuotas(t *testing.T) { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if actual == nil { t.Fatal("didn't get quotas") } @@ -112,17 +106,13 @@ func TestGetFreeQuotas(t *testing.T) { } func TestGetFreeQuotasSingle(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/quotas/free", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestGetFreeQuotasResponseSingleRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/quotas/free", + TestGetFreeQuotasResponseSingleRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := quotas.GetFree(ctx, testEnv.Client) @@ -132,23 +122,22 @@ func TestGetFreeQuotasSingle(t *testing.T) { expected := TestGetFreeQuotasResponseSingle + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actual, expected) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } func TestGetProjectsQuotas(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/quotas/projects", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestGetProjectsQuotasResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/quotas/projects", + TestGetProjectsQuotasResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := quotas.GetProjectsQuotas(ctx, testEnv.Client) @@ -156,6 +145,9 @@ func TestGetProjectsQuotas(t *testing.T) { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if actual == nil { t.Fatal("didn't get quotas") } @@ -174,17 +166,13 @@ func TestGetProjectsQuotas(t *testing.T) { } func TestGetProjectsQuotasSingle(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/quotas/projects", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestGetProjectsQuotasResponseSingleRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/quotas/projects", + TestGetProjectsQuotasResponseSingleRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := quotas.GetProjectsQuotas(ctx, testEnv.Client) @@ -194,23 +182,22 @@ func TestGetProjectsQuotasSingle(t *testing.T) { expected := TestGetProjectsQuotasResponseSingle + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actual, expected) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } func TestGetProjectQuotas(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/quotas/projects/c83243b3c18a4d109a5f0fe45336af85", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestGetProjectQuotasResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/quotas/projects/c83243b3c18a4d109a5f0fe45336af85", + TestGetProjectQuotasResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := quotas.GetProjectQuotas(ctx, testEnv.Client, "c83243b3c18a4d109a5f0fe45336af85") @@ -218,6 +205,9 @@ func TestGetProjectQuotas(t *testing.T) { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if actual == nil { t.Fatal("didn't get quotas") } @@ -236,17 +226,13 @@ func TestGetProjectQuotas(t *testing.T) { } func TestGetProjectQuotasSingle(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/quotas/projects/c83243b3c18a4d109a5f0fe45336af85", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestGetProjectQuotasResponseSingleRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/quotas/projects/c83243b3c18a4d109a5f0fe45336af85", + TestGetProjectQuotasResponseSingleRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := quotas.GetProjectQuotas(ctx, testEnv.Client, "c83243b3c18a4d109a5f0fe45336af85") @@ -256,44 +242,23 @@ func TestGetProjectQuotasSingle(t *testing.T) { expected := TestGetProjectQuotasResponseSingle + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actual, expected) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } func TestUpdateProjectQuotas(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/quotas/projects/c83243b3c18a4d109a5f0fe45336af85", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestUpdateProjectQuotasResponseRaw) - - if r.Method != http.MethodPatch { - t.Fatalf("expected %s method but got %s", http.MethodPatch, r.Method) - } - - b, err := ioutil.ReadAll(r.Body) - if err != nil { - t.Errorf("unable to read the request body: %v", err) - } - - var actualRequest interface{} - err = json.Unmarshal(b, &actualRequest) - if err != nil { - t.Errorf("unable to unmarshal the request body: %v", err) - } - - var expectedRequest interface{} - err = json.Unmarshal([]byte(TestUpdateQuotasOptsRaw), &expectedRequest) - if err != nil { - t.Errorf("unable to unmarshal expected raw response: %v", err) - } - - if !reflect.DeepEqual(actualRequest, expectedRequest) { - t.Fatalf("expected %#v update options, but got %#v", expectedRequest, actualRequest) - } - }) + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/quotas/projects/c83243b3c18a4d109a5f0fe45336af85", + TestUpdateProjectQuotasResponseRaw, TestUpdateQuotasOptsRaw, http.MethodPatch, http.StatusOK, + &endpointCalled, t) ctx := context.Background() updateOpts := TestUpdateQuotasOpts @@ -304,6 +269,9 @@ func TestUpdateProjectQuotas(t *testing.T) { expectedResponse := TestUpdateProjectQuotasResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actualResponse, expectedResponse) { t.Fatalf("expected %#v, but got %#v", actualResponse, expectedResponse) } diff --git a/selvpcclient/resell/v2/subnets/testing/request_test.go b/selvpcclient/resell/v2/subnets/testing/request_test.go index 22f3ffd..eb49d8d 100644 --- a/selvpcclient/resell/v2/subnets/testing/request_test.go +++ b/selvpcclient/resell/v2/subnets/testing/request_test.go @@ -2,9 +2,6 @@ package testing import ( "context" - "encoding/json" - "fmt" - "io/ioutil" "net/http" "reflect" "testing" @@ -14,17 +11,13 @@ import ( ) func TestGetSubnet(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/subnets/111122", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestGetSubnetResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/subnets/111122", + TestGetSubnetResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := subnets.Get(ctx, testEnv.Client, "111122") @@ -34,23 +27,22 @@ func TestGetSubnet(t *testing.T) { expected := TestGetSubnetResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actual, expected) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } func TestListSubnets(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/subnets", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestListSubnetsResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/subnets", + TestListSubnetsResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := subnets.List(ctx, testEnv.Client) @@ -58,6 +50,9 @@ func TestListSubnets(t *testing.T) { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if actual == nil { t.Fatal("didn't get subnets") } @@ -71,17 +66,13 @@ func TestListSubnets(t *testing.T) { } func TestListSubnetsSingle(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/subnets", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestListSubnetsSingleResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/subnets", + TestListSubnetsSingleResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := subnets.List(ctx, testEnv.Client) @@ -91,44 +82,23 @@ func TestListSubnetsSingle(t *testing.T) { expected := TestListSubnetsSingleResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actual, expected) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } func TestCreateSubnets(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/subnets/projects/9c97bdc75295493096cf5edcb8c37933", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - fmt.Fprintf(w, TestCreateSubnetsResponseRaw) - - if r.Method != http.MethodPost { - t.Fatalf("expected %s method but got %s", http.MethodPost, r.Method) - } - - b, err := ioutil.ReadAll(r.Body) - if err != nil { - t.Errorf("unable to read the request body: %v", err) - } - - var actualRequest interface{} - err = json.Unmarshal(b, &actualRequest) - if err != nil { - t.Errorf("unable to unmarshal the request body: %v", err) - } - - var expectedRequest interface{} - err = json.Unmarshal([]byte(TestCreateSubnetsOptsRaw), &expectedRequest) - if err != nil { - t.Errorf("unable to unmarshal expected raw response: %v", err) - } - - if !reflect.DeepEqual(actualRequest, expectedRequest) { - t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) - } - }) + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/subnets/projects/9c97bdc75295493096cf5edcb8c37933", + TestCreateSubnetsResponseRaw, TestCreateSubnetsOptsRaw, http.MethodPost, + http.StatusOK, &endpointCalled, t) ctx := context.Background() createOpts := TestCreateSubnetsOpts @@ -139,26 +109,30 @@ func TestCreateSubnets(t *testing.T) { expectedResponse := TestCreateSubnetResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actualResponse, expectedResponse) { t.Fatalf("expected %#v, but got %#v", actualResponse, expectedResponse) } } func TestDeleteSubnet(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/subnets/112233", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - - if r.Method != http.MethodDelete { - t.Fatalf("expected %s method but got %s", http.MethodDelete, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/subnets/112233", "", + http.MethodDelete, http.StatusOK, &endpointCalled, t) ctx := context.Background() _, err := subnets.Delete(ctx, testEnv.Client, "112233") if err != nil { t.Fatal(err) } + + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } } diff --git a/selvpcclient/resell/v2/users/testing/requests_test.go b/selvpcclient/resell/v2/users/testing/requests_test.go index d179fff..d0de8c4 100644 --- a/selvpcclient/resell/v2/users/testing/requests_test.go +++ b/selvpcclient/resell/v2/users/testing/requests_test.go @@ -2,9 +2,6 @@ package testing import ( "context" - "encoding/json" - "fmt" - "io/ioutil" "net/http" "reflect" "testing" @@ -14,18 +11,13 @@ import ( ) func TestListUsers(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - fmt.Fprintf(w, TestListUsersResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/users", + TestListUsersResponseRaw, http.MethodGet, http.StatusOK, &endpointCalled, t) ctx := context.Background() actual, _, err := users.List(ctx, testEnv.Client) @@ -33,6 +25,9 @@ func TestListUsers(t *testing.T) { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if actual == nil { t.Fatal("didn't get users") } @@ -46,18 +41,14 @@ func TestListUsers(t *testing.T) { } func TestListUsersSingle(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - fmt.Fprintf(w, TestListUsersSingleUserResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/users", + TestListUsersSingleUserResponseRaw, http.MethodGet, http.StatusOK, + &endpointCalled, t) ctx := context.Background() actual, _, err := users.List(ctx, testEnv.Client) @@ -67,27 +58,30 @@ func TestListUsersSingle(t *testing.T) { expected := TestListUsersSingleUserResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actual, expected) { t.Fatalf("expected %#v, but got %#v", expected, actual) } } func TestListUsersHTTPError(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(http.StatusBadGateway) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/users", + TestListUsersSingleUserResponseRaw, http.MethodGet, http.StatusBadGateway, + &endpointCalled, t) ctx := context.Background() allUsers, httpResponse, err := users.List(ctx, testEnv.Client) + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if allUsers != nil { t.Fatal("expected no users from the List method") } @@ -95,7 +89,8 @@ func TestListUsersHTTPError(t *testing.T) { t.Fatal("expected error from the List method") } if httpResponse.StatusCode != http.StatusBadGateway { - t.Fatalf("expected %d status in the HTTP response, but got %d", http.StatusBadGateway, httpResponse.StatusCode) + t.Fatalf("expected %d status in the HTTP response, but got %d", + http.StatusBadGateway, httpResponse.StatusCode) } } @@ -117,22 +112,21 @@ func TestListUsersTimeoutError(t *testing.T) { } func TestListUsersUnmarshallError(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - fmt.Fprintf(w, TestManyUsersInvalidResponseRaw) - - if r.Method != http.MethodGet { - t.Fatalf("expected %s method but got %s", http.MethodGet, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/users", + TestManyUsersInvalidResponseRaw, http.MethodGet, http.StatusOK, + &endpointCalled, t) ctx := context.Background() allUsers, _, err := users.List(ctx, testEnv.Client) + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if allUsers != nil { t.Fatal("expected no users from the List method") } @@ -142,39 +136,14 @@ func TestListUsersUnmarshallError(t *testing.T) { } func TestCreateUser(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - fmt.Fprintf(w, TestCreateUserResponseRaw) - - if r.Method != http.MethodPost { - t.Fatalf("expected %s method but got %s", http.MethodPost, r.Method) - } - - b, err := ioutil.ReadAll(r.Body) - if err != nil { - t.Errorf("unable to read the request body: %v", err) - } - - var actualRequest interface{} - err = json.Unmarshal(b, &actualRequest) - if err != nil { - t.Errorf("unable to unmarshal the request body: %v", err) - } - - var expectedRequest interface{} - err = json.Unmarshal([]byte(TestCreateUserOptsRaw), &expectedRequest) - if err != nil { - t.Errorf("unable to unmarshal expected raw response: %v", err) - } - - if !reflect.DeepEqual(actualRequest, expectedRequest) { - t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) - } - }) + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/users", + TestCreateUserResponseRaw, TestCreateUserOptsRaw, http.MethodPost, + http.StatusOK, &endpointCalled, t) ctx := context.Background() createOpts := TestCreateUserOpts @@ -185,49 +154,30 @@ func TestCreateUser(t *testing.T) { expectedResponse := TestCreateUserResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actualResponse, expectedResponse) { t.Fatalf("expected %#v, but got %#v", actualResponse, expectedResponse) } } func TestCreateUserHTTPError(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(http.StatusBadRequest) - - if r.Method != http.MethodPost { - t.Fatalf("expected %s method but got %s", http.MethodPost, r.Method) - } - - b, err := ioutil.ReadAll(r.Body) - if err != nil { - t.Errorf("unable to read the request body: %v", err) - } - - var actualRequest interface{} - err = json.Unmarshal(b, &actualRequest) - if err != nil { - t.Errorf("unable to unmarshal the request body: %v", err) - } - - var expectedRequest interface{} - err = json.Unmarshal([]byte(TestCreateUserOptsRaw), &expectedRequest) - if err != nil { - t.Errorf("unable to unmarshal expected raw response: %v", err) - } - - if !reflect.DeepEqual(actualRequest, expectedRequest) { - t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) - } - }) + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/users", "", + TestCreateUserOptsRaw, http.MethodPost, http.StatusBadRequest, &endpointCalled, t) ctx := context.Background() createOpts := TestCreateUserOpts user, httpResponse, err := users.Create(ctx, testEnv.Client, createOpts) + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if user != nil { t.Fatal("expected no user from the Create method") } @@ -258,44 +208,21 @@ func TestCreateUserTimeoutError(t *testing.T) { } func TestCreateUserUnmarshallError(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/users", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - fmt.Fprintf(w, TestSingleUserInvalidResponseRaw) - - if r.Method != http.MethodPost { - t.Fatalf("expected %s method but got %s", http.MethodPost, r.Method) - } - - b, err := ioutil.ReadAll(r.Body) - if err != nil { - t.Errorf("unable to read the request body: %v", err) - } - - var actualRequest interface{} - err = json.Unmarshal(b, &actualRequest) - if err != nil { - t.Errorf("unable to unmarshal the request body: %v", err) - } - - var expectedRequest interface{} - err = json.Unmarshal([]byte(TestCreateUserOptsRaw), &expectedRequest) - if err != nil { - t.Errorf("unable to unmarshal expected raw response: %v", err) - } - - if !reflect.DeepEqual(actualRequest, expectedRequest) { - t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) - } - }) + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/users", TestSingleUserInvalidResponseRaw, + TestCreateUserOptsRaw, http.MethodPost, http.StatusOK, &endpointCalled, t) ctx := context.Background() createOpts := TestCreateUserOpts user, _, err := users.Create(ctx, testEnv.Client, createOpts) + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if user != nil { t.Fatal("expected no user from the Create method") } @@ -305,39 +232,14 @@ func TestCreateUserUnmarshallError(t *testing.T) { } func TestUpdateUser(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - fmt.Fprintf(w, TestUpdateUserResponseRaw) - - if r.Method != http.MethodPatch { - t.Fatalf("expected %s method but got %s", http.MethodPatch, r.Method) - } - - b, err := ioutil.ReadAll(r.Body) - if err != nil { - t.Errorf("unable to read the request body: %v", err) - } - - var actualRequest interface{} - err = json.Unmarshal(b, &actualRequest) - if err != nil { - t.Errorf("unable to unmarshal the request body: %v", err) - } - - var expectedRequest interface{} - err = json.Unmarshal([]byte(TestUpdateUserOptsRaw), &expectedRequest) - if err != nil { - t.Errorf("unable to unmarshal expected raw response: %v", err) - } - - if !reflect.DeepEqual(actualRequest, expectedRequest) { - t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) - } - }) + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", + TestUpdateUserResponseRaw, TestUpdateUserOptsRaw, http.MethodPatch, http.StatusOK, + &endpointCalled, t) ctx := context.Background() updateOpts := TestUpdateUserOpts @@ -348,49 +250,30 @@ func TestUpdateUser(t *testing.T) { expectedResponse := TestUpdateUserResponse + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if !reflect.DeepEqual(actualResponse, expectedResponse) { t.Fatalf("expected %#v, but got %#v", actualResponse, expectedResponse) } } func TestUpdateUserHTTPError(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(http.StatusBadRequest) - - if r.Method != http.MethodPatch { - t.Fatalf("expected %s method but got %s", http.MethodPatch, r.Method) - } - - b, err := ioutil.ReadAll(r.Body) - if err != nil { - t.Errorf("unable to read the request body: %v", err) - } - - var actualRequest interface{} - err = json.Unmarshal(b, &actualRequest) - if err != nil { - t.Errorf("unable to unmarshal the request body: %v", err) - } - - var expectedRequest interface{} - err = json.Unmarshal([]byte(TestUpdateUserOptsRaw), &expectedRequest) - if err != nil { - t.Errorf("unable to unmarshal expected raw response: %v", err) - } - - if !reflect.DeepEqual(actualRequest, expectedRequest) { - t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) - } - }) + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", + "", TestUpdateUserOptsRaw, http.MethodPatch, http.StatusBadRequest, &endpointCalled, t) ctx := context.Background() updateOpts := TestUpdateUserOpts user, httpResponse, err := users.Update(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f", updateOpts) + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if user != nil { t.Fatal("expected no user from the Update method") } @@ -421,44 +304,22 @@ func TestUpdateUserTimeoutError(t *testing.T) { } func TestUpdateUserUnmarshallError(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - fmt.Fprintf(w, TestSingleUserInvalidResponseRaw) - - if r.Method != http.MethodPatch { - t.Fatalf("expected %s method but got %s", http.MethodPatch, r.Method) - } - - b, err := ioutil.ReadAll(r.Body) - if err != nil { - t.Errorf("unable to read the request body: %v", err) - } - - var actualRequest interface{} - err = json.Unmarshal(b, &actualRequest) - if err != nil { - t.Errorf("unable to unmarshal the request body: %v", err) - } - - var expectedRequest interface{} - err = json.Unmarshal([]byte(TestUpdateUserOptsRaw), &expectedRequest) - if err != nil { - t.Errorf("unable to unmarshal expected raw response: %v", err) - } - - if !reflect.DeepEqual(actualRequest, expectedRequest) { - t.Fatalf("expected %#v create options, but got %#v", expectedRequest, actualRequest) - } - }) + testutils.HandleReqWithBody(testEnv.Mux, "/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", + TestSingleUserInvalidResponseRaw, TestUpdateUserOptsRaw, http.MethodPatch, + http.StatusOK, &endpointCalled, t) ctx := context.Background() updateOpts := TestUpdateUserOpts user, _, err := users.Update(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f", updateOpts) + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if user != nil { t.Fatal("expected no user from the Update method") } @@ -468,40 +329,39 @@ func TestUpdateUserUnmarshallError(t *testing.T) { } func TestDeleteUser(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - - if r.Method != http.MethodDelete { - t.Fatalf("expected %s method but got %s", http.MethodDelete, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", + "", http.MethodDelete, http.StatusOK, &endpointCalled, t) ctx := context.Background() _, err := users.Delete(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f") if err != nil { t.Fatal(err) } + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } } func TestDeleteUserHTTPError(t *testing.T) { + endpointCalled := false + testEnv := testutils.SetupTestEnv() defer testEnv.TearDownTestEnv() testEnv.NewTestResellV2Client() - testEnv.Mux.HandleFunc("/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Content-Type", "application/json") - w.WriteHeader(http.StatusBadGateway) - - if r.Method != http.MethodDelete { - t.Fatalf("expected %s method but got %s", http.MethodDelete, r.Method) - } - }) + testutils.HandleReqWithoutBody(testEnv.Mux, "/resell/v2/users/4b2e452ed4c940bd87a88499eaf14c4f", + "", http.MethodDelete, http.StatusBadGateway, &endpointCalled, t) ctx := context.Background() httpResponse, err := users.Delete(ctx, testEnv.Client, "4b2e452ed4c940bd87a88499eaf14c4f") + if !endpointCalled { + t.Fatal("endpoint wasn't called") + } if err == nil { t.Fatal("expected error from the Delete method") } diff --git a/selvpcclient/testutils/handlers.go b/selvpcclient/testutils/handlers.go new file mode 100644 index 0000000..2f6c7aa --- /dev/null +++ b/selvpcclient/testutils/handlers.go @@ -0,0 +1,61 @@ +package testutils + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "reflect" + "testing" +) + +// HandleReqWithoutBody provides the HTTP endpoint to test requests without body. +func HandleReqWithoutBody(mux *http.ServeMux, url, rawResponse, method string, httpStatus int, callFlag *bool, t *testing.T) { + mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(httpStatus) + fmt.Fprintf(w, rawResponse) + + if r.Method != method { + t.Fatalf("expected %s method but got %s", method, r.Method) + } + + *callFlag = true + }) +} + +// HandleReqWithBody provides the HTTP endpoint to test requests with body. +func HandleReqWithBody(mux *http.ServeMux, url, rawResponse, rawRequest, method string, httpStatus int, callFlag *bool, t *testing.T) { + mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(httpStatus) + fmt.Fprintf(w, rawResponse) + + if r.Method != method { + t.Fatalf("expected %s method but got %s", method, r.Method) + } + + b, err := ioutil.ReadAll(r.Body) + if err != nil { + t.Errorf("unable to read the request body: %v", err) + } + + var actualRequest interface{} + err = json.Unmarshal(b, &actualRequest) + if err != nil { + t.Errorf("unable to unmarshal the request body: %v", err) + } + + var expectedRequest interface{} + err = json.Unmarshal([]byte(rawRequest), &expectedRequest) + if err != nil { + t.Errorf("unable to unmarshal expected raw request: %v", err) + } + + if !reflect.DeepEqual(expectedRequest, actualRequest) { + t.Fatalf("expected %#v request, but got %#v", expectedRequest, actualRequest) + } + + *callFlag = true + }) +}