https://github.com/dogukanayd/requester
mockable Go request package
https://github.com/dogukanayd/requester
go golang http request testable
Last synced: 2 months ago
JSON representation
mockable Go request package
- Host: GitHub
- URL: https://github.com/dogukanayd/requester
- Owner: dogukanayd
- License: mit
- Created: 2022-01-07T15:56:58.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-14T18:31:27.000Z (over 3 years ago)
- Last Synced: 2025-01-27T21:14:30.304Z (4 months ago)
- Topics: go, golang, http, request, testable
- Language: Go
- Homepage: https://pkg.go.dev/github.com/dogukanayd/requester
- Size: 18.6 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Requester
[](https://coveralls.io/github/dogukanayd/requester?branch=main)
Request is a HTTP request library for Go with interfaces and mocks for unit tests.
The reason for having this package is using the interfaces to mock the HTTP requests easily on the codebase. Before
creating this repository I was copying and pasting this package to my projects. So I decided to move this package to the
separated package from my projects and use it with all of my projects.## How to use?
In your codebase, you should create a package called "requester" or with a name anything that you want. After that, you
have to create exactly the same interfaces that the package provides. Right after that you can mock the methods and
use them inside your unit tests.Example usage of the package:
```go
type ExampleRequestBody struct {
Name string
Surname string
}func example() {
erb := ExampleRequestBody{
Name: "Dogukan",
Surname: "Aydogdu",
}erbj, _ := json.Marshal(erb)
response, err := (&Request{
Timeout: 60,
Headers: []map[string]interface{}{
{
"Content-Type": "application/json",
},
},
Endpoint: "https://www.example.com",
Body: erbj,
}).Post()
}
```Example for mock the interface your own codebase:
```go
package your_request_packageimport (
"github.com/dogukanayd/requester"
"net/http"
)type Requester interface {
Get() (*http.Response, error)
Post() (*http.Response, error)
Put() (*http.Response, error)
Delete() (*http.Response, error)
}```