Repository: cloudflare/cloudflare-go
Pull request #989 (2022-07-13 00:04:54)

tests: provide a `loadFixture` helper to clean up JSON outputs
Provide a simpler approach to API payloads in tests.

codecov-commenter: # [Codecov](https://codecov.io/gh/cloudflare/cloudflare-go/pull/989?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CloudFlare) Report
> Merging [#989](https://codecov.io/gh/cloudflare/cloudflare-go/pull/989?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CloudFlare) (2b3b4dc) into [master](https://codecov.io/gh/cloudflare/cloudflare-go/commit/ea4c0d9922bbc9c62b0d33e28694087d822fa6d0?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CloudFlare) (ea4c0d9) will **increase** coverage by `0.01%`.
> The diff coverage is `62.50%`.

```diff
@@            Coverage Diff             @@
##           master     #989      +/-   ##
==========================================
+ Coverage   49.01%   49.02%   +0.01%     
==========================================
  Files         108      108              
  Lines       10361    10369       +8     
==========================================
+ Hits         5078     5083       +5     
- Misses       4176     4178       +2     
- Partials     1107     1108       +1     
```


| [Impacted Files](https://codecov.io/gh/cloudflare/cloudflare-go/pull/989?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CloudFlare) | Coverage Δ | |
|---|---|---|
| [utils.go](https://codecov.io/gh/cloudflare/cloudflare-go/pull/989/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CloudFlare#diff-dXRpbHMuZ28=) | `72.72% <62.50%> (-27.28%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/cloudflare/cloudflare-go/pull/989?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CloudFlare).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CloudFlare)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/cloudflare/cloudflare-go/pull/989?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CloudFlare). Last update [ea4c0d9...2b3b4dc](https://codecov.io/gh/cloudflare/cloudflare-go/pull/989?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CloudFlare). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=CloudFlare).


Patch:
diff --git a/utils.go b/utils.go
index 1e04f8342..38d4315d7 100644
--- a/utils.go
+++ b/utils.go
@@ -1,7 +1,10 @@
 package cloudflare
 
 import (
+	"fmt"
 	"net/url"
+	"os"
+	"path/filepath"
 
 	"github.com/google/go-querystring/query"
 )
@@ -11,3 +14,15 @@ func buildURI(path string, options interface{}) string {
 	v, _ := query.Values(options)
 	return (&url.URL{Path: path, RawQuery: v.Encode()}).String()
 }
+
+// loadFixture takes a series of path components and returns the JSON fixture at
+// that locationassociated.
+func loadFixture(parts ...string) string {
+	paths := []string{"testdata", "fixtures"}
+	paths = append(paths, parts...)
+	b, err := os.ReadFile(filepath.Join(paths...) + ".json")
+	if err != nil {
+		fmt.Print(err)
+	}
+	return string(b)
+}


Test patch:
diff --git a/testdata/fixtures/tunnel/configuration.json b/testdata/fixtures/tunnel/configuration.json
new file mode 100644
index 000000000..86192c631
--- /dev/null
+++ b/testdata/fixtures/tunnel/configuration.json
@@ -0,0 +1,11 @@
+{
+  "success": true,
+  "errors": [],
+  "messages": [],
+  "result": {
+    "config": "{\"warp-routing\": {\"enabled\": true},  \"originRequest\" : {\"connectTimeout\": 10}, \"ingress\" : [ {\"hostname\": \"test.example.com\", \"service\": \"https://localhost:8000\" } , {\"service\": \"http_status:404\"} ]}",
+    "tunnel_id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+    "version": 5,
+    "created_at": "2021-01-25T18:22:34.317854Z"
+  }
+}
diff --git a/testdata/fixtures/tunnel/empty.json b/testdata/fixtures/tunnel/empty.json
new file mode 100644
index 000000000..ca0f4176f
--- /dev/null
+++ b/testdata/fixtures/tunnel/empty.json
@@ -0,0 +1,5 @@
+{
+  "success": true,
+  "errors": [],
+  "messages": []
+}
diff --git a/testdata/fixtures/tunnel/multiple_full.json b/testdata/fixtures/tunnel/multiple_full.json
new file mode 100644
index 000000000..524c22f35
--- /dev/null
+++ b/testdata/fixtures/tunnel/multiple_full.json
@@ -0,0 +1,24 @@
+{
+  "success": true,
+  "errors": [],
+  "messages": [],
+  "result": [
+    {
+      "id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+      "name": "blog",
+      "created_at": "2009-11-10T23:00:00Z",
+      "deleted_at": "2009-11-10T23:00:00Z",
+      "connections": [
+        {
+          "colo_name": "DFW",
+          "id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+          "is_pending_reconnect": false,
+          "client_id": "dc6472cc-f1ae-44a0-b795-6b8a0ce29f90",
+          "client_version": "2022.2.0",
+          "opened_at": "2021-01-25T18:22:34.317854Z",
+          "origin_ip": "85.12.78.6"
+        }
+      ]
+    }
+  ]
+}
diff --git a/testdata/fixtures/tunnel/single_full.json b/testdata/fixtures/tunnel/single_full.json
new file mode 100644
index 000000000..b447c75c2
--- /dev/null
+++ b/testdata/fixtures/tunnel/single_full.json
@@ -0,0 +1,22 @@
+{
+  "success": true,
+  "errors": [],
+  "messages": [],
+  "result": {
+    "id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+    "name": "blog",
+    "created_at": "2009-11-10T23:00:00Z",
+    "deleted_at": "2009-11-10T23:00:00Z",
+    "connections": [
+      {
+        "colo_name": "DFW",
+        "id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
+        "is_pending_reconnect": false,
+        "client_id": "dc6472cc-f1ae-44a0-b795-6b8a0ce29f90",
+        "client_version": "2022.2.0",
+        "opened_at": "2021-01-25T18:22:34.317854Z",
+        "origin_ip": "85.12.78.6"
+      }
+    ]
+  }
+}
diff --git a/testdata/fixtures/tunnel/token.json b/testdata/fixtures/tunnel/token.json
new file mode 100644
index 000000000..9e1a53407
--- /dev/null
+++ b/testdata/fixtures/tunnel/token.json
@@ -0,0 +1,6 @@
+{
+  "success": true,
+  "errors": [],
+  "messages": [],
+  "result": "ZHNraGdhc2RraGFza2hqZGFza2poZGFza2poYXNrZGpoYWtzamRoa2FzZGpoa2FzamRoa2Rhc2po\na2FzamRoa2FqCg=="
+}
diff --git a/tunnel_test.go b/tunnel_test.go
index cee1ec6d7..ab013a694 100644
--- a/tunnel_test.go
+++ b/tunnel_test.go
@@ -17,31 +17,7 @@ func TestTunnels(t *testing.T) {
 	handler := func(w http.ResponseWriter, r *http.Request) {
 		assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
 		w.Header().Set("content-type", "application/json")
-		fmt.Fprintf(w, `{
-			"success": true,
-			"errors": [],
-			"messages": [],
-			"result": [
-				{
-					"id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
-					"name": "blog",
-					"created_at": "2009-11-10T23:00:00Z",
-					"deleted_at": "2009-11-10T23:00:00Z",
-					"connections": [
-						{
-							"colo_name": "DFW",
-							"id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
-							"is_pending_reconnect": false,
-							"client_id": "dc6472cc-f1ae-44a0-b795-6b8a0ce29f90",
-							"client_version": "2022.2.0",
-							"opened_at": "2021-01-25T18:22:34.317854Z",
-							"origin_ip": "85.12.78.6"
-						}
-					]
-				}
-			]
-		}
-		`)
+		fmt.Fprint(w, loadFixture("tunnel", "multiple_full"))
 	}
 
 	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel", handler)
@@ -78,29 +54,7 @@ func TestTunnel(t *testing.T) {
 	handler := func(w http.ResponseWriter, r *http.Request) {
 		assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
 		w.Header().Set("content-type", "application/json")
-		fmt.Fprintf(w, `{
-			"success":true,
-			"errors":[],
-			"messages":[],
-			"result":{
-				"id":"f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
-				"name":"blog",
-				"created_at":"2009-11-10T23:00:00Z",
-				"deleted_at":"2009-11-10T23:00:00Z",
-				"connections": [
-					{
-						"colo_name": "DFW",
-						"id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
-						"is_pending_reconnect": false,
-						"client_id": "dc6472cc-f1ae-44a0-b795-6b8a0ce29f90",
-						"client_version": "2022.2.0",
-						"opened_at": "2021-01-25T18:22:34.317854Z",
-						"origin_ip": "85.12.78.6"
-					}
-				]
-			}
-		}
-		`)
+		fmt.Fprint(w, loadFixture("tunnel", "single_full"))
 	}
 
 	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415", handler)
@@ -137,29 +91,7 @@ func TestCreateTunnel(t *testing.T) {
 	handler := func(w http.ResponseWriter, r *http.Request) {
 		assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method)
 		w.Header().Set("content-type", "application/json")
-		fmt.Fprintf(w, `{
-			"success":true,
-			"errors":[],
-			"messages":[],
-			"result":{
-				"id":"f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
-				"name":"blog",
-				"created_at":"2009-11-10T23:00:00Z",
-				"deleted_at":"2009-11-10T23:00:00Z",
-				"connections": [
-					{
-						"colo_name": "DFW",
-						"id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
-						"is_pending_reconnect": false,
-						"client_id": "dc6472cc-f1ae-44a0-b795-6b8a0ce29f90",
-						"client_version": "2022.2.0",
-						"opened_at": "2021-01-25T18:22:34.317854Z",
-						"origin_ip": "85.12.78.6"
-					}
-				]
-			}
-		}
-		`)
+		fmt.Fprint(w, loadFixture("tunnel", "single_full"))
 	}
 
 	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel", handler)
@@ -197,17 +129,7 @@ func TestUpdateTunnelConfiguration(t *testing.T) {
 		assert.Equal(t, http.MethodPut, r.Method, "Expected method '%s', got %s", http.MethodPut, r.Method)
 
 		w.Header().Set("content-type", "application/json")
-		fmt.Fprintf(w, `{
-			"success": true,
-			"errors": [],
-			"messages": [],
-			"result": {
-			  "config": "{\"warp-routing\": {\"enabled\": true},  \"originRequest\" : {\"connectTimeout\": 10}, \"ingress\" : [ {\"hostname\": \"test.example.com\", \"service\": \"https://localhost:8000\" } , {\"service\": \"http_status:404\"} ]}",
-			  "tunnel_id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
-			  "version": 5,
-			  "created_at": "2021-01-25T18:22:34.317854Z"
-			}
-		  }`)
+		fmt.Fprint(w, loadFixture("tunnel", "configuration"))
 	}
 
 	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/configurations", handler)
@@ -266,17 +188,7 @@ func TestGetTunnelConfiguration(t *testing.T) {
 		assert.Equal(t, http.MethodGet, r.Method, "Expected method '%s', got %s", http.MethodGet, r.Method)
 
 		w.Header().Set("content-type", "application/json")
-		fmt.Fprintf(w, `{
-			"success": true,
-			"errors": [],
-			"messages": [],
-			"result": {
-			  "config": "{\"warp-routing\": {\"enabled\": true},  \"originRequest\" : {\"connectTimeout\": 10}, \"ingress\" : [ {\"hostname\": \"test.example.com\", \"service\": \"https://localhost:8000\" } , {\"service\": \"http_status:404\"} ]}",
-			  "tunnel_id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
-			  "version": 5,
-			  "created_at": "2021-01-25T18:22:34.317854Z"
-			}
-		  }`)
+		fmt.Fprint(w, loadFixture("tunnel", "configuration"))
 	}
 
 	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/configurations", handler)
@@ -315,7 +227,7 @@ func TestTunnelConnections(t *testing.T) {
 	handler := func(w http.ResponseWriter, r *http.Request) {
 		assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
 		w.Header().Set("content-type", "application/json")
-		fmt.Fprintf(w, `{
+		fmt.Fprint(w, `{
 			"success":true,
 			"errors":[],
 			"messages":[],
@@ -385,29 +297,7 @@ func TestDeleteTunnel(t *testing.T) {
 	handler := func(w http.ResponseWriter, r *http.Request) {
 		assert.Equal(t, http.MethodDelete, r.Method, "Expected method 'DELETE', got %s", r.Method)
 		w.Header().Set("content-type", "application/json")
-		fmt.Fprintf(w, `{
-			"success":true,
-			"errors":[],
-			"messages":[],
-			"result":{
-				"id":"f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
-				"name":"blog",
-				"created_at":"2009-11-10T23:00:00Z",
-				"deleted_at":"2009-11-10T23:00:00Z",
-				"connections": [
-					{
-						"colo_name": "DFW",
-						"id": "f174e90a-fafe-4643-bbbc-4a0ed4fc8415",
-						"is_pending_reconnect": false,
-						"client_id": "dc6472cc-f1ae-44a0-b795-6b8a0ce29f90",
-						"client_version": "2022.2.0",
-						"opened_at": "2021-01-25T18:22:34.317854Z",
-						"origin_ip": "85.12.78.6"
-					}
-				]
-			}
-		}
-		`)
+		fmt.Fprint(w, loadFixture("tunnel", "single_full"))
 	}
 
 	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415", handler)
@@ -423,12 +313,7 @@ func TestCleanupTunnelConnections(t *testing.T) {
 	handler := func(w http.ResponseWriter, r *http.Request) {
 		assert.Equal(t, http.MethodDelete, r.Method, "Expected method 'DELETE', got %s", r.Method)
 		w.Header().Set("content-type", "application/json")
-		fmt.Fprintf(w, `{
-			"success": true,
-			"errors": [],
-			"messages": []
-		}
-		`)
+		fmt.Fprint(w, loadFixture("tunnel", "empty"))
 	}
 
 	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/connections", handler)
@@ -444,13 +329,7 @@ func TestTunnelToken(t *testing.T) {
 	handler := func(w http.ResponseWriter, r *http.Request) {
 		assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
 		w.Header().Set("content-type", "application/json")
-		fmt.Fprintf(w, `{
-			"success": true,
-			"errors": [],
-			"messages": [],
-			"result": "ZHNraGdhc2RraGFza2hqZGFza2poZGFza2poYXNrZGpoYWtzamRoa2FzZGpoa2FzamRoa2Rhc2po\na2FzamRoa2FqCg=="
-		  }
-		`)
+		fmt.Fprint(w, loadFixture("tunnel", "token"))
 	}
 
 	mux.HandleFunc("/accounts/"+testAccountID+"/cfd_tunnel/f174e90a-fafe-4643-bbbc-4a0ed4fc8415/token", handler)