Skip to content

Commit

Permalink
replaced hardcoded strings with http constants (#1376)
Browse files Browse the repository at this point in the history
Co-authored-by: Sahib Yar <s.yar@centroidsol.com>
Co-authored-by: Kevin P. Fleming <kpfleming@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 23, 2025
1 parent e52205e commit 92feb03
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 18 deletions.
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

0 comments on commit 92feb03

Please sign in to comment.