-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reuse mockutils package (#462)
Removes the now duplicated mock request utils, and uses the one introduced in #460.
- Loading branch information
Showing
2 changed files
with
48 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++ | ||
}) | ||
} |