Repository: cloudflare/cloudflare-go
Pull request #1071 (2022-08-30 10:56:46)

url_normalization_settings: Add APIs to get and update URL normalizat…
## Description

We want to start versioning URL normalization settings in HTTP Applications, so I'm adding these APIs.
The public API implements operations for getting and updating URL normalization settings.
Public API: https://api.cloudflare.com/#url-normalization-properties

## Has your change been tested?

I created unit tests and I tested the APIs locally with my account.

## Screenshots (if appropriate):

## Types of changes

What sort of change does your code introduce/modify?

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.
- [x] This change is using publicly documented (api.cloudflare.com or developers.cloudflare.com) and stable APIs.

[1]: https://help.github.com/articles/closing-issues-using-keywords/


github-actions[bot]: Oops! It looks like no changelog entry is attached to this PR. Please include a release note as described in https://github.com/cloudflare/cloudflare-go/blob/master/docs/changelog-process.md.

Example: 

~~~
```release-note:TYPE
Release note
```
~~~

If you do not require a release note to be included, please add the `workflow/skip-changelog-entry` label.

Patch:
diff --git a/.changelog/1071.txt b/.changelog/1071.txt
new file mode 100644
index 000000000..124a8d794
--- /dev/null
+++ b/.changelog/1071.txt
@@ -0,0 +1,3 @@
+```release-note:enhancement
+url_normalization_settings: Add APIs to get and update URL normalization settings
+```
\ No newline at end of file
diff --git a/url_normalization_settings.go b/url_normalization_settings.go
new file mode 100644
index 000000000..c0247ceb9
--- /dev/null
+++ b/url_normalization_settings.go
@@ -0,0 +1,59 @@
+package cloudflare
+
+import (
+	"context"
+	"encoding/json"
+	"fmt"
+	"net/http"
+)
+
+type URLNormalizationSettings struct {
+	Type  string `json:"type"`
+	Scope string `json:"scope"`
+}
+
+type URLNormalizationSettingsResponse struct {
+	Result URLNormalizationSettings `json:"result"`
+	Response
+}
+
+type URLNormalizationSettingsUpdateParams struct {
+	Type  string `json:"type"`
+	Scope string `json:"scope"`
+}
+
+// URLNormalizationSettings API reference: https://api.cloudflare.com/#url-normalization-get-url-normalization-settings
+func (api *API) URLNormalizationSettings(ctx context.Context, rc *ResourceContainer) (URLNormalizationSettings, error) {
+	uri := fmt.Sprintf("/zones/%s/url_normalization", rc.Identifier)
+
+	res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
+	if err != nil {
+		return URLNormalizationSettings{}, err
+	}
+
+	var urlNormalizationSettingsResponse URLNormalizationSettingsResponse
+	err = json.Unmarshal(res, &urlNormalizationSettingsResponse)
+	if err != nil {
+		return URLNormalizationSettings{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
+	}
+
+	return urlNormalizationSettingsResponse.Result, nil
+}
+
+// UpdateURLNormalizationSettings https://api.cloudflare.com/#url-normalization-update-url-normalization-settings
+func (api *API) UpdateURLNormalizationSettings(ctx context.Context, rc *ResourceContainer, params URLNormalizationSettingsUpdateParams) (URLNormalizationSettings, error) {
+	uri := fmt.Sprintf("/zones/%s/url_normalization", rc.Identifier)
+
+	res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
+	if err != nil {
+		return URLNormalizationSettings{}, err
+	}
+
+	var urlNormalizationSettingsResponse URLNormalizationSettingsResponse
+	err = json.Unmarshal(res, &urlNormalizationSettingsResponse)
+	if err != nil {
+		return URLNormalizationSettings{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
+	}
+
+	return urlNormalizationSettingsResponse.Result, nil
+}


Test patch:
diff --git a/url_normalization_settings_test.go b/url_normalization_settings_test.go
new file mode 100644
index 000000000..0ad7f67a4
--- /dev/null
+++ b/url_normalization_settings_test.go
@@ -0,0 +1,86 @@
+package cloudflare
+
+import (
+	"context"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestURLNormalizationSettings(t *testing.T) {
+	setup()
+	defer teardown()
+
+	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, `{
+			"result": {
+				"type": "cloudflare",
+				"scope": "incoming"
+			},
+			"success": true,
+			"errors": [],
+			"messages": []
+		}`)
+	}
+
+	mux.HandleFunc("/zones/"+testZoneID+"/url_normalization", handler)
+
+	want := URLNormalizationSettings{
+		Type:  "cloudflare",
+		Scope: "incoming",
+	}
+
+	got, err := client.URLNormalizationSettings(context.Background(), ZoneIdentifier(testZoneID))
+	if assert.NoError(t, err) {
+		assert.Equal(t, want, got)
+	}
+}
+
+func TestUpdateURLNormalizationSettings(t *testing.T) {
+	setup()
+	defer teardown()
+
+	handler := func(w http.ResponseWriter, r *http.Request) {
+		assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)
+		body, err := ioutil.ReadAll(r.Body)
+		if err != nil {
+			panic(err)
+		}
+		defer r.Body.Close()
+
+		assert.Equal(t, `{"type":"cloudflare","scope":"incoming"}`, string(body))
+
+		w.Header().Set("content-type", "application/json")
+		fmt.Fprintf(w, `{
+			"result": {
+				"type": "cloudflare",
+				"scope": "incoming"
+			},
+			"success": true,
+			"errors": [],
+			"messages": []
+		}`)
+	}
+
+	mux.HandleFunc("/zones/"+testZoneID+"/url_normalization", handler)
+
+	params := URLNormalizationSettingsUpdateParams{
+		Type:  "cloudflare",
+		Scope: "incoming",
+	}
+
+	want := URLNormalizationSettings{
+		Type:  "cloudflare",
+		Scope: "incoming",
+	}
+
+	got, err := client.UpdateURLNormalizationSettings(context.Background(), ZoneIdentifier(testZoneID), params)
+	if assert.NoError(t, err) {
+		assert.Equal(t, want, got)
+	}
+}