Skip to content

Commit 0005519

Browse files
chemidyhiranya911
authored andcommitted
use const for Common HTTP methods and HTTP status codes (#58)
* use const for Common HTTP methods and HTTP status codes * fix format verbs %q to %d
1 parent ec2cae9 commit 0005519

File tree

8 files changed

+41
-40
lines changed

8 files changed

+41
-40
lines changed

auth/auth.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/pem"
2323
"errors"
2424
"fmt"
25+
"net/http"
2526
"strings"
2627

2728
"firebase.google.com/go/internal"
@@ -131,7 +132,7 @@ func NewClient(ctx context.Context, c *internal.AuthConfig) (*Client, error) {
131132
func (c *Client) makeHTTPCall(ctx context.Context, serviceName string, payload interface{}, result interface{}) error {
132133
versionHeader := internal.WithHeader("X-Client-Version", c.version)
133134
request := &internal.Request{
134-
Method: "POST",
135+
Method: http.MethodPost,
135136
URL: c.url + serviceName,
136137
Body: internal.NewJSONEntity(payload),
137138
Opts: []internal.HTTPOption{versionHeader},
@@ -140,7 +141,7 @@ func (c *Client) makeHTTPCall(ctx context.Context, serviceName string, payload i
140141
if err != nil {
141142
return err
142143
}
143-
return resp.Unmarshal(200, result)
144+
return resp.Unmarshal(http.StatusOK, result)
144145
}
145146

146147
// CustomToken creates a signed custom authentication token with the specified user ID. The resulting

auth/crypto_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func newTestHTTPClient(data []byte) (*http.Client, *mockReadCloser) {
4848
Transport: &mockHTTPResponse{
4949
Response: http.Response{
5050
Status: "200 OK",
51-
StatusCode: 200,
51+
StatusCode: http.StatusOK,
5252
Header: http.Header{
5353
"Cache-Control": {"public, max-age=100"},
5454
},

auth/user_mgt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ func TestMakeExportedUser(t *testing.T) {
626626
func TestHTTPError(t *testing.T) {
627627
s := echoServer([]byte(`{"error":"test"}`), t)
628628
defer s.Close()
629-
s.Status = 500
629+
s.Status = http.StatusInternalServerError
630630

631631
u, err := s.Client.GetUser(context.Background(), "some uid")
632632
if u != nil || err == nil {

firebase_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ func TestClientOptions(t *testing.T) {
8585
if err != nil {
8686
t.Fatal(err)
8787
}
88-
if resp.StatusCode != 200 {
89-
t.Errorf("Status: %d; want: 200", resp.StatusCode)
88+
if resp.StatusCode != http.StatusOK {
89+
t.Errorf("Status: %d; want: %d", resp.StatusCode, http.StatusOK)
9090
}
9191
if bearer != "Bearer mock-token" {
9292
t.Errorf("Bearer token: %q; want: %q", bearer, "Bearer mock-token")
@@ -318,8 +318,8 @@ func TestCustomTokenSource(t *testing.T) {
318318
if err != nil {
319319
t.Fatal(err)
320320
}
321-
if resp.StatusCode != 200 {
322-
t.Errorf("Status: %d; want: 200", resp.StatusCode)
321+
if resp.StatusCode != http.StatusOK {
322+
t.Errorf("Status: %d; want: %d", resp.StatusCode, http.StatusOK)
323323
}
324324
if bearer != "Bearer "+ts.AccessToken {
325325
t.Errorf("Bearer token: %q; want: %q", bearer, "Bearer "+ts.AccessToken)

iid/iid.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ import (
3030
const iidEndpoint = "https://console.firebase.google.com/v1"
3131

3232
var errorCodes = map[int]string{
33-
400: "malformed instance id argument",
34-
401: "request not authorized",
35-
403: "project does not match instance ID or the client does not have sufficient privileges",
36-
404: "failed to find the instance id",
37-
409: "already deleted",
38-
429: "request throttled out by the backend server",
39-
500: "internal server error",
40-
503: "backend servers are over capacity",
33+
http.StatusBadRequest: "malformed instance id argument",
34+
http.StatusUnauthorized: "request not authorized",
35+
http.StatusForbidden: "project does not match instance ID or the client does not have sufficient privileges",
36+
http.StatusNotFound: "failed to find the instance id",
37+
http.StatusConflict: "already deleted",
38+
http.StatusTooManyRequests: "request throttled out by the backend server",
39+
http.StatusInternalServerError: "internal server error",
40+
http.StatusServiceUnavailable: "backend servers are over capacity",
4141
}
4242

4343
// Client is the interface for the Firebase Instance ID service.
@@ -79,7 +79,7 @@ func (c *Client) DeleteInstanceID(ctx context.Context, iid string) error {
7979
}
8080

8181
url := fmt.Sprintf("%s/project/%s/instanceId/%s", c.endpoint, c.project, iid)
82-
resp, err := c.client.Do(ctx, &internal.Request{Method: "DELETE", URL: url})
82+
resp, err := c.client.Do(ctx, &internal.Request{Method: http.MethodDelete, URL: url})
8383
if err != nil {
8484
return err
8585
}

iid/iid_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func TestDeleteInstanceID(t *testing.T) {
7575
if tr == nil {
7676
t.Fatalf("Request = nil; want non-nil")
7777
}
78-
if tr.Method != "DELETE" {
79-
t.Errorf("Method = %q; want = %q", tr.Method, "DELETE")
78+
if tr.Method != http.MethodDelete {
79+
t.Errorf("Method = %q; want = %q", tr.Method, http.MethodDelete)
8080
}
8181
if tr.URL.Path != "/project/test-project/instanceId/test-iid" {
8282
t.Errorf("Path = %q; want = %q", tr.URL.Path, "/project/test-project/instanceId/test-iid")
@@ -87,7 +87,7 @@ func TestDeleteInstanceID(t *testing.T) {
8787
}
8888

8989
func TestDeleteInstanceIDError(t *testing.T) {
90-
status := 200
90+
status := http.StatusOK
9191
var tr *http.Request
9292
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
9393
tr = r
@@ -119,8 +119,8 @@ func TestDeleteInstanceIDError(t *testing.T) {
119119
if tr == nil {
120120
t.Fatalf("Request = nil; want non-nil")
121121
}
122-
if tr.Method != "DELETE" {
123-
t.Errorf("Method = %q; want = %q", tr.Method, "DELETE")
122+
if tr.Method != http.MethodDelete {
123+
t.Errorf("Method = %q; want = %q", tr.Method, http.MethodDelete)
124124
}
125125
if tr.URL.Path != "/project/test-project/instanceId/test-iid" {
126126
t.Errorf("Path = %q; want = %q", tr.URL.Path, "/project/test-project/instanceId/test-iid")
@@ -162,8 +162,8 @@ func TestDeleteInstanceIDUnexpectedError(t *testing.T) {
162162
if tr == nil {
163163
t.Fatalf("Request = nil; want non-nil")
164164
}
165-
if tr.Method != "DELETE" {
166-
t.Errorf("Method = %q; want = %q", tr.Method, "DELETE")
165+
if tr.Method != http.MethodDelete {
166+
t.Errorf("Method = %q; want = %q", tr.Method, http.MethodDelete)
167167
}
168168
if tr.URL.Path != "/project/test-project/instanceId/test-iid" {
169169
t.Errorf("Path = %q; want = %q", tr.URL.Path, "/project/test-project/instanceId/test-iid")

integration/auth/auth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func postRequest(url string, req []byte) ([]byte, error) {
139139
}
140140

141141
defer resp.Body.Close()
142-
if resp.StatusCode != 200 {
142+
if resp.StatusCode != http.StatusOK {
143143
return nil, fmt.Errorf("unexpected http status code: %d", resp.StatusCode)
144144
}
145145
return ioutil.ReadAll(resp.Body)

internal/http_client_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,61 +33,61 @@ var cases = []struct {
3333
}{
3434
{
3535
req: &Request{
36-
Method: "GET",
36+
Method: http.MethodGet,
3737
},
38-
method: "GET",
38+
method: http.MethodGet,
3939
},
4040
{
4141
req: &Request{
42-
Method: "GET",
42+
Method: http.MethodGet,
4343
Opts: []HTTPOption{
4444
WithHeader("Test-Header", "value1"),
4545
WithQueryParam("testParam", "value2"),
4646
},
4747
},
48-
method: "GET",
48+
method: http.MethodGet,
4949
headers: map[string]string{"Test-Header": "value1"},
5050
query: map[string]string{"testParam": "value2"},
5151
},
5252
{
5353
req: &Request{
54-
Method: "POST",
54+
Method: http.MethodPost,
5555
Body: NewJSONEntity(map[string]string{"foo": "bar"}),
5656
Opts: []HTTPOption{
5757
WithHeader("Test-Header", "value1"),
5858
WithQueryParam("testParam1", "value2"),
5959
WithQueryParam("testParam2", "value3"),
6060
},
6161
},
62-
method: "POST",
62+
method: http.MethodPost,
6363
body: "{\"foo\":\"bar\"}",
6464
headers: map[string]string{"Test-Header": "value1"},
6565
query: map[string]string{"testParam1": "value2", "testParam2": "value3"},
6666
},
6767
{
6868
req: &Request{
69-
Method: "POST",
69+
Method: http.MethodPost,
7070
Body: NewJSONEntity("body"),
7171
Opts: []HTTPOption{
7272
WithHeader("Test-Header", "value1"),
7373
WithQueryParams(map[string]string{"testParam1": "value2", "testParam2": "value3"}),
7474
},
7575
},
76-
method: "POST",
76+
method: http.MethodPost,
7777
body: "\"body\"",
7878
headers: map[string]string{"Test-Header": "value1"},
7979
query: map[string]string{"testParam1": "value2", "testParam2": "value3"},
8080
},
8181
{
8282
req: &Request{
83-
Method: "PUT",
83+
Method: http.MethodPut,
8484
Body: NewJSONEntity(nil),
8585
Opts: []HTTPOption{
8686
WithHeader("Test-Header", "value1"),
8787
WithQueryParams(map[string]string{"testParam1": "value2", "testParam2": "value3"}),
8888
},
8989
},
90-
method: "PUT",
90+
method: http.MethodPut,
9191
body: "null",
9292
headers: map[string]string{"Test-Header": "value1"},
9393
query: map[string]string{"testParam1": "value2", "testParam2": "value3"},
@@ -184,7 +184,7 @@ func TestContext(t *testing.T) {
184184
client := &HTTPClient{Client: http.DefaultClient}
185185
ctx, cancel := context.WithCancel(context.Background())
186186
resp, err := client.Do(ctx, &Request{
187-
Method: "GET",
187+
Method: http.MethodGet,
188188
URL: server.URL,
189189
})
190190
if err != nil {
@@ -196,7 +196,7 @@ func TestContext(t *testing.T) {
196196

197197
cancel()
198198
resp, err = client.Do(ctx, &Request{
199-
Method: "GET",
199+
Method: http.MethodGet,
200200
URL: server.URL,
201201
})
202202
if resp != nil || err == nil {
@@ -234,7 +234,7 @@ func TestErrorParser(t *testing.T) {
234234
Client: http.DefaultClient,
235235
ErrParser: ep,
236236
}
237-
req := &Request{Method: "GET", URL: server.URL}
237+
req := &Request{Method: http.MethodGet, URL: server.URL}
238238
resp, err := client.Do(context.Background(), req)
239239
if err != nil {
240240
t.Fatal(err)
@@ -255,7 +255,7 @@ func TestErrorParser(t *testing.T) {
255255

256256
func TestInvalidURL(t *testing.T) {
257257
req := &Request{
258-
Method: "GET",
258+
Method: http.MethodGet,
259259
URL: "http://localhost:250/mock.url",
260260
}
261261
client := &HTTPClient{Client: http.DefaultClient}
@@ -281,7 +281,7 @@ func TestUnmarshalError(t *testing.T) {
281281
server := httptest.NewServer(handler)
282282
defer server.Close()
283283

284-
req := &Request{Method: "GET", URL: server.URL}
284+
req := &Request{Method: http.MethodGet, URL: server.URL}
285285
client := &HTTPClient{Client: http.DefaultClient}
286286
resp, err := client.Do(context.Background(), req)
287287
if err != nil {

0 commit comments

Comments
 (0)