Skip to content

Commit 037c263

Browse files
committed
replace hard coded http method names
1 parent ebab0cc commit 037c263

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ If the request could be made but the response status code was not `2xx` an error
88

99
## Example
1010
```go
11-
import "github.com/fastbill/go-request/v2"
11+
import (
12+
"net/http"
13+
"github.com/fastbill/go-request/v2"
14+
)
1215

1316
type Input struct {
1417
RequestValue string `json:"requestValue"`
@@ -20,7 +23,7 @@ type Output struct {
2023

2124
params := request.Params{
2225
URL: "https://example.com",
23-
Method: "POST",
26+
Method: http.MethodPost,
2427
Headers: map[string]string{"my-header":"value", "another-header":"value2"},
2528
Body: Input{RequestValue: "someValueIn"},
2629
Query: map[string]string{"key": "value"},
@@ -80,7 +83,7 @@ if err != nil {
8083
return err
8184
}
8285

83-
req, err := http.NewRequest("POST", url, buf)
86+
req, err := http.NewRequest(http.MethodPost, url, buf)
8487
if err != nil {
8588
return err
8689
}

request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ func createRequest(params Params) (*http.Request, error) {
144144

145145
// Get is a convience wrapper for "Do" to execute GET requests
146146
func Get(url string, responseBody interface{}) error {
147-
return Do(Params{Method: "GET", URL: url}, responseBody)
147+
return Do(Params{Method: http.MethodGet, URL: url}, responseBody)
148148
}
149149

150150
// Post is a convience wrapper for "Do" to execute POST requests
151151
func Post(url string, requestBody interface{}, responseBody interface{}) error {
152-
return Do(Params{Method: "POST", URL: url, Body: requestBody}, responseBody)
152+
return Do(Params{Method: http.MethodPost, URL: url, Body: requestBody}, responseBody)
153153
}
154154

155155
func convertToReader(body interface{}) (io.Reader, error) {

request_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestDoSuccessful(t *testing.T) {
6262
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6363
body, _ := ioutil.ReadAll(r.Body)
6464
assert.Equal(t, `{"requestValue":"someValueIn"}`+"\n", string(body))
65-
assert.Equal(t, r.Method, "POST")
65+
assert.Equal(t, r.Method, http.MethodPost)
6666
w.WriteHeader(http.StatusCreated)
6767
_, err := w.Write([]byte(`{"responseValue":"someValueOut"}`))
6868
assert.NoError(t, err)
@@ -71,7 +71,7 @@ func TestDoSuccessful(t *testing.T) {
7171

7272
params := Params{
7373
URL: ts.URL,
74-
Method: "POST",
74+
Method: http.MethodPost,
7575
Body: Input{RequestValue: "someValueIn"},
7676
ExpectedResponseCode: http.StatusCreated,
7777
}
@@ -91,7 +91,7 @@ func TestDoSuccessful(t *testing.T) {
9191

9292
params := Params{
9393
URL: ts.URL + "?beenhere=before",
94-
Method: "POST",
94+
Method: http.MethodPost,
9595
Query: map[string]string{
9696
"testKey": "testValue",
9797
"öä": "%&/",
@@ -104,15 +104,15 @@ func TestDoSuccessful(t *testing.T) {
104104

105105
t.Run("no request body", func(t *testing.T) {
106106
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
107-
assert.Equal(t, r.Method, "POST")
107+
assert.Equal(t, r.Method, http.MethodPost)
108108
_, err := w.Write([]byte(`{"responseValue":"someValueOut"}`))
109109
assert.NoError(t, err)
110110
}))
111111
defer ts.Close()
112112

113113
params := Params{
114114
URL: ts.URL,
115-
Method: "POST",
115+
Method: http.MethodPost,
116116
}
117117

118118
result := &Output{}
@@ -123,14 +123,14 @@ func TestDoSuccessful(t *testing.T) {
123123

124124
t.Run("no response body and no request body", func(t *testing.T) {
125125
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
126-
assert.Equal(t, r.Method, "GET")
126+
assert.Equal(t, r.Method, http.MethodGet)
127127
w.WriteHeader(http.StatusOK)
128128
}))
129129
defer ts.Close()
130130

131131
params := Params{
132132
URL: ts.URL,
133-
Method: "GET",
133+
Method: http.MethodGet,
134134
Body: nil,
135135
}
136136

@@ -147,7 +147,7 @@ func TestDoSuccessful(t *testing.T) {
147147

148148
params := Params{
149149
URL: ts.URL,
150-
Method: "GET",
150+
Method: http.MethodGet,
151151
Body: nil,
152152
Timeout: 1 * time.Millisecond,
153153
}
@@ -162,15 +162,15 @@ func TestDoSuccessful(t *testing.T) {
162162
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
163163
body, _ := ioutil.ReadAll(r.Body)
164164
assert.Equal(t, `{"requestValue":"someValueIn"}`, string(body))
165-
assert.Equal(t, r.Method, "POST")
165+
assert.Equal(t, r.Method, http.MethodPost)
166166
_, err := w.Write([]byte(`{"responseValue":"someValueOut"}`))
167167
assert.NoError(t, err)
168168
}))
169169
defer ts.Close()
170170

171171
params := Params{
172172
URL: ts.URL,
173-
Method: "POST",
173+
Method: http.MethodPost,
174174
Body: strings.NewReader(`{"requestValue":"someValueIn"}`),
175175
}
176176

@@ -224,7 +224,7 @@ func TestDoHTTPErrors(t *testing.T) {
224224

225225
params := Params{
226226
URL: ts.URL,
227-
Method: "GET",
227+
Method: http.MethodGet,
228228
}
229229

230230
err := Do(params, nil)
@@ -245,7 +245,7 @@ func TestDoHTTPErrors(t *testing.T) {
245245

246246
params := Params{
247247
URL: ts.URL,
248-
Method: "GET",
248+
Method: http.MethodGet,
249249
}
250250

251251
err := Do(params, nil)
@@ -320,7 +320,7 @@ func TestDoWithStringResponse(t *testing.T) {
320320

321321
func TestGet(t *testing.T) {
322322
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
323-
assert.Equal(t, r.Method, "GET")
323+
assert.Equal(t, r.Method, http.MethodGet)
324324
_, err := w.Write([]byte(`{"responseValue":"someValueOut"}`))
325325
assert.NoError(t, err)
326326
}))
@@ -336,7 +336,7 @@ func TestPost(t *testing.T) {
336336
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
337337
body, _ := ioutil.ReadAll(r.Body)
338338
assert.Equal(t, `{"requestValue":"someValueIn"}`+"\n", string(body))
339-
assert.Equal(t, r.Method, "POST")
339+
assert.Equal(t, r.Method, http.MethodPost)
340340
_, err := w.Write([]byte(`{"responseValue":"someValueOut"}`))
341341
assert.NoError(t, err)
342342
}))
@@ -361,7 +361,7 @@ func ExampleDo() {
361361

362362
params := Params{
363363
URL: ts.URL,
364-
Method: "POST",
364+
Method: http.MethodPost,
365365
Body: Input{RequestValue: "someValueIn"},
366366
}
367367

@@ -386,7 +386,7 @@ func ExampleDoWithStringResponse() {
386386

387387
params := Params{
388388
URL: ts.URL,
389-
Method: "POST",
389+
Method: http.MethodPost,
390390
}
391391

392392
result, err := DoWithStringResponse(params)

0 commit comments

Comments
 (0)