Repository: hetznercloud/hcloud-go
Pull request #543 (2024-10-31 11:39:40)

feat(exp): add mockutil.Server helper
This helper ensures that the wrapped test server will be closed at the end of each test.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/hetznercloud/hcloud-go/pull/543?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 67.57%. Comparing base [(`09a582a`)](https://app.codecov.io/gh/hetznercloud/hcloud-go/commit/09a582a9c94f6aa3290e5eeb0f74af52dd579d3f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) to head [(`f6b0649`)](https://app.codecov.io/gh/hetznercloud/hcloud-go/commit/f6b0649d82e8631ab9e0bcfc010f094011e895cf?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #543      +/-   ##
==========================================
+ Coverage   67.50%   67.57%   +0.07%     
==========================================
  Files          46       46              
  Lines        4871     4882      +11     
==========================================
+ Hits         3288     3299      +11     
  Misses       1171     1171              
  Partials      412      412              
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/hetznercloud/hcloud-go/pull/543?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).


Patch:
diff --git a/hcloud/exp/mockutil/http.go b/hcloud/exp/mockutil/http.go
index b83e760..15b3959 100644
--- a/hcloud/exp/mockutil/http.go
+++ b/hcloud/exp/mockutil/http.go
@@ -3,6 +3,7 @@ package mockutil
 import (
 	"encoding/json"
 	"net/http"
+	"net/http/httptest"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
@@ -83,3 +84,13 @@ func Handler(t *testing.T, requests []Request) http.HandlerFunc {
 		index++
 	})
 }
+
+// Server is a [httptest.Server] wrapping a [Handler] that closes itself at the end of the test.
+func Server(t *testing.T, requests []Request) *httptest.Server {
+	t.Helper()
+
+	server := httptest.NewServer(Handler(t, requests))
+	t.Cleanup(server.Close)
+
+	return server
+}


Test patch:
diff --git a/hcloud/exp/mockutil/http_test.go b/hcloud/exp/mockutil/http_test.go
index cf2a62f..2e027e6 100644
--- a/hcloud/exp/mockutil/http_test.go
+++ b/hcloud/exp/mockutil/http_test.go
@@ -5,7 +5,6 @@ import (
 	"io"
 	"math/rand"
 	"net/http"
-	"net/http/httptest"
 	"strings"
 	"testing"
 
@@ -14,7 +13,7 @@ import (
 )
 
 func TestHandler(t *testing.T) {
-	server := httptest.NewServer(Handler(t, []Request{
+	server := Server(t, []Request{
 		{
 			Method: "GET", Path: "/",
 			Status: 200,
@@ -40,8 +39,7 @@ func TestHandler(t *testing.T) {
 			},
 			Status: 200,
 		},
-	}))
-	defer server.Close()
+	})
 
 	// Request 1
 	resp, err := http.Get(server.URL)