Skip to content

Commit

Permalink
refactor: reuse mockutils package (#462)
Browse files Browse the repository at this point in the history
Removes the now duplicated mock request utils, and uses the one
introduced in #460.
  • Loading branch information
jooola authored Jun 18, 2024
1 parent 4774e3d commit 895ada1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 79 deletions.
69 changes: 43 additions & 26 deletions hcloud/action_waiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,34 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/mockutils"
)

func TestWaitFor(t *testing.T) {
RunMockedTestCases(t,
[]MockedTestCase{
{
Name: "succeed",
WantRequests: []MockedRequest{
{"GET", "/actions?id=1509772237&page=1&sort=status&sort=id", nil, 200,
`{
WantRequests: []mockutils.Request{
{Method: "GET", Path: "/actions?id=1509772237&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237, "status": "running", "progress": 0 }
],
"meta": { "pagination": { "page": 1 }}
}`},
{"GET", "/actions?id=1509772237&page=1&sort=status&sort=id", nil, 200,
`{
}`,
},
{Method: "GET", Path: "/actions?id=1509772237&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237, "status": "success", "progress": 100 }
],
"meta": { "pagination": { "page": 1 }}
}`},
}`,
},
},
Run: func(env testEnv) {
actions := []*Action{{ID: 1509772237, Status: ActionStatusRunning}}
Expand All @@ -46,12 +52,14 @@ func TestWaitFor(t *testing.T) {
},
{
Name: "fail with unknown action",
WantRequests: []MockedRequest{
{"GET", "/actions?id=1509772237&page=1&sort=status&sort=id", nil, 200,
`{
WantRequests: []mockutils.Request{
{Method: "GET", Path: "/actions?id=1509772237&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [],
"meta": { "pagination": { "page": 1 }}
}`},
}`,
},
},
Run: func(env testEnv) {
actions := []*Action{{ID: 1509772237, Status: ActionStatusRunning}}
Expand All @@ -74,8 +82,9 @@ func TestWaitFor(t *testing.T) {
},
{
Name: "fail with api error",
WantRequests: []MockedRequest{
{"GET", "/actions?id=1509772237&page=1&sort=status&sort=id", nil, 503, ""},
WantRequests: []mockutils.Request{
{Method: "GET", Path: "/actions?id=1509772237&page=1&sort=status&sort=id",
Status: 503},
},
Run: func(env testEnv) {
actions := []*Action{{ID: 1509772237, Status: ActionStatusRunning}}
Expand All @@ -94,33 +103,40 @@ func TestWaitForFunc(t *testing.T) {
[]MockedTestCase{
{
Name: "succeed",
WantRequests: []MockedRequest{
{"GET", "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id", nil, 200,
`{
WantRequests: []mockutils.Request{
{Method: "GET", Path: "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237, "status": "running", "progress": 40 },
{ "id": 1509772238, "status": "running", "progress": 0 }
],
"meta": { "pagination": { "page": 1 }}
}`},
{"GET", "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id", nil, 200,
`{
}`,
},
{Method: "GET", Path: "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237, "status": "running", "progress": 60 },
{ "id": 1509772238, "status": "running", "progress": 50 }
],
"meta": { "pagination": { "page": 1 }}
}`},
{"GET", "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id", nil, 200,
`{
}`,
},
{Method: "GET", Path: "/actions?id=1509772237&id=1509772238&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772237, "status": "success", "progress": 100 },
{ "id": 1509772238, "status": "running", "progress": 75 }
],
"meta": { "pagination": { "page": 1 }}
}`},
{"GET", "/actions?id=1509772238&page=1&sort=status&sort=id", nil, 200,
`{
}`,
},
{Method: "GET", Path: "/actions?id=1509772238&page=1&sort=status&sort=id",
Status: 200,
JSONRaw: `{
"actions": [
{ "id": 1509772238, "status": "error", "progress": 75,
"error": {
Expand All @@ -130,7 +146,8 @@ func TestWaitForFunc(t *testing.T) {
}
],
"meta": { "pagination": { "page": 1 }}
}`},
}`,
},
},
Run: func(env testEnv) {
actions := []*Action{
Expand Down
58 changes: 5 additions & 53 deletions hcloud/mocked_test.go
Original file line number Diff line number Diff line change
@@ -1,75 +1,27 @@
package hcloud

import (
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/hetznercloud/hcloud-go/v2/hcloud/exp/mockutils"
)

type MockedTestCase struct {
Name string
WantRequests []MockedRequest
WantRequests []mockutils.Request
Run func(env testEnv)
}

type MockedRequest struct {
Method string
Path string
WantRequestBodyFunc func(t *testing.T, r *http.Request, body []byte)

Status int
Body string
}

func RunMockedTestCases(t *testing.T, testCases []MockedTestCase) {
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
env := newTestEnvWithServer(httptest.NewServer(MockedRequestHandler(t, testCase.WantRequests)), nil)
testServer := httptest.NewServer(mockutils.Handler(t, testCase.WantRequests))

env := newTestEnvWithServer(testServer, nil)
defer env.Teardown()

testCase.Run(env)
})
}
}

func MockedRequestHandler(t *testing.T, requests []MockedRequest) http.HandlerFunc {
index := 0
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if testing.Verbose() {
t.Logf("request %d: %s %s\n", index, r.Method, r.URL.Path)
}

if index >= len(requests) {
t.Fatalf("received unknown request %d", index)
}

response := requests[index]
assert.Equal(t, response.Method, r.Method)
assert.Equal(t, response.Path, r.RequestURI)

if response.WantRequestBodyFunc != nil {
buffer, err := io.ReadAll(r.Body)
defer func() {
if err := r.Body.Close(); err != nil {
t.Fatal(err)
}
}()
if err != nil {
t.Fatal(err)
}
response.WantRequestBodyFunc(t, r, buffer)
}

w.WriteHeader(response.Status)
w.Header().Set("Content-Type", "application/json")
_, err := w.Write([]byte(response.Body))
if err != nil {
t.Fatal(err)
}

index++
})
}

0 comments on commit 895ada1

Please sign in to comment.