diff --git a/fastly/account_event_test.go b/fastly/account_event_test.go index 917e5d2c..854a1e56 100644 --- a/fastly/account_event_test.go +++ b/fastly/account_event_test.go @@ -4,6 +4,8 @@ import ( "bytes" "io" "testing" + + "github.com/google/jsonapi" ) func TestClient_APIEvents(t *testing.T) { @@ -65,12 +67,12 @@ func TestGetAPIEventsFilterInput_formatFilters(t *testing.T) { PageNumber: 2, }, expected: map[string]string{ - "filter[customer_id]": "65135846153687547", - "filter[service_id]": "5343548168357658", - "filter[event_type]": "version.activate", - "filter[user_id]": "654681384354746951", - "page[size]": "1", - "page[number]": "2", + "filter[customer_id]": "65135846153687547", + "filter[service_id]": "5343548168357658", + "filter[event_type]": "version.activate", + "filter[user_id]": "654681384354746951", + jsonapi.QueryParamPageSize: "1", + jsonapi.QueryParamPageNumber: "2", }, }, } diff --git a/fastly/account_events.go b/fastly/account_events.go index 4de5705c..43b14855 100644 --- a/fastly/account_events.go +++ b/fastly/account_events.go @@ -111,7 +111,8 @@ func (c *Client) GetAPIEvent(i *GetAPIEventInput) (*Event, error) { } // interpretAPIEventsPage accepts a Fastly response for a set of WAF rule statuses -// and unmarshals the results. If there are more pages of results, it fetches the next +// and unmarshal the results. +// If there are more pages of results, it fetches the next // page, adds that response to the array of results, and repeats until all results have // been fetched. func (c *Client) interpretAPIEventsPage(answer *GetAPIEventsResponse, pageNum int, received *http.Response) error { @@ -174,12 +175,12 @@ func getEventsPages(body io.Reader) (EventsPaginationInfo, io.Reader, error) { func (i *GetAPIEventsFilterInput) formatEventFilters() map[string]string { result := map[string]string{} pairings := map[string]any{ - "filter[customer_id]": i.CustomerID, - "filter[service_id]": i.ServiceID, - "filter[event_type]": i.EventType, - "filter[user_id]": i.UserID, - "page[size]": i.MaxResults, - "page[number]": i.PageNumber, // starts at 1, not 0 + "filter[customer_id]": i.CustomerID, + "filter[service_id]": i.ServiceID, + "filter[event_type]": i.EventType, + "filter[user_id]": i.UserID, + jsonapi.QueryParamPageSize: i.MaxResults, + jsonapi.QueryParamPageNumber: i.PageNumber, // starts at 1, not 0 } // NOTE: This setup means we will not be able to send the zero value // of any of these filters. It doesn't appear we would need to at present. @@ -188,12 +189,12 @@ func (i *GetAPIEventsFilterInput) formatEventFilters() map[string]string { switch t := reflect.TypeOf(value).String(); t { case "string": if value != "" { - v, _ := value.(string) // type assert to avoid runtime panic (v will have zero value for its type) + v, _ := value.(string) // type asserts to avoid runtime panic (v will have zero value for its type) result[key] = v } case "int": if value != 0 { - v, _ := value.(int) // type assert to avoid runtime panic (v will have zero value for its type) + v, _ := value.(int) // type asserts to avoid runtime panic (v will have zero value for its type) result[key] = strconv.Itoa(v) } } diff --git a/fastly/acl_entry.go b/fastly/acl_entry.go index 59e20123..6eb2ab1e 100644 --- a/fastly/acl_entry.go +++ b/fastly/acl_entry.go @@ -2,6 +2,7 @@ package fastly import ( "fmt" + "net/http" "time" ) @@ -244,7 +245,7 @@ func (c *Client) UpdateACLEntry(i *UpdateACLEntryInput) (*ACLEntry, error) { path := ToSafeURL("service", i.ServiceID, "acl", i.ACLID, "entry", i.EntryID) - resp, err := c.RequestForm("PATCH", path, i, nil) + resp, err := c.RequestForm(http.MethodPatch, path, i, nil) if err != nil { return nil, err } diff --git a/fastly/client.go b/fastly/client.go index f5454fcd..3604eb46 100644 --- a/fastly/client.go +++ b/fastly/client.go @@ -80,9 +80,9 @@ type Client struct { // apiKey is the Fastly API key to authenticate requests. apiKey string - // remaining is last observed value of http header Fastly-RateLimit-Remaining + // remaining is the last observed value of http header Fastly-RateLimit-Remaining remaining int - // reset is last observed value of http header Fastly-RateLimit-Reset + // reset is the last observed value of http header Fastly-RateLimit-Reset reset int64 // updateLock forces serialization of calls that modify a service. // Concurrent modifications have undefined semantics. @@ -207,7 +207,7 @@ func (c *Client) Get(p string, ro *RequestOptions) (*http.Response, error) { ro = new(RequestOptions) } ro.Parallel = true - return c.Request("GET", p, ro) + return c.Request(http.MethodGet, p, ro) } // GetJSON issues an HTTP GET request and indicates that the response @@ -221,7 +221,7 @@ func (c *Client) GetJSON(p string, ro *RequestOptions) (*http.Response, error) { } ro.Parallel = true ro.Headers["Accept"] = JSONMimeType - return c.Request("GET", p, ro) + return c.Request(http.MethodGet, p, ro) } // Head issues an HTTP HEAD request. @@ -230,97 +230,97 @@ func (c *Client) Head(p string, ro *RequestOptions) (*http.Response, error) { ro = new(RequestOptions) } ro.Parallel = true - return c.Request("HEAD", p, ro) + return c.Request(http.MethodHead, p, ro) } // Patch issues an HTTP PATCH request. func (c *Client) Patch(p string, ro *RequestOptions) (*http.Response, error) { - return c.Request("PATCH", p, ro) + return c.Request(http.MethodPatch, p, ro) } // PatchForm issues an HTTP PUT request with the given interface form-encoded. func (c *Client) PatchForm(p string, i any, ro *RequestOptions) (*http.Response, error) { - return c.RequestForm("PATCH", p, i, ro) + return c.RequestForm(http.MethodPatch, p, i, ro) } // PatchJSON issues an HTTP PUT request with the given interface json-encoded. func (c *Client) PatchJSON(p string, i any, ro *RequestOptions) (*http.Response, error) { - return c.RequestJSON("PATCH", p, i, ro) + return c.RequestJSON(http.MethodPatch, p, i, ro) } // PatchJSONAPI issues an HTTP PUT request with the given interface json-encoded. func (c *Client) PatchJSONAPI(p string, i any, ro *RequestOptions) (*http.Response, error) { - return c.RequestJSONAPI("PATCH", p, i, ro) + return c.RequestJSONAPI(http.MethodPatch, p, i, ro) } // Post issues an HTTP POST request. func (c *Client) Post(p string, ro *RequestOptions) (*http.Response, error) { - return c.Request("POST", p, ro) + return c.Request(http.MethodPost, p, ro) } // PostForm issues an HTTP POST request with the given interface form-encoded. func (c *Client) PostForm(p string, i any, ro *RequestOptions) (*http.Response, error) { - return c.RequestForm("POST", p, i, ro) + return c.RequestForm(http.MethodPost, p, i, ro) } // PostJSON issues an HTTP POST request with the given interface json-encoded. func (c *Client) PostJSON(p string, i any, ro *RequestOptions) (*http.Response, error) { - return c.RequestJSON("POST", p, i, ro) + return c.RequestJSON(http.MethodPost, p, i, ro) } // PostJSONAPI issues an HTTP POST request with the given interface json-encoded. func (c *Client) PostJSONAPI(p string, i any, ro *RequestOptions) (*http.Response, error) { - return c.RequestJSONAPI("POST", p, i, ro) + return c.RequestJSONAPI(http.MethodPost, p, i, ro) } // PostJSONAPIBulk issues an HTTP POST request with the given interface json-encoded and bulk requests. func (c *Client) PostJSONAPIBulk(p string, i any, ro *RequestOptions) (*http.Response, error) { - return c.RequestJSONAPIBulk("POST", p, i, ro) + return c.RequestJSONAPIBulk(http.MethodPost, p, i, ro) } // Put issues an HTTP PUT request. func (c *Client) Put(p string, ro *RequestOptions) (*http.Response, error) { - return c.Request("PUT", p, ro) + return c.Request(http.MethodPut, p, ro) } // PutForm issues an HTTP PUT request with the given interface form-encoded. func (c *Client) PutForm(p string, i any, ro *RequestOptions) (*http.Response, error) { - return c.RequestForm("PUT", p, i, ro) + return c.RequestForm(http.MethodPut, p, i, ro) } // PutFormFile issues an HTTP PUT request (multipart/form-encoded) to put a file to an endpoint. func (c *Client) PutFormFile(urlPath, filePath, fieldName string, ro *RequestOptions) (*http.Response, error) { - return c.RequestFormFile("PUT", urlPath, filePath, fieldName, ro) + return c.RequestFormFile(http.MethodPut, urlPath, filePath, fieldName, ro) } // PutFormFileFromReader issues an HTTP PUT request (multipart/form-encoded) to put a file to an endpoint. func (c *Client) PutFormFileFromReader(urlPath, fileName string, fileBytes io.Reader, fieldName string, ro *RequestOptions) (*http.Response, error) { - return c.RequestFormFileFromReader("PUT", urlPath, fileName, fileBytes, fieldName, ro) + return c.RequestFormFileFromReader(http.MethodPut, urlPath, fileName, fileBytes, fieldName, ro) } // PutJSON issues an HTTP PUT request with the given interface json-encoded. func (c *Client) PutJSON(p string, i any, ro *RequestOptions) (*http.Response, error) { - return c.RequestJSON("PUT", p, i, ro) + return c.RequestJSON(http.MethodPut, p, i, ro) } // PutJSONAPI issues an HTTP PUT request with the given interface json-encoded. func (c *Client) PutJSONAPI(p string, i any, ro *RequestOptions) (*http.Response, error) { - return c.RequestJSONAPI("PUT", p, i, ro) + return c.RequestJSONAPI(http.MethodPut, p, i, ro) } // Delete issues an HTTP DELETE request. func (c *Client) Delete(p string, ro *RequestOptions) (*http.Response, error) { - return c.Request("DELETE", p, ro) + return c.Request(http.MethodDelete, p, ro) } // DeleteJSONAPI issues an HTTP DELETE request with the given interface json-encoded. func (c *Client) DeleteJSONAPI(p string, i any, ro *RequestOptions) (*http.Response, error) { - return c.RequestJSONAPI("DELETE", p, i, ro) + return c.RequestJSONAPI(http.MethodDelete, p, i, ro) } // DeleteJSONAPIBulk issues an HTTP DELETE request with the given interface json-encoded and bulk requests. func (c *Client) DeleteJSONAPIBulk(p string, i any, ro *RequestOptions) (*http.Response, error) { - return c.RequestJSONAPIBulk("DELETE", p, i, ro) + return c.RequestJSONAPIBulk(http.MethodDelete, p, i, ro) } // Request makes an HTTP request against the HTTPClient using the given verb, @@ -343,7 +343,7 @@ func (c *Client) Request(verb, p string, ro *RequestOptions) (*http.Response, er // contents since io.Reader is not seekable and cannot // be rewound - r.Header.Del("Fastly-Key") + r.Header.Del(APIKeyHeader) dump, _ := httputil.DumpRequest(r, true) // httputil.DumpRequest has read the Body from 'r', @@ -365,11 +365,11 @@ func (c *Client) Request(verb, p string, ro *RequestOptions) (*http.Response, er fmt.Printf("http.Response (dump): %q\n", dump) } - if err != nil { + if err != nil || resp == nil { return resp, err } - if verb != "GET" && verb != "HEAD" { + if verb != http.MethodGet && verb != http.MethodHead { remaining := resp.Header.Get("Fastly-RateLimit-Remaining") if remaining != "" { if val, err := strconv.Atoi(remaining); err == nil { @@ -402,7 +402,7 @@ type RequestOptions struct { // TODO: Lookout for this when it comes to the future code-generated API // client world, as this special case might get omitted accidentally. HealthCheckHeaders bool - // Can this request run in parallel + // Can this request run in parallel? Parallel bool // Params is a map of key-value pairs that will be added to the Request. Params map[string]string diff --git a/fastly/client_test.go b/fastly/client_test.go index 7707f340..c157d55a 100644 --- a/fastly/client_test.go +++ b/fastly/client_test.go @@ -1,6 +1,7 @@ package fastly import ( + "net/http" "net/url" "strings" "testing" @@ -8,7 +9,7 @@ import ( func TestClient_RawRequest(t *testing.T) { validAPIHosts := []string{ - "https://api.fastly.com", + DefaultEndpoint, "https://api.fastly.com/", } purgeAPIPaths := []string{ @@ -29,7 +30,7 @@ func TestClient_RawRequest(t *testing.T) { } for _, p := range purgeAPIPaths { for _, k := range cacheKeys { - r, err := c.RawRequest("GET", p+url.PathEscape(k), nil) + r, err := c.RawRequest(http.MethodGet, p+url.PathEscape(k), nil) // Cannot test results for success if we get an error if err != nil { t.Fatal("Could not make RawRequest for ", h, p, k) diff --git a/fastly/config_store.go b/fastly/config_store.go index e570de2c..df472466 100644 --- a/fastly/config_store.go +++ b/fastly/config_store.go @@ -38,7 +38,7 @@ func (c *Client) CreateConfigStore(i *CreateConfigStoreInput) (*ConfigStore, err resp, err := c.PostForm(path, i, &RequestOptions{ Headers: map[string]string{ // PostForm adds the appropriate Content-Type header. - "Accept": "application/json", + "Accept": JSONMimeType, }, Parallel: true, }) @@ -71,7 +71,7 @@ func (c *Client) DeleteConfigStore(i *DeleteConfigStoreInput) error { resp, err := c.Delete(path, &RequestOptions{ Headers: map[string]string{ - "Accept": "application/json", + "Accept": JSONMimeType, }, Parallel: true, }) @@ -101,7 +101,7 @@ func (c *Client) GetConfigStore(i *GetConfigStoreInput) (*ConfigStore, error) { resp, err := c.Get(path, &RequestOptions{ Headers: map[string]string{ - "Accept": "application/json", + "Accept": JSONMimeType, }, Parallel: true, }) @@ -134,7 +134,7 @@ func (c *Client) GetConfigStoreMetadata(i *GetConfigStoreMetadataInput) (*Config resp, err := c.Get(path, &RequestOptions{ Headers: map[string]string{ - "Accept": "application/json", + "Accept": JSONMimeType, }, Parallel: true, }) @@ -163,7 +163,7 @@ func (c *Client) ListConfigStores(i *ListConfigStoresInput) ([]*ConfigStore, err requestOptions := &RequestOptions{ Headers: map[string]string{ - "Accept": "application/json", + "Accept": JSONMimeType, }, Parallel: true, } @@ -206,7 +206,7 @@ func (c *Client) ListConfigStoreServices(i *ListConfigStoreServicesInput) ([]*Se resp, err := c.Get(path, &RequestOptions{ Headers: map[string]string{ - "Accept": "application/json", + "Accept": JSONMimeType, }, Parallel: true, }) @@ -243,7 +243,7 @@ func (c *Client) UpdateConfigStore(i *UpdateConfigStoreInput) (*ConfigStore, err resp, err := c.PutForm(path, i, &RequestOptions{ Headers: map[string]string{ // PutForm adds the appropriate Content-Type header. - "Accept": "application/json", + "Accept": JSONMimeType, }, Parallel: true, }) diff --git a/fastly/config_store_item.go b/fastly/config_store_item.go index f54829e6..a10a18ee 100644 --- a/fastly/config_store_item.go +++ b/fastly/config_store_item.go @@ -41,7 +41,7 @@ func (c *Client) CreateConfigStoreItem(i *CreateConfigStoreItemInput) (*ConfigSt resp, err := c.PostForm(path, i, &RequestOptions{ Headers: map[string]string{ // PostForm adds the appropriate Content-Type header. - "Accept": "application/json", + "Accept": JSONMimeType, }, Parallel: true, }) @@ -79,7 +79,7 @@ func (c *Client) DeleteConfigStoreItem(i *DeleteConfigStoreItemInput) error { resp, err := c.Delete(path, &RequestOptions{ Headers: map[string]string{ - "Accept": "application/json", + "Accept": JSONMimeType, }, Parallel: true, }) @@ -114,7 +114,7 @@ func (c *Client) GetConfigStoreItem(i *GetConfigStoreItemInput) (*ConfigStoreIte resp, err := c.Get(path, &RequestOptions{ Headers: map[string]string{ - "Accept": "application/json", + "Accept": JSONMimeType, }, Parallel: true, }) @@ -197,7 +197,7 @@ func (c *Client) UpdateConfigStoreItem(i *UpdateConfigStoreItemInput) (*ConfigSt resp, err := c.RequestForm(httpMethod, path, i, &RequestOptions{ Headers: map[string]string{ // RequestForm adds the appropriate Content-Type header. - "Accept": "application/json", + "Accept": JSONMimeType, }, Parallel: true, }) diff --git a/fastly/errors.go b/fastly/errors.go index 66ba83f7..dc9f7319 100644 --- a/fastly/errors.go +++ b/fastly/errors.go @@ -528,10 +528,10 @@ func (e *HTTPError) String() string { // IsBadRequest returns true if the HTTP status code is 400, false otherwise. func (e *HTTPError) IsBadRequest() bool { - return e.StatusCode == 400 + return e.StatusCode == http.StatusBadRequest } // IsNotFound returns true if the HTTP status code is 404, false otherwise. func (e *HTTPError) IsNotFound() bool { - return e.StatusCode == 404 + return e.StatusCode == http.StatusNotFound } diff --git a/fastly/errors_test.go b/fastly/errors_test.go index 617d88cf..6fe09374 100644 --- a/fastly/errors_test.go +++ b/fastly/errors_test.go @@ -15,13 +15,13 @@ func TestNewHTTPError(t *testing.T) { t.Run("legacy", func(t *testing.T) { resp := &http.Response{ - StatusCode: 404, + StatusCode: http.StatusNotFound, Body: io.NopCloser(bytes.NewBufferString( `{"msg": "hello", "detail": "nope"}`)), } e := NewHTTPError(resp) - if e.StatusCode != 404 { + if e.StatusCode != http.StatusNotFound { t.Errorf("bad status code: %d", e.StatusCode) } @@ -45,15 +45,15 @@ func TestNewHTTPError(t *testing.T) { t.Run("jsonapi", func(t *testing.T) { resp := &http.Response{ - StatusCode: 404, + StatusCode: http.StatusNotFound, Header: http.Header(map[string][]string{"Content-Type": {jsonapi.MediaType}}), Body: io.NopCloser(bytes.NewBufferString( `{"errors":[{"id":"abc123", "title":"Not found", "detail":"That resource does not exist"}]}`)), } e := NewHTTPError(resp) - if e.StatusCode != 404 { - t.Errorf("expected %d to be %d", e.StatusCode, 404) + if e.StatusCode != http.StatusNotFound { + t.Errorf("expected %d to be %d", e.StatusCode, http.StatusNotFound) } expected := strings.TrimSpace(` @@ -77,7 +77,7 @@ func TestNewHTTPError(t *testing.T) { t.Run("problem detail", func(t *testing.T) { resp := &http.Response{ - StatusCode: 404, + StatusCode: http.StatusNotFound, Header: http.Header(map[string][]string{"Content-Type": {"application/problem+json"}}), Body: io.NopCloser(bytes.NewBufferString( `{"title": "Error", "detail": "this was an error", "status": 404}`, @@ -85,8 +85,8 @@ func TestNewHTTPError(t *testing.T) { } e := NewHTTPError(resp) - if e.StatusCode != 404 { - t.Errorf("expected %d to be %d", e.StatusCode, 404) + if e.StatusCode != http.StatusNotFound { + t.Errorf("expected %d to be %d", e.StatusCode, http.StatusNotFound) } expected := strings.TrimSpace(` @@ -116,14 +116,14 @@ func TestNewHTTPError(t *testing.T) { for _, ct := range contentTypes { resp := &http.Response{ - StatusCode: 404, + StatusCode: http.StatusNotFound, Header: http.Header(map[string][]string{"Content-Type": {ct}}), Body: io.NopCloser(bytes.NewBufferString(`THIS IS NOT JSON`)), } e := NewHTTPError(resp) - if e.StatusCode != 404 { - t.Errorf("expected %d to be %d", e.StatusCode, 404) + if e.StatusCode != http.StatusNotFound { + t.Errorf("expected %d to be %d", e.StatusCode, http.StatusNotFound) } expected := strings.TrimSpace(` diff --git a/fastly/fastly_test_utils.go b/fastly/fastly_test_utils.go index 1d8fb873..ee4255e1 100644 --- a/fastly/fastly_test_utils.go +++ b/fastly/fastly_test_utils.go @@ -114,7 +114,7 @@ func getRecorder(t *testing.T, fixture string) *recorder.Recorder { // Add a filter which removes Fastly-Key header from all recorded requests. r.AddFilter(func(i *cassette.Interaction) error { - delete(i.Request.Headers, "Fastly-Key") + delete(i.Request.Headers, APIKeyHeader) return nil }) diff --git a/fastly/health_check_test.go b/fastly/health_check_test.go index 06448259..c521d0eb 100644 --- a/fastly/health_check_test.go +++ b/fastly/health_check_test.go @@ -1,6 +1,7 @@ package fastly import ( + "net/http" "testing" ) @@ -20,7 +21,7 @@ func TestClient_HealthChecks(t *testing.T) { ServiceID: TestDeliveryServiceID, ServiceVersion: *tv.Number, Name: ToPointer("test-healthcheck"), - Method: ToPointer("HEAD"), + Method: ToPointer(http.MethodHead), Headers: ToPointer([]string{ "Foo: Bar", "Baz: Qux", @@ -30,7 +31,7 @@ func TestClient_HealthChecks(t *testing.T) { HTTPVersion: ToPointer("1.1"), Timeout: ToPointer(1500), CheckInterval: ToPointer(2500), - ExpectedResponse: ToPointer(200), + ExpectedResponse: ToPointer(http.StatusOK), Window: ToPointer(5000), Threshold: ToPointer(10), Initial: ToPointer(10), @@ -60,7 +61,7 @@ func TestClient_HealthChecks(t *testing.T) { if *hc.Name != "test-healthcheck" { t.Errorf("bad name: %q", *hc.Name) } - if *hc.Method != "HEAD" { + if *hc.Method != http.MethodHead { t.Errorf("bad address: %q", *hc.Method) } if *hc.Host != "example.com" { @@ -78,7 +79,7 @@ func TestClient_HealthChecks(t *testing.T) { if *hc.CheckInterval != 2500 { t.Errorf("bad check_interval: %q", *hc.CheckInterval) } - if *hc.ExpectedResponse != 200 { + if *hc.ExpectedResponse != http.StatusOK { t.Errorf("bad timeout: %q", *hc.ExpectedResponse) } if *hc.Window != 5000 { diff --git a/fastly/image_optimizer_default_settings.go b/fastly/image_optimizer_default_settings.go index fee2647f..e3cfbb2d 100644 --- a/fastly/image_optimizer_default_settings.go +++ b/fastly/image_optimizer_default_settings.go @@ -2,6 +2,7 @@ package fastly import ( "encoding/json" + "net/http" "strconv" ) @@ -137,7 +138,7 @@ func (c *Client) GetImageOptimizerDefaultSettings(i *GetImageOptimizerDefaultSet resp, err := c.Get(path, nil) if err != nil { if herr, ok := err.(*HTTPError); ok { - if herr.StatusCode == 404 { + if herr.StatusCode == http.StatusNotFound { // API endpoint returns 404 for services without Image Optimizer settings set. return nil, nil } diff --git a/fastly/logging_blobstorage_test.go b/fastly/logging_blobstorage_test.go index 900fffa9..87cc2a8d 100644 --- a/fastly/logging_blobstorage_test.go +++ b/fastly/logging_blobstorage_test.go @@ -1,11 +1,12 @@ package fastly import ( + "net/http" "testing" ) const ( - MiB = 1048576 + MiB = http.DefaultMaxHeaderBytes ) func TestClient_BlobStorages(t *testing.T) { diff --git a/fastly/logging_https_test.go b/fastly/logging_https_test.go index 3a91c822..959f1af1 100644 --- a/fastly/logging_https_test.go +++ b/fastly/logging_https_test.go @@ -1,6 +1,7 @@ package fastly import ( + "net/http" "strings" "testing" ) @@ -45,10 +46,10 @@ Wm7DCfrPNGVwFWUQOmsPue9rZBgO URL: ToPointer("https://example.com/"), RequestMaxEntries: ToPointer(1), RequestMaxBytes: ToPointer(1000), - ContentType: ToPointer("application/json"), + ContentType: ToPointer(JSONMimeType), HeaderName: ToPointer("X-Example-Header"), HeaderValue: ToPointer("ExampleValue"), - Method: ToPointer("PUT"), + Method: ToPointer(http.MethodPut), JSONFormat: ToPointer("2"), Placement: ToPointer("waf_debug"), TLSCACert: ToPointer(caCert), @@ -96,7 +97,7 @@ Wm7DCfrPNGVwFWUQOmsPue9rZBgO if *h.RequestMaxBytes != 1000 { t.Errorf("bad request_max_bytes: %q", *h.RequestMaxBytes) } - if *h.ContentType != "application/json" { + if *h.ContentType != JSONMimeType { t.Errorf("bad content_type: %q", *h.ContentType) } if *h.HeaderName != "X-Example-Header" { @@ -105,7 +106,7 @@ Wm7DCfrPNGVwFWUQOmsPue9rZBgO if *h.HeaderValue != "ExampleValue" { t.Errorf("bad *h.ader_value: %q", *h.HeaderValue) } - if *h.Method != "PUT" { + if *h.Method != http.MethodPut { t.Errorf("bad met*h.d: %q", *h.Method) } if *h.JSONFormat != "2" { @@ -220,7 +221,7 @@ Wm7DCfrPNGVwFWUQOmsPue9rZBgO ServiceVersion: *tv.Number, Name: "test-https", NewName: ToPointer("new-test-https"), - Method: ToPointer("POST"), + Method: ToPointer(http.MethodPost), }) }) if err != nil { @@ -229,7 +230,7 @@ Wm7DCfrPNGVwFWUQOmsPue9rZBgO if *uh.Name != "new-test-https" { t.Errorf("bad name: %q", *uh.Name) } - if *uh.Method != "POST" { + if *uh.Method != http.MethodPost { t.Errorf("bad method: %q", *uh.Method) } diff --git a/fastly/paginator.go b/fastly/paginator.go index aa9ee3ac..8c92ed4e 100644 --- a/fastly/paginator.go +++ b/fastly/paginator.go @@ -5,6 +5,8 @@ import ( "net/url" "strconv" + "github.com/google/jsonapi" + "github.com/peterhellberg/link" ) @@ -117,13 +119,13 @@ func (p *ListPaginator[T]) GetNext() ([]*T, error) { for _, l := range link.ParseResponse(resp) { // Indicates the Link response header contained the next page instruction - if l.Rel == "next" { + if l.Rel == jsonapi.KeyNextPage { u, _ := url.Parse(l.URI) query := u.Query() p.NextPage, _ = strconv.Atoi(query["page"][0]) } // Indicates the Link response header contained the last page instruction - if l.Rel == "last" { + if l.Rel == jsonapi.KeyLastPage { u, _ := url.Parse(l.URI) query := u.Query() p.LastPage, _ = strconv.Atoi(query["page"][0]) diff --git a/fastly/purge.go b/fastly/purge.go index 2fd70ebc..e71311b4 100644 --- a/fastly/purge.go +++ b/fastly/purge.go @@ -1,6 +1,7 @@ package fastly import ( + "net/http" "net/url" "strings" ) @@ -97,7 +98,7 @@ func (c *Client) PurgeKey(i *PurgeKeyInput) (*Purge, error) { ro := new(RequestOptions) ro.Parallel = true - req, err := c.RawRequest("POST", path, ro) + req, err := c.RawRequest(http.MethodPost, path, ro) if err != nil { return nil, err } @@ -142,7 +143,7 @@ func (c *Client) PurgeKeys(i *PurgeKeysInput) (map[string]string, error) { ro := new(RequestOptions) ro.Parallel = true - req, err := c.RawRequest("POST", path, ro) + req, err := c.RawRequest(http.MethodPost, path, ro) if err != nil { return nil, err } @@ -180,7 +181,7 @@ func (c *Client) PurgeAll(i *PurgeAllInput) (*Purge, error) { path := ToSafeURL("service", i.ServiceID, "purge_all") - req, err := c.RawRequest("POST", path, nil) + req, err := c.RawRequest(http.MethodPost, path, nil) if err != nil { return nil, err } diff --git a/fastly/secret_store.go b/fastly/secret_store.go index ff440ad9..f2bcc1eb 100644 --- a/fastly/secret_store.go +++ b/fastly/secret_store.go @@ -100,8 +100,8 @@ func (c *Client) ListSecretStores(i *ListSecretStoresInput) (*SecretStores, erro resp, err := c.Get(path, &RequestOptions{ Params: params, Headers: map[string]string{ - "Content-Type": "application/json", - "Accept": "application/json", + "Content-Type": JSONMimeType, + "Accept": JSONMimeType, }, Parallel: true, }) @@ -134,8 +134,8 @@ func (c *Client) GetSecretStore(i *GetSecretStoreInput) (*SecretStore, error) { resp, err := c.Get(path, &RequestOptions{ Headers: map[string]string{ - "Content-Type": "application/json", - "Accept": "application/json", + "Content-Type": JSONMimeType, + "Accept": JSONMimeType, }, Parallel: true, }) @@ -168,8 +168,8 @@ func (c *Client) DeleteSecretStore(i *DeleteSecretStoreInput) error { resp, err := c.Delete(path, &RequestOptions{ Headers: map[string]string{ - "Content-Type": "application/json", - "Accept": "application/json", + "Content-Type": JSONMimeType, + "Accept": JSONMimeType, }, Parallel: true, }) @@ -253,8 +253,8 @@ func (c *Client) CreateSecret(i *CreateSecretInput) (*Secret, error) { resp, err := c.Request(method, path, &RequestOptions{ Headers: map[string]string{ - "Content-Type": "application/json", - "Accept": "application/json", + "Content-Type": JSONMimeType, + "Accept": JSONMimeType, }, Body: &body, Parallel: true, @@ -312,8 +312,8 @@ func (c *Client) ListSecrets(i *ListSecretsInput) (*Secrets, error) { resp, err := c.Get(path, &RequestOptions{ Params: params, Headers: map[string]string{ - "Content-Type": "application/json", - "Accept": "application/json", + "Content-Type": JSONMimeType, + "Accept": JSONMimeType, }, Parallel: true, }) @@ -351,8 +351,8 @@ func (c *Client) GetSecret(i *GetSecretInput) (*Secret, error) { resp, err := c.Get(path, &RequestOptions{ Headers: map[string]string{ - "Content-Type": "application/json", - "Accept": "application/json", + "Content-Type": JSONMimeType, + "Accept": JSONMimeType, }, Parallel: true, }) @@ -390,8 +390,8 @@ func (c *Client) DeleteSecret(i *DeleteSecretInput) error { resp, err := c.Delete(path, &RequestOptions{ Headers: map[string]string{ - "Content-Type": "application/json", - "Accept": "application/json", + "Content-Type": JSONMimeType, + "Accept": JSONMimeType, }, Parallel: true, }) @@ -443,8 +443,8 @@ func (c *Client) CreateClientKey() (*ClientKey, error) { resp, err := c.Post(path, &RequestOptions{ Headers: map[string]string{ - "Content-Type": "application/json", - "Accept": "application/json", + "Content-Type": JSONMimeType, + "Accept": JSONMimeType, }, Parallel: true, }) @@ -469,8 +469,8 @@ func (c *Client) GetSigningKey() (ed25519.PublicKey, error) { resp, err := c.Get(path, &RequestOptions{ Headers: map[string]string{ - "Content-Type": "application/json", - "Accept": "application/json", + "Content-Type": JSONMimeType, + "Accept": JSONMimeType, }, Parallel: true, }) diff --git a/fastly/service_authorization.go b/fastly/service_authorization.go index 5ccf4835..b5680b75 100644 --- a/fastly/service_authorization.go +++ b/fastly/service_authorization.go @@ -55,8 +55,8 @@ type ListServiceAuthorizationsInput struct { func (i *ListServiceAuthorizationsInput) formatFilters() map[string]string { result := map[string]string{} pairings := map[string]int{ - "page[size]": i.PageSize, - "page[number]": i.PageNumber, + jsonapi.QueryParamPageSize: i.PageSize, + jsonapi.QueryParamPageNumber: i.PageNumber, } for key, value := range pairings { diff --git a/fastly/service_authorization_test.go b/fastly/service_authorization_test.go index 6349f52d..88c1b478 100644 --- a/fastly/service_authorization_test.go +++ b/fastly/service_authorization_test.go @@ -3,6 +3,8 @@ package fastly import ( "reflect" "testing" + + "github.com/google/jsonapi" ) func TestClient_ServiceAuthorizations(t *testing.T) { @@ -172,8 +174,8 @@ func TestClient_listServiceAuthorizations_formatFilters(t *testing.T) { PageNumber: 2, }, local: map[string]string{ - "page[size]": "2", - "page[number]": "2", + jsonapi.QueryParamPageSize: "2", + jsonapi.QueryParamPageNumber: "2", }, }, } diff --git a/fastly/tls_custom_activation.go b/fastly/tls_custom_activation.go index e94222a0..7784059d 100644 --- a/fastly/tls_custom_activation.go +++ b/fastly/tls_custom_activation.go @@ -43,8 +43,8 @@ func (i *ListTLSActivationsInput) formatFilters() map[string]string { "filter[tls_configuration.id]": i.FilterTLSConfigurationID, "filter[tls_domain.id]": i.FilterTLSDomainID, "include": i.Include, - "page[number]": i.PageNumber, - "page[size]": i.PageSize, + jsonapi.QueryParamPageNumber: i.PageNumber, + jsonapi.QueryParamPageSize: i.PageSize, } for key, value := range pairings { @@ -71,7 +71,7 @@ func (c *Client) ListTLSActivations(i *ListTLSActivationsInput) ([]*TLSActivatio filters := &RequestOptions{ Params: i.formatFilters(), Headers: map[string]string{ - "Accept": "application/vnd.api+json", // this is required otherwise the filters don't work + "Accept": jsonapi.MediaType, // this is required otherwise the filters don't work }, } @@ -116,7 +116,7 @@ func (c *Client) GetTLSActivation(i *GetTLSActivationInput) (*TLSActivation, err ro := &RequestOptions{ Headers: map[string]string{ - "Accept": "application/vnd.api+json", // this is required otherwise the params don't work + "Accept": jsonapi.MediaType, // this is required otherwise the params don't work }, } diff --git a/fastly/tls_custom_certificate.go b/fastly/tls_custom_certificate.go index 67f8e05c..e015768a 100644 --- a/fastly/tls_custom_certificate.go +++ b/fastly/tls_custom_certificate.go @@ -47,13 +47,13 @@ type ListCustomTLSCertificatesInput struct { func (i *ListCustomTLSCertificatesInput) formatFilters() map[string]string { result := map[string]string{} pairings := map[string]any{ - "filter[in_use]": i.FilterInUse, - "filter[not_after]": i.FilterNotAfter, - "filter[tls_domains.id]": i.FilterTLSDomainsID, - "include": i.Include, - "page[size]": i.PageSize, - "page[number]": i.PageNumber, - "sort": i.Sort, + "filter[in_use]": i.FilterInUse, + "filter[not_after]": i.FilterNotAfter, + "filter[tls_domains.id]": i.FilterTLSDomainsID, + "include": i.Include, + jsonapi.QueryParamPageSize: i.PageSize, + jsonapi.QueryParamPageNumber: i.PageNumber, + "sort": i.Sort, } for key, value := range pairings { @@ -82,7 +82,7 @@ func (c *Client) ListCustomTLSCertificates(i *ListCustomTLSCertificatesInput) ([ filters := &RequestOptions{ Params: i.formatFilters(), Headers: map[string]string{ - "Accept": "application/vnd.api+json", // this is required otherwise the filters don't work + "Accept": jsonapi.MediaType, // this is required otherwise the filters don't work }, } diff --git a/fastly/tls_custom_configuration.go b/fastly/tls_custom_configuration.go index 0b310ba8..1848328d 100644 --- a/fastly/tls_custom_configuration.go +++ b/fastly/tls_custom_configuration.go @@ -45,10 +45,10 @@ type ListCustomTLSConfigurationsInput struct { func (i *ListCustomTLSConfigurationsInput) formatFilters() map[string]string { result := map[string]string{} pairings := map[string]any{ - "filter[bulk]": i.FilterBulk, - "include": i.Include, - "page[size]": i.PageSize, - "page[number]": i.PageNumber, + "filter[bulk]": i.FilterBulk, + "include": i.Include, + jsonapi.QueryParamPageSize: i.PageSize, + jsonapi.QueryParamPageNumber: i.PageNumber, } for key, value := range pairings { @@ -75,7 +75,7 @@ func (c *Client) ListCustomTLSConfigurations(i *ListCustomTLSConfigurationsInput ro := &RequestOptions{ Params: i.formatFilters(), Headers: map[string]string{ - "Accept": "application/vnd.api+json", // this is required otherwise the filters don't work + "Accept": jsonapi.MediaType, // this is required otherwise the filters don't work }, } @@ -120,7 +120,7 @@ func (c *Client) GetCustomTLSConfiguration(i *GetCustomTLSConfigurationInput) (* ro := &RequestOptions{ Headers: map[string]string{ - "Accept": "application/vnd.api+json", // this is required otherwise the params don't work + "Accept": jsonapi.MediaType, // this is required otherwise the params don't work }, } diff --git a/fastly/tls_custom_domain.go b/fastly/tls_custom_domain.go index 2694eb7c..058c0526 100644 --- a/fastly/tls_custom_domain.go +++ b/fastly/tls_custom_domain.go @@ -34,8 +34,8 @@ func (l *ListTLSDomainsInput) formatFilters() map[string]string { "filter[tls_certificates.id]": l.FilterTLSCertificateID, "filter[tls_subscriptions.id]": l.FilterTLSSubscriptionID, "include": l.Include, - "page[number]": l.PageNumber, - "page[size]": l.PageSize, + jsonapi.QueryParamPageNumber: l.PageNumber, + jsonapi.QueryParamPageSize: l.PageSize, "sort": l.Sort, } @@ -65,7 +65,7 @@ func (c *Client) ListTLSDomains(i *ListTLSDomainsInput) ([]*TLSDomain, error) { filters := &RequestOptions{ Params: i.formatFilters(), Headers: map[string]string{ - "Accept": "application/vnd.api+json", // this is required otherwise the filters don't work + "Accept": jsonapi.MediaType, // this is required otherwise the filters don't work }, } diff --git a/fastly/tls_mutual_authentication.go b/fastly/tls_mutual_authentication.go index 08476794..98720db2 100644 --- a/fastly/tls_mutual_authentication.go +++ b/fastly/tls_mutual_authentication.go @@ -37,10 +37,10 @@ func (i *ListTLSMutualAuthenticationsInput) formatFilters() map[string]string { result["include"] = strings.Join(i.Include, ",") } if i.PageSize > 0 { - result["page[size]"] = strconv.Itoa(i.PageSize) + result[jsonapi.QueryParamPageSize] = strconv.Itoa(i.PageSize) } if i.PageNumber > 0 { - result["page[number]"] = strconv.Itoa(i.PageNumber) + result[jsonapi.QueryParamPageNumber] = strconv.Itoa(i.PageNumber) } return result } @@ -51,7 +51,7 @@ func (c *Client) ListTLSMutualAuthentication(i *ListTLSMutualAuthenticationsInpu filters := &RequestOptions{ Params: i.formatFilters(), Headers: map[string]string{ - "Accept": "application/vnd.api+json", // this is required otherwise the filters don't work + "Accept": jsonapi.MediaType, // this is required otherwise the filters don't work }, } @@ -99,7 +99,7 @@ func (c *Client) GetTLSMutualAuthentication(i *GetTLSMutualAuthenticationInput) "include": i.Include, }, Headers: map[string]string{ - "Accept": "application/vnd.api+json", // this is required otherwise the filters don't work + "Accept": jsonapi.MediaType, // this is required otherwise the filters don't work }, } } diff --git a/fastly/tls_platform.go b/fastly/tls_platform.go index e4af2ae2..c11962d5 100644 --- a/fastly/tls_platform.go +++ b/fastly/tls_platform.go @@ -60,8 +60,8 @@ func (i *ListBulkCertificatesInput) formatFilters() map[string]string { result := map[string]string{} pairings := map[string]any{ "filter[tls_domains.id][match]": i.FilterTLSDomainsIDMatch, - "page[size]": i.PageSize, - "page[number]": i.PageNumber, + jsonapi.QueryParamPageSize: i.PageSize, + jsonapi.QueryParamPageNumber: i.PageNumber, "sort": i.Sort, } for key, value := range pairings { @@ -85,7 +85,7 @@ func (c *Client) ListBulkCertificates(i *ListBulkCertificatesInput) ([]*BulkCert filters := &RequestOptions{ Params: i.formatFilters(), Headers: map[string]string{ - "Accept": "application/vnd.api+json", // this is required otherwise the filters don't work + "Accept": jsonapi.MediaType, // this is required otherwise the filters don't work }, } diff --git a/fastly/tls_private_keys.go b/fastly/tls_private_keys.go index 2fcee28a..4bc031e0 100644 --- a/fastly/tls_private_keys.go +++ b/fastly/tls_private_keys.go @@ -41,9 +41,9 @@ type ListPrivateKeysInput struct { func (i *ListPrivateKeysInput) formatFilters() map[string]string { result := map[string]string{} pairings := map[string]any{ - "filter[in_use]": i.FilterInUse, - "page[size]": i.PageSize, - "page[number]": i.PageNumber, + "filter[in_use]": i.FilterInUse, + jsonapi.QueryParamPageSize: i.PageSize, + jsonapi.QueryParamPageNumber: i.PageNumber, } for key, value := range pairings { @@ -69,7 +69,7 @@ func (c *Client) ListPrivateKeys(i *ListPrivateKeysInput) ([]*PrivateKey, error) filters := &RequestOptions{ Params: i.formatFilters(), Headers: map[string]string{ - "Accept": "application/vnd.api+json", // this is required otherwise the filters don't work + "Accept": jsonapi.MediaType, // this is required otherwise the filters don't work }, } diff --git a/fastly/tls_subscription.go b/fastly/tls_subscription.go index 0e8f358a..42645e95 100644 --- a/fastly/tls_subscription.go +++ b/fastly/tls_subscription.go @@ -90,13 +90,13 @@ type ListTLSSubscriptionsInput struct { func (s *ListTLSSubscriptionsInput) formatFilters() map[string]string { result := map[string]string{} pairings := map[string]any{ - "filter[has_active_order]": s.FilterActiveOrders, - "filter[state]": s.FilterState, - "filter[tls_domains.id]": s.FilterTLSDomainsID, - "include": s.Include, - "page[number]": s.PageNumber, - "page[size]": s.PageSize, - "sort": s.Sort, + "filter[has_active_order]": s.FilterActiveOrders, + "filter[state]": s.FilterState, + "filter[tls_domains.id]": s.FilterTLSDomainsID, + "include": s.Include, + jsonapi.QueryParamPageNumber: s.PageNumber, + jsonapi.QueryParamPageSize: s.PageSize, + "sort": s.Sort, } for key, v := range pairings { @@ -130,7 +130,7 @@ func (c *Client) ListTLSSubscriptions(i *ListTLSSubscriptionsInput) ([]*TLSSubsc resp, err := c.Get("/tls/subscriptions", &RequestOptions{ Params: i.formatFilters(), Headers: map[string]string{ - "Accept": "application/vnd.api+json", // Needed for "include" but seemingly not the other fields + "Accept": jsonapi.MediaType, // Needed for "include" but seemingly not the other fields }, }) if err != nil { @@ -225,7 +225,7 @@ func (c *Client) GetTLSSubscription(i *GetTLSSubscriptionInput) (*TLSSubscriptio requestOptions := &RequestOptions{ Headers: map[string]string{ - "Accept": "application/vnd.api+json", // this is required otherwise the params don't work + "Accept": jsonapi.MediaType, // this is required otherwise the params don't work }, } diff --git a/fastly/vcl_rate_limiter_test.go b/fastly/vcl_rate_limiter_test.go index 6584729f..66439692 100644 --- a/fastly/vcl_rate_limiter_test.go +++ b/fastly/vcl_rate_limiter_test.go @@ -32,7 +32,7 @@ func TestClient_ERL(t *testing.T) { PenaltyBoxDuration: ToPointer(30), Response: &ERLResponseType{ ERLStatus: ToPointer(429), - ERLContentType: ToPointer("application/json"), + ERLContentType: ToPointer(JSONMimeType), ERLContent: ToPointer("Too many requests"), }, RpsLimit: ToPointer(20), @@ -61,7 +61,7 @@ func TestClient_ERL(t *testing.T) { if *e.Response.ERLContent != "Too many requests" { t.Errorf("want 'Too many requests', got %q", *e.Response.ERLContent) } - if *e.Response.ERLContentType != "application/json" { + if *e.Response.ERLContentType != JSONMimeType { t.Errorf("want 'application/json', got %q", *e.Response.ERLContentType) } if *e.Response.ERLStatus != 429 { diff --git a/fastly/vcl_response_object_test.go b/fastly/vcl_response_object_test.go index d95bb23f..a9db5066 100644 --- a/fastly/vcl_response_object_test.go +++ b/fastly/vcl_response_object_test.go @@ -1,6 +1,7 @@ package fastly import ( + "net/http" "testing" ) @@ -20,7 +21,7 @@ func TestClient_ResponseObjects(t *testing.T) { ServiceID: TestDeliveryServiceID, ServiceVersion: *tv.Number, Name: ToPointer("test-response-object"), - Status: ToPointer(200), + Status: ToPointer(http.StatusOK), Response: ToPointer("Ok"), Content: ToPointer("abcd"), ContentType: ToPointer("text/plain"), @@ -50,7 +51,7 @@ func TestClient_ResponseObjects(t *testing.T) { if *ro.Name != "test-response-object" { t.Errorf("bad name: %q", *ro.Name) } - if *ro.Status != 200 { + if *ro.Status != http.StatusOK { t.Errorf("bad status: %q", *ro.Status) } if *ro.Response != "Ok" { diff --git a/fastly/waf.go b/fastly/waf.go index 664fd0c1..2ec8e8b1 100644 --- a/fastly/waf.go +++ b/fastly/waf.go @@ -63,8 +63,8 @@ type ListWAFsInput struct { func (i *ListWAFsInput) formatFilters() map[string]string { result := map[string]string{} pairings := map[string]any{ - "page[size]": i.PageSize, - "page[number]": i.PageNumber, + jsonapi.QueryParamPageSize: i.PageSize, + jsonapi.QueryParamPageNumber: i.PageNumber, "filter[service_id]": i.FilterService, "filter[service_version_number]": i.FilterVersion, "include": i.Include, diff --git a/fastly/waf_active_rule.go b/fastly/waf_active_rule.go index a31d62a4..c322d743 100644 --- a/fastly/waf_active_rule.go +++ b/fastly/waf_active_rule.go @@ -59,8 +59,8 @@ func (i *ListWAFActiveRulesInput) formatFilters() map[string]string { "filter[status]": i.FilterStatus, "filter[waf_rule_revision][message]": i.FilterMessage, "filter[waf_rule_revision][modsec_rule_id]": i.FilterModSedID, - "page[size]": i.PageSize, - "page[number]": i.PageNumber, + jsonapi.QueryParamPageSize: i.PageSize, + jsonapi.QueryParamPageNumber: i.PageNumber, "include": i.Include, } diff --git a/fastly/waf_rule_exclusion.go b/fastly/waf_rule_exclusion.go index 231509ca..66fe280f 100644 --- a/fastly/waf_rule_exclusion.go +++ b/fastly/waf_rule_exclusion.go @@ -117,8 +117,8 @@ func (i *ListWAFRuleExclusionsInput) formatFilters() map[string]string { "filter[exclusion_type]": i.FilterExclusionType, "filter[name]": i.FilterName, "filter[waf_rules.modsec_rule_id]": i.FilterModSedID, - "page[size]": i.PageSize, - "page[number]": i.PageNumber, + jsonapi.QueryParamPageSize: i.PageSize, + jsonapi.QueryParamPageNumber: i.PageNumber, "include": include, } diff --git a/fastly/waf_rules.go b/fastly/waf_rules.go index dc27d974..d3adfbb6 100644 --- a/fastly/waf_rules.go +++ b/fastly/waf_rules.go @@ -68,8 +68,8 @@ func (i *ListWAFRulesInput) formatFilters() map[string]string { "filter[publisher][in]": i.FilterPublishers, "filter[modsec_rule_id][in]": i.FilterModSecIDs, "filter[modsec_rule_id][not]": i.ExcludeModSecIDs, - "page[size]": i.PageSize, - "page[number]": i.PageNumber, + jsonapi.QueryParamPageSize: i.PageSize, + jsonapi.QueryParamPageNumber: i.PageNumber, "include": i.Include, } diff --git a/fastly/waf_rules_test.go b/fastly/waf_rules_test.go index d90174fb..8b049dc6 100644 --- a/fastly/waf_rules_test.go +++ b/fastly/waf_rules_test.go @@ -3,6 +3,8 @@ package fastly import ( "reflect" "testing" + + "github.com/google/jsonapi" ) func TestClient_WAF_Rules(t *testing.T) { @@ -108,8 +110,8 @@ func TestClient_listWAFRules_formatFilters(t *testing.T) { "filter[publisher][in]": "owasp,trustwave", "filter[modsec_rule_id][in]": "1010060,1010070", "filter[modsec_rule_id][not]": "123456,1234567", - "page[size]": "2", - "page[number]": "2", + jsonapi.QueryParamPageSize: "2", + jsonapi.QueryParamPageNumber: "2", "include": "included", }, }, diff --git a/fastly/waf_test.go b/fastly/waf_test.go index 6f25691e..7f82d76e 100644 --- a/fastly/waf_test.go +++ b/fastly/waf_test.go @@ -3,6 +3,8 @@ package fastly import ( "reflect" "testing" + + "github.com/google/jsonapi" ) func TestClient_WAFs(t *testing.T) { @@ -289,8 +291,8 @@ func TestClient_listWAFs_formatFilters(t *testing.T) { local: map[string]string{ "filter[service_id]": "service1", "filter[service_version_number]": "1", - "page[size]": "2", - "page[number]": "2", + jsonapi.QueryParamPageSize: "2", + jsonapi.QueryParamPageNumber: "2", "include": "included", }, }, diff --git a/fastly/waf_version.go b/fastly/waf_version.go index 82de1764..66937d41 100644 --- a/fastly/waf_version.go +++ b/fastly/waf_version.go @@ -105,9 +105,9 @@ type ListWAFVersionsInput struct { func (i *ListWAFVersionsInput) formatFilters() map[string]string { result := map[string]string{} pairings := map[string]any{ - "page[size]": i.PageSize, - "page[number]": i.PageNumber, - "include": i.Include, + jsonapi.QueryParamPageSize: i.PageSize, + jsonapi.QueryParamPageNumber: i.PageNumber, + "include": i.Include, } for key, value := range pairings { diff --git a/fastly/waf_version_test.go b/fastly/waf_version_test.go index af718f7d..1e1606f3 100644 --- a/fastly/waf_version_test.go +++ b/fastly/waf_version_test.go @@ -5,6 +5,8 @@ import ( "reflect" "testing" "time" + + "github.com/google/jsonapi" ) func TestClient_WAF_Versions(t *testing.T) { @@ -367,9 +369,9 @@ func TestClient_listWAFVersions_formatFilters(t *testing.T) { Include: "included", }, local: map[string]string{ - "page[size]": "2", - "page[number]": "2", - "include": "included", + jsonapi.QueryParamPageSize: "2", + jsonapi.QueryParamPageNumber: "2", + "include": "included", }, }, } diff --git a/go.mod b/go.mod index 8846fb02..c5ce7f8a 100644 --- a/go.mod +++ b/go.mod @@ -2,27 +2,30 @@ module github.com/fastly/go-fastly/v9 require ( github.com/dnaeon/go-vcr v1.2.0 - github.com/google/go-cmp v0.5.8 + github.com/google/go-cmp v0.6.0 github.com/google/go-querystring v1.1.0 github.com/google/jsonapi v1.0.0 github.com/hashicorp/go-cleanhttp v0.5.2 - github.com/mitchellh/mapstructure v1.4.3 - github.com/peterhellberg/link v1.1.0 + github.com/mitchellh/mapstructure v1.5.0 + github.com/peterhellberg/link v1.2.0 github.com/stretchr/testify v1.10.0 - golang.org/x/crypto v0.31.0 - golang.org/x/tools v0.15.0 - honnef.co/go/tools v0.3.3 + golang.org/x/crypto v0.32.0 + golang.org/x/tools v0.29.0 + honnef.co/go/tools v0.5.1 ) require ( - github.com/BurntSushi/toml v0.4.1 // indirect + github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e // indirect - golang.org/x/mod v0.14.0 // indirect - golang.org/x/sys v0.28.0 // indirect - gopkg.in/yaml.v2 v2.2.8 // indirect + golang.org/x/exp/typeparams v0.0.0-20250106191152-7588d65b2ba8 // indirect + golang.org/x/mod v0.22.0 // indirect + golang.org/x/sync v0.10.0 // indirect + golang.org/x/sys v0.29.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) -go 1.20 +go 1.22.1 + +toolchain go1.23.2 diff --git a/go.sum b/go.sum index 763c77f2..547d7a9f 100644 --- a/go.sum +++ b/go.sum @@ -1,44 +1,46 @@ -github.com/BurntSushi/toml v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw= -github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs= +github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI= github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= -github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/jsonapi v1.0.0 h1:qIGgO5Smu3yJmSs+QlvhQnrscdZfFhiV6S8ryJAglqU= github.com/google/jsonapi v1.0.0/go.mod h1:YYHiRPJT8ARXGER8In9VuLv4qvLfDmA9ULQqptbLE4s= github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= -github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs= -github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8= -github.com/peterhellberg/link v1.1.0 h1:s2+RH8EGuI/mI4QwrWGSYQCRz7uNgip9BaM04HKu5kc= -github.com/peterhellberg/link v1.1.0/go.mod h1:gtSlOT4jmkY8P47hbTc8PTgiDDWpdPbFYl75keYyBB8= -golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= -golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= +github.com/peterhellberg/link v1.2.0 h1:UA5pg3Gp/E0F2WdX7GERiNrPQrM1K6CVJUUWfHa4t6c= +github.com/peterhellberg/link v1.2.0/go.mod h1:gYfAh+oJgQu2SrZHg5hROVRQe1ICoK0/HHJTcE0edxc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e h1:qyrTQ++p1afMkO4DPEeLGq/3oTsdlvdH4vqZUBWzUKM= -golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= -golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= -golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= -golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= -golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8= -golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk= +golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc= +golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= +golang.org/x/exp/typeparams v0.0.0-20250106191152-7588d65b2ba8 h1:EN2027N+i+Tda4MiYFv7Mb0WAxf/gfHdpF2QAtplC9s= +golang.org/x/exp/typeparams v0.0.0-20250106191152-7588d65b2ba8/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= +golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= +golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= +golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/tools v0.29.0 h1:Xx0h3TtM9rzQpQuR4dKLrdglAmCEN5Oi+P74JdhdzXE= +golang.org/x/tools v0.29.0/go.mod h1:KMQVMRsVxU6nHCFXrBPhDB8XncLNLM0lIy/F14RP588= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -honnef.co/go/tools v0.3.3 h1:oDx7VAwstgpYpb3wv0oxiZlxY+foCpRAwY7Vk6XpAgA= -honnef.co/go/tools v0.3.3/go.mod h1:jzwdWgg7Jdq75wlfblQxO4neNaFFSvgc1tD5Wv8U0Yw= +honnef.co/go/tools v0.5.1 h1:4bH5o3b5ZULQ4UrBmP+63W9r7qIkqJClEA9ko5YKx+I= +honnef.co/go/tools v0.5.1/go.mod h1:e9irvo83WDG9/irijV44wr3tbhcFeRnfpVlRqVwpzMs= diff --git a/internal/test_utils/functional.go b/internal/test_utils/functional.go index d42cc4a9..05815e31 100644 --- a/internal/test_utils/functional.go +++ b/internal/test_utils/functional.go @@ -19,10 +19,10 @@ type FunctionalTest struct { // WantNoError indicates that the test case should fail if any // error is returned by TestFn WantNoError bool - // WantError indicates the error (value, not type) that must - // be returned by TestFn for this test case + // WantError indicates the error (value, not type) that TestFn must + // return for this test case WantError error - // CheckErrorFn is a function which can be used to perform + // CheckErrorFn is a function that can be used to perform // additional validation of the error returned by TestFn; the // function will be passed a formatted string containing the // test case's name, along with the returned error