Skip to content

Commit

Permalink
Merge pull request #134 from jarcoal/go119
Browse files Browse the repository at this point in the history
ci: switch to go1.19, golangci-lint v1.49.0 &  go-testdeep v1. 12.0
  • Loading branch information
maxatome committed Aug 26, 2022
2 parents fbf2267 + 638f16c commit f6c4876
Show file tree
Hide file tree
Showing 10 changed files with 728 additions and 611 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
test:
strategy:
matrix:
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]
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]
full-tests: [false]
include:
- go-version: 1.18.x
- go-version: 1.19.x
full-tests: true

runs-on: ubuntu-latest
Expand All @@ -31,7 +31,7 @@ jobs:
if: matrix.full-tests
run: |
curl -sL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh |
sh -s -- -b $HOME/go/bin v1.45.2
sh -s -- -b $HOME/go/bin v1.49.0
$HOME/go/bin/golangci-lint run --max-issues-per-linter 0 \
--max-same-issues 0 \
-E bidichk \
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Easy mocking of http responses from external resources.

## Install

Currently supports Go 1.9 - 1.18.
Currently supports Go 1.9 - 1.19.

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

Expand Down
138 changes: 70 additions & 68 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,82 @@
Package httpmock provides tools for mocking HTTP responses.
Simple Example:
func TestFetchArticles(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// Exact URL match
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))
func TestFetchArticles(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// Regexp match (could use httpmock.RegisterRegexpResponder instead)
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/\d+\z`,
httpmock.NewStringResponder(200, `{"id": 1, "name": "My Great Article"}`))
// Exact URL match
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))
// do stuff that makes a request to articles
// Regexp match (could use httpmock.RegisterRegexpResponder instead)
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/\d+\z`,
httpmock.NewStringResponder(200, `{"id": 1, "name": "My Great Article"}`))
// get count info
httpmock.GetTotalCallCount()
// do stuff that makes a request to articles
// get the amount of calls for the registered responder
info := httpmock.GetCallCountInfo()
info["GET https://api.mybiz.com/articles"] // number of GET calls made to https://api.mybiz.com/articles
info["GET https://api.mybiz.com/articles/id/12"] // number of GET calls made to https://api.mybiz.com/articles/id/12
info[`GET =~^https://api\.mybiz\.com/articles/id/\d+\z`] // number of GET calls made to https://api.mybiz.com/articles/id/<any-number>
}
// get count info
httpmock.GetTotalCallCount()
// get the amount of calls for the registered responder
info := httpmock.GetCallCountInfo()
info["GET https://api.mybiz.com/articles"] // number of GET calls made to https://api.mybiz.com/articles
info["GET https://api.mybiz.com/articles/id/12"] // number of GET calls made to https://api.mybiz.com/articles/id/12
info[`GET =~^https://api\.mybiz\.com/articles/id/\d+\z`] // number of GET calls made to https://api.mybiz.com/articles/id/<any-number>
}
Advanced Example:
func TestFetchArticles(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// our database of articles
articles := make([]map[string]any, 0)
// mock to list out the articles
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(200, articles)
if err != nil {
return httpmock.NewStringResponse(500, ""), nil
}
return resp, nil
},
)
// return an article related to the request with the help of regexp submatch (\d+)
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/(\d+)\z`,
func(req *http.Request) (*http.Response, error) {
// Get ID from request
id := httpmock.MustGetSubmatchAsUint(req, 1) // 1=first regexp submatch
return httpmock.NewJsonResponse(200, map[string]any{
"id": id,
"name": "My Great Article",
})
},
)
// mock to add a new article
httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles",
func(req *http.Request) (*http.Response, error) {
article := make(map[string]any)
if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
articles = append(articles, article)
resp, err := httpmock.NewJsonResponse(200, article)
if err != nil {
return httpmock.NewStringResponse(500, ""), nil
}
return resp, nil
},
)
// do stuff that adds and checks articles
}
func TestFetchArticles(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// our database of articles
articles := make([]map[string]any, 0)
// mock to list out the articles
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles",
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(200, articles)
if err != nil {
return httpmock.NewStringResponse(500, ""), nil
}
return resp, nil
},
)
// return an article related to the request with the help of regexp submatch (\d+)
httpmock.RegisterResponder("GET", `=~^https://api\.mybiz\.com/articles/id/(\d+)\z`,
func(req *http.Request) (*http.Response, error) {
// Get ID from request
id := httpmock.MustGetSubmatchAsUint(req, 1) // 1=first regexp submatch
return httpmock.NewJsonResponse(200, map[string]any{
"id": id,
"name": "My Great Article",
})
},
)
// mock to add a new article
httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles",
func(req *http.Request) (*http.Response, error) {
article := make(map[string]any)
if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
articles = append(articles, article)
resp, err := httpmock.NewJsonResponse(200, article)
if err != nil {
return httpmock.NewStringResponse(500, ""), nil
}
return resp, nil
},
)
// do stuff that adds and checks articles
}
*/
package httpmock
42 changes: 24 additions & 18 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@ package httpmock

import (
"fmt"
"io/ioutil"
"io/ioutil" //nolint: staticcheck
)

// File is a file name. The contents of this file is loaded on demand
// by the following methods.
//
// Note that:
// file := httpmock.File("file.txt")
// fmt.Printf("file: %s\n", file)
//
// prints the content of file "file.txt" as String() method is used.
// file := httpmock.File("file.txt")
// fmt.Printf("file: %s\n", file)
//
// prints the content of file "file.txt" as [File.String] method is used.
//
// To print the file name, and not its content, simply do:
// file := httpmock.File("file.txt")
// fmt.Printf("file: %s\n", string(file))
//
// file := httpmock.File("file.txt")
// fmt.Printf("file: %s\n", string(file))
type File string

// MarshalJSON implements json.Marshaler.
// MarshalJSON implements [encoding/json.Marshaler].
//
// Useful to be used in conjunction with [NewJsonResponse] or
// [NewJsonResponder] as in:
//
// Useful to be used in conjunction with NewJsonResponse() or
// NewJsonResponder() as in:
// httpmock.NewJsonResponder(200, httpmock.File("body.json"))
// httpmock.NewJsonResponder(200, httpmock.File("body.json"))
func (f File) MarshalJSON() ([]byte, error) {
return f.bytes()
}
Expand All @@ -35,9 +38,10 @@ func (f File) bytes() ([]byte, error) {
// Bytes returns the content of file as a []byte. If an error occurs
// during the opening or reading of the file, it panics.
//
// Useful to be used in conjunction with NewBytesResponse() or
// NewBytesResponder() as in:
// httpmock.NewBytesResponder(200, httpmock.File("body.raw").Bytes())
// Useful to be used in conjunction with [NewBytesResponse] or
// [NewBytesResponder] as in:
//
// httpmock.NewBytesResponder(200, httpmock.File("body.raw").Bytes())
func (f File) Bytes() []byte {
b, err := f.bytes()
if err != nil {
Expand All @@ -46,12 +50,14 @@ func (f File) Bytes() []byte {
return b
}

// String returns the content of file as a string. If an error occurs
// during the opening or reading of the file, it panics.
// String implements [fmt.Stringer] and returns the content of file as
// a string. If an error occurs during the opening or reading of the
// file, it panics.
//
// Useful to be used in conjunction with [NewStringResponse] or
// [NewStringResponder] as in:
//
// Useful to be used in conjunction with NewStringResponse() or
// NewStringResponder() as in:
// httpmock.NewStringResponder(200, httpmock.File("body.txt").String())
// httpmock.NewStringResponder(200, httpmock.File("body.txt").String())
func (f File) String() string {
return string(f.Bytes())
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module github.com/jarcoal/httpmock

go 1.18

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

require github.com/davecgh/go-spew v1.1.1 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/maxatome/go-testdeep v1.11.0 h1:Tgh5efyCYyJFGUYiT0qxBSIDeXw0F5zSoatlou685kk=
github.com/maxatome/go-testdeep v1.11.0/go.mod h1:011SgQ6efzZYAen6fDn4BqQ+lUR72ysdyKe7Dyogw70=
github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g=
github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
Loading

0 comments on commit f6c4876

Please sign in to comment.