Skip to content

Commit c43cb51

Browse files
committed
ci: switch to go1.19, golangci-lint v1.49.0, go-testdeep v1.12.0
Signed-off-by: Maxime Soulé <[email protected]>
1 parent fbf2267 commit c43cb51

File tree

10 files changed

+469
-410
lines changed

10 files changed

+469
-410
lines changed

.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ jobs:
1010
test:
1111
strategy:
1212
matrix:
13-
go-version: [1.9.x, 1.10.x, 1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, tip]
13+
go-version: [1.9.x, 1.10.x, 1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x, 1.16.x, 1.17.x, 1.18.x, tip]
1414
full-tests: [false]
1515
include:
16-
- go-version: 1.18.x
16+
- go-version: 1.19.x
1717
full-tests: true
1818

1919
runs-on: ubuntu-latest
@@ -31,7 +31,7 @@ jobs:
3131
if: matrix.full-tests
3232
run: |
3333
curl -sL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh |
34-
sh -s -- -b $HOME/go/bin v1.45.2
34+
sh -s -- -b $HOME/go/bin v1.49.0
3535
$HOME/go/bin/golangci-lint run --max-issues-per-linter 0 \
3636
--max-same-issues 0 \
3737
-E bidichk \

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Easy mocking of http responses from external resources.
44

55
## Install
66

7-
Currently supports Go 1.9 - 1.18.
7+
Currently supports Go 1.9 - 1.19.
88

99
`v1` branch has to be used instead of `master`.
1010

doc.go

+70-68
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,82 @@
22
Package httpmock provides tools for mocking HTTP responses.
33
44
Simple Example:
5-
func TestFetchArticles(t *testing.T) {
6-
httpmock.Activate()
7-
defer httpmock.DeactivateAndReset()
85
9-
// Exact URL match
10-
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
11-
httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))
6+
func TestFetchArticles(t *testing.T) {
7+
httpmock.Activate()
8+
defer httpmock.DeactivateAndReset()
129
13-
// Regexp match (could use httpmock.RegisterRegexpResponder instead)
14-
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/\d+\z`,
15-
httpmock.NewStringResponder(200, `{"id": 1, "name": "My Great Article"}`))
10+
// Exact URL match
11+
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
12+
httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))
1613
17-
// do stuff that makes a request to articles
14+
// Regexp match (could use httpmock.RegisterRegexpResponder instead)
15+
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/\d+\z`,
16+
httpmock.NewStringResponder(200, `{"id": 1, "name": "My Great Article"}`))
1817
19-
// get count info
20-
httpmock.GetTotalCallCount()
18+
// do stuff that makes a request to articles
2119
22-
// get the amount of calls for the registered responder
23-
info := httpmock.GetCallCountInfo()
24-
info["GET https://api.mybiz.com/articles"] // number of GET calls made to https://api.mybiz.com/articles
25-
info["GET https://api.mybiz.com/articles/id/12"] // number of GET calls made to https://api.mybiz.com/articles/id/12
26-
info[`GET =~^https://api\.mybiz\.com/articles/id/\d+\z`] // number of GET calls made to https://api.mybiz.com/articles/id/<any-number>
27-
}
20+
// get count info
21+
httpmock.GetTotalCallCount()
22+
23+
// get the amount of calls for the registered responder
24+
info := httpmock.GetCallCountInfo()
25+
info["GET https://api.mybiz.com/articles"] // number of GET calls made to https://api.mybiz.com/articles
26+
info["GET https://api.mybiz.com/articles/id/12"] // number of GET calls made to https://api.mybiz.com/articles/id/12
27+
info[`GET =~^https://api\.mybiz\.com/articles/id/\d+\z`] // number of GET calls made to https://api.mybiz.com/articles/id/<any-number>
28+
}
2829
2930
Advanced Example:
30-
func TestFetchArticles(t *testing.T) {
31-
httpmock.Activate()
32-
defer httpmock.DeactivateAndReset()
33-
34-
// our database of articles
35-
articles := make([]map[string]any, 0)
36-
37-
// mock to list out the articles
38-
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
39-
func(req *http.Request) (*http.Response, error) {
40-
resp, err := httpmock.NewJsonResponse(200, articles)
41-
if err != nil {
42-
return httpmock.NewStringResponse(500, ""), nil
43-
}
44-
return resp, nil
45-
},
46-
)
47-
48-
// return an article related to the request with the help of regexp submatch (\d+)
49-
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/(\d+)\z`,
50-
func(req *http.Request) (*http.Response, error) {
51-
// Get ID from request
52-
id := httpmock.MustGetSubmatchAsUint(req, 1) // 1=first regexp submatch
53-
return httpmock.NewJsonResponse(200, map[string]any{
54-
"id": id,
55-
"name": "My Great Article",
56-
})
57-
},
58-
)
59-
60-
// mock to add a new article
61-
httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles",
62-
func(req *http.Request) (*http.Response, error) {
63-
article := make(map[string]any)
64-
if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
65-
return httpmock.NewStringResponse(400, ""), nil
66-
}
67-
68-
articles = append(articles, article)
69-
70-
resp, err := httpmock.NewJsonResponse(200, article)
71-
if err != nil {
72-
return httpmock.NewStringResponse(500, ""), nil
73-
}
74-
return resp, nil
75-
},
76-
)
77-
78-
// do stuff that adds and checks articles
79-
}
31+
32+
func TestFetchArticles(t *testing.T) {
33+
httpmock.Activate()
34+
defer httpmock.DeactivateAndReset()
35+
36+
// our database of articles
37+
articles := make([]map[string]any, 0)
38+
39+
// mock to list out the articles
40+
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
41+
func(req *http.Request) (*http.Response, error) {
42+
resp, err := httpmock.NewJsonResponse(200, articles)
43+
if err != nil {
44+
return httpmock.NewStringResponse(500, ""), nil
45+
}
46+
return resp, nil
47+
},
48+
)
49+
50+
// return an article related to the request with the help of regexp submatch (\d+)
51+
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/(\d+)\z`,
52+
func(req *http.Request) (*http.Response, error) {
53+
// Get ID from request
54+
id := httpmock.MustGetSubmatchAsUint(req, 1) // 1=first regexp submatch
55+
return httpmock.NewJsonResponse(200, map[string]any{
56+
"id": id,
57+
"name": "My Great Article",
58+
})
59+
},
60+
)
61+
62+
// mock to add a new article
63+
httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles",
64+
func(req *http.Request) (*http.Response, error) {
65+
article := make(map[string]any)
66+
if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
67+
return httpmock.NewStringResponse(400, ""), nil
68+
}
69+
70+
articles = append(articles, article)
71+
72+
resp, err := httpmock.NewJsonResponse(200, article)
73+
if err != nil {
74+
return httpmock.NewStringResponse(500, ""), nil
75+
}
76+
return resp, nil
77+
},
78+
)
79+
80+
// do stuff that adds and checks articles
81+
}
8082
*/
8183
package httpmock

file.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,31 @@ package httpmock
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io/ioutil" //nolint: staticcheck
66
)
77

88
// File is a file name. The contents of this file is loaded on demand
99
// by the following methods.
1010
//
1111
// Note that:
12-
// file := httpmock.File("file.txt")
13-
// fmt.Printf("file: %s\n", file)
12+
//
13+
// file := httpmock.File("file.txt")
14+
// fmt.Printf("file: %s\n", file)
1415
//
1516
// prints the content of file "file.txt" as String() method is used.
1617
//
1718
// To print the file name, and not its content, simply do:
18-
// file := httpmock.File("file.txt")
19-
// fmt.Printf("file: %s\n", string(file))
19+
//
20+
// file := httpmock.File("file.txt")
21+
// fmt.Printf("file: %s\n", string(file))
2022
type File string
2123

2224
// MarshalJSON implements json.Marshaler.
2325
//
2426
// Useful to be used in conjunction with NewJsonResponse() or
2527
// NewJsonResponder() as in:
26-
// httpmock.NewJsonResponder(200, httpmock.File("body.json"))
28+
//
29+
// httpmock.NewJsonResponder(200, httpmock.File("body.json"))
2730
func (f File) MarshalJSON() ([]byte, error) {
2831
return f.bytes()
2932
}
@@ -37,7 +40,8 @@ func (f File) bytes() ([]byte, error) {
3740
//
3841
// Useful to be used in conjunction with NewBytesResponse() or
3942
// NewBytesResponder() as in:
40-
// httpmock.NewBytesResponder(200, httpmock.File("body.raw").Bytes())
43+
//
44+
// httpmock.NewBytesResponder(200, httpmock.File("body.raw").Bytes())
4145
func (f File) Bytes() []byte {
4246
b, err := f.bytes()
4347
if err != nil {
@@ -51,7 +55,8 @@ func (f File) Bytes() []byte {
5155
//
5256
// Useful to be used in conjunction with NewStringResponse() or
5357
// NewStringResponder() as in:
54-
// httpmock.NewStringResponder(200, httpmock.File("body.txt").String())
58+
//
59+
// httpmock.NewStringResponder(200, httpmock.File("body.txt").String())
5560
func (f File) String() string {
5661
return string(f.Bytes())
5762
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module github.com/jarcoal/httpmock
22

33
go 1.18
44

5-
require github.com/maxatome/go-testdeep v1.11.0
5+
require github.com/maxatome/go-testdeep v1.12.0
66

77
require github.com/davecgh/go-spew v1.1.1 // indirect

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/maxatome/go-testdeep v1.11.0 h1:Tgh5efyCYyJFGUYiT0qxBSIDeXw0F5zSoatlou685kk=
4-
github.com/maxatome/go-testdeep v1.11.0/go.mod h1:011SgQ6efzZYAen6fDn4BqQ+lUR72ysdyKe7Dyogw70=
3+
github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g=
4+
github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=

0 commit comments

Comments
 (0)