Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/arvida/webstub
Stubbing HTTP requests in Go. WIP
https://github.com/arvida/webstub
Last synced: about 2 months ago
JSON representation
Stubbing HTTP requests in Go. WIP
- Host: GitHub
- URL: https://github.com/arvida/webstub
- Owner: arvida
- License: mit
- Created: 2013-04-16T15:57:10.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2013-04-17T15:03:07.000Z (almost 12 years ago)
- Last Synced: 2024-10-14T10:13:58.683Z (3 months ago)
- Language: Go
- Size: 125 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# webstub
A Go package for stubbing HTTP requests.
Can be useful when you are writing tests for your golang code that talks to HTTP servers.
## Installation
$ go get github.com/arvida/webstub
## Example
```go
package mainimport (
"github.com/arvida/webstub"
"io/ioutil"
"log"
"net/http"
)func main() {
// Inject webstub
webstub.Enable()// Setup a stubbed response for GET requests to http://example.com/my-endpoint
r := webstub.Request{
Method: "GET",
Url: "http://example.com/my-endpoint",
Response: "Hello from the example!",
}
webstub.Register(r)// Make a request
resp, err := http.Get("http://example.com/my-endpoint")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}log.Println(string(body))
}
```### Response from file
Save the response to a text file. It is easy to save a live response with `curl`:
$ curl -is www.example.com > stubbed_response.txt
Set the `webstub.Request` `file` parameter to specify the response file to use:
```go
r := webstub.Request{
method: "GET",
url: "http://example.com/",
file: "stubbed_response.txt",
}
webstub.Register(r)
```## Contribute
Please do. Create a issue or pull request.
## Copyright
Copyright (c) 2013 Arvid Andersson. See LICENSE for details.