Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use constants from net/http for HTTP methods instead of hardcoded strings. #1376

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/api/undocumented/undocumented.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func Call(opts CallOptions) (data []byte, err error) {
return []byte{}, NewError(err, res.StatusCode)
}

if res.StatusCode >= 400 {
if res.StatusCode >= http.StatusBadRequest {
return data, NewError(fmt.Errorf("error response: %q", data), res.StatusCode)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (s Server) GetJWT(authorizationCode string) (JWT, error) {
"http://localhost:8080/callback", // NOTE: not redirected to, just a security check.
)

req, err := http.NewRequest("POST", s.WellKnownEndpoints.Token, strings.NewReader(payload))
req, err := http.NewRequest(http.MethodPost, s.WellKnownEndpoints.Token, strings.NewReader(payload))
if err != nil {
return JWT{}, err
}
Expand Down Expand Up @@ -352,7 +352,7 @@ func (s *Server) RefreshAccessToken(refreshToken string) (JWT, error) {
refreshToken,
)

req, err := http.NewRequest("POST", s.WellKnownEndpoints.Token, strings.NewReader(payload))
req, err := http.NewRequest(http.MethodPost, s.WellKnownEndpoints.Token, strings.NewReader(payload))
if err != nil {
return JWT{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/compute/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ func generateTimeout(d time.Duration) string {
// whatever the user defined with --status-check-code), which should help
// signify if the service is generally available.
func pingServiceURL(serviceURL string, httpClient api.HTTPClient, expectedStatusCode int, debugMode bool) (ok bool, status int, err error) {
req, err := http.NewRequest("GET", serviceURL, nil)
req, err := http.NewRequest(http.MethodGet, serviceURL, nil)
if err != nil {
return false, 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/compute/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ func (c *InitCommand) FetchPackageTemplate(branch, tag string, archives []file.A
return fmt.Errorf("--from url seems invalid: %s", c.CloneFrom)
}

req, err := http.NewRequest("GET", u.String(), nil)
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
err = fmt.Errorf("failed to construct package request URL: %w", err)
c.Globals.ErrLog.Add(err)
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/healthcheck/healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"io"
"net/http"
"strings"
"testing"

Expand Down Expand Up @@ -307,7 +308,7 @@ func listHealthChecksOK(i *fastly.ListHealthChecksInput) ([]*fastly.HealthCheck,
ServiceVersion: fastly.ToPointer(i.ServiceVersion),
Name: fastly.ToPointer("test"),
Comment: fastly.ToPointer("test"),
Method: fastly.ToPointer("HEAD"),
Method: fastly.ToPointer(http.MethodHead),
Host: fastly.ToPointer("www.test.com"),
Path: fastly.ToPointer("/health"),
},
Expand All @@ -316,7 +317,7 @@ func listHealthChecksOK(i *fastly.ListHealthChecksInput) ([]*fastly.HealthCheck,
ServiceVersion: fastly.ToPointer(i.ServiceVersion),
Name: fastly.ToPointer("example"),
Comment: fastly.ToPointer("example"),
Method: fastly.ToPointer("HEAD"),
Method: fastly.ToPointer(http.MethodHead),
Host: fastly.ToPointer("www.example.com"),
Path: fastly.ToPointer("/health"),
},
Expand Down Expand Up @@ -373,7 +374,7 @@ func getHealthCheckOK(i *fastly.GetHealthCheckInput) (*fastly.HealthCheck, error
ServiceID: fastly.ToPointer(i.ServiceID),
ServiceVersion: fastly.ToPointer(i.ServiceVersion),
Name: fastly.ToPointer("test"),
Method: fastly.ToPointer("HEAD"),
Method: fastly.ToPointer(http.MethodHead),
Host: fastly.ToPointer("www.test.com"),
Path: fastly.ToPointer("/healthcheck"),
Comment: fastly.ToPointer("test"),
Expand Down
11 changes: 6 additions & 5 deletions pkg/commands/logging/https/https_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"io"
"net/http"
"strings"
"testing"

Expand Down Expand Up @@ -285,7 +286,7 @@ func createHTTPSOK(i *fastly.CreateHTTPSInput) (*fastly.HTTPS, error) {
ContentType: fastly.ToPointer("application/json"),
HeaderName: fastly.ToPointer("name"),
HeaderValue: fastly.ToPointer("value"),
Method: fastly.ToPointer("GET"),
Method: fastly.ToPointer(http.MethodGet),
JSONFormat: fastly.ToPointer("1"),
Placement: fastly.ToPointer("none"),
TLSCACert: fastly.ToPointer("-----BEGIN CERTIFICATE-----foo"),
Expand Down Expand Up @@ -315,7 +316,7 @@ func listHTTPSsOK(i *fastly.ListHTTPSInput) ([]*fastly.HTTPS, error) {
ContentType: fastly.ToPointer("application/json"),
HeaderName: fastly.ToPointer("name"),
HeaderValue: fastly.ToPointer("value"),
Method: fastly.ToPointer("GET"),
Method: fastly.ToPointer(http.MethodGet),
JSONFormat: fastly.ToPointer("1"),
Placement: fastly.ToPointer("none"),
TLSCACert: fastly.ToPointer("-----BEGIN CERTIFICATE-----foo"),
Expand All @@ -337,7 +338,7 @@ func listHTTPSsOK(i *fastly.ListHTTPSInput) ([]*fastly.HTTPS, error) {
ContentType: fastly.ToPointer("application/json"),
HeaderName: fastly.ToPointer("name"),
HeaderValue: fastly.ToPointer("value"),
Method: fastly.ToPointer("GET"),
Method: fastly.ToPointer(http.MethodGet),
JSONFormat: fastly.ToPointer("1"),
Placement: fastly.ToPointer("none"),
TLSCACert: fastly.ToPointer("-----BEGIN CERTIFICATE-----foo"),
Expand Down Expand Up @@ -424,7 +425,7 @@ func getHTTPSOK(i *fastly.GetHTTPSInput) (*fastly.HTTPS, error) {
ContentType: fastly.ToPointer("application/json"),
HeaderName: fastly.ToPointer("name"),
HeaderValue: fastly.ToPointer("value"),
Method: fastly.ToPointer("GET"),
Method: fastly.ToPointer(http.MethodGet),
JSONFormat: fastly.ToPointer("1"),
Placement: fastly.ToPointer("none"),
TLSCACert: fastly.ToPointer("-----BEGIN CERTIFICATE-----foo"),
Expand Down Expand Up @@ -476,7 +477,7 @@ func updateHTTPSOK(i *fastly.UpdateHTTPSInput) (*fastly.HTTPS, error) {
ContentType: fastly.ToPointer("application/json"),
HeaderName: fastly.ToPointer("name"),
HeaderValue: fastly.ToPointer("value"),
Method: fastly.ToPointer("GET"),
Method: fastly.ToPointer(http.MethodGet),
JSONFormat: fastly.ToPointer("1"),
Placement: fastly.ToPointer("none"),
TLSCACert: fastly.ToPointer("-----BEGIN CERTIFICATE-----foo"),
Expand Down
5 changes: 3 additions & 2 deletions pkg/commands/logging/https/https_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package https_test

import (
"bytes"
"net/http"
"testing"

"github.com/fastly/go-fastly/v9/fastly"
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestCreateHTTPSInput(t *testing.T) {
ContentType: fastly.ToPointer("application/json"),
HeaderName: fastly.ToPointer("name"),
HeaderValue: fastly.ToPointer("value"),
Method: fastly.ToPointer("GET"),
Method: fastly.ToPointer(http.MethodGet),
JSONFormat: fastly.ToPointer("1"),
Placement: fastly.ToPointer("none"),
TLSCACert: fastly.ToPointer("-----BEGIN CERTIFICATE-----foo"),
Expand Down Expand Up @@ -271,7 +272,7 @@ func createCommandAll() *https.CreateCommand {
ContentType: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "application/json"},
HeaderName: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "name"},
HeaderValue: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "value"},
Method: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "GET"},
Method: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: http.MethodGet},
JSONFormat: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "1"},
MessageType: argparser.OptionalString{Optional: argparser.Optional{WasSet: true}, Value: "classic"},
RequestMaxEntries: argparser.OptionalInt{Optional: argparser.Optional{WasSet: true}, Value: 2},
Expand Down
6 changes: 3 additions & 3 deletions pkg/commands/logtail/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ func (c *RootCommand) tail(out io.Writer) error {
break
}

req, err := http.NewRequest("GET", path, nil)
req, err := http.NewRequest(http.MethodGet, path, nil)
if err != nil {
c.Globals.ErrLog.AddWithContext(err, map[string]any{
"GET": path,
http.MethodGet: path,
})
return fmt.Errorf("unable to create new request: %w", err)
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func (c *RootCommand) tail(out io.Writer) error {

// Try the response again after a 1 second wait.
if resp.StatusCode/100 == 5 && resp.StatusCode != 501 ||
resp.StatusCode == 429 {
resp.StatusCode == http.StatusTooManyRequests {
time.Sleep(1 * time.Second)
continue
}
Expand Down
Loading