Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/segment-boneyard/mockwebserver
Scriptable web server for testing HTTP clients.
https://github.com/segment-boneyard/mockwebserver
Last synced: about 5 hours ago
JSON representation
Scriptable web server for testing HTTP clients.
- Host: GitHub
- URL: https://github.com/segment-boneyard/mockwebserver
- Owner: segment-boneyard
- Created: 2015-07-09T21:43:08.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-07-17T21:58:11.000Z (over 9 years ago)
- Last Synced: 2024-04-09T16:31:12.445Z (7 months ago)
- Language: Go
- Homepage:
- Size: 190 KB
- Stars: 8
- Watchers: 46
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MockWebServer [![GoDoc](https://godoc.org/github.com/segmentio/mockwebserver?status.svg)](https://godoc.org/github.com/segmentio/mockwebserver)
`MockWebServer` is a scriptable web server for testing HTTP clients.
This library makes it easy to test that your app Does The Right Thing when it makes HTTP and HTTPS calls. It lets you specify which responses to return and then verify that requests were made as expected.
Because it exercises your full HTTP stack, you can be confident that you're testing everything. You can even copy & paste HTTP responses from your real web server to create representative test cases. Or test that your code survives in awkward-to-reproduce situations like 500 errors or slow-loading responses.
Inspired by [`MockWebServer` in OkHttp](https://github.com/square/okhttp/tree/master/mockwebserver).
## Example
Use mockwebserver the same way that you use mocking frameworks:
1. Script the mocks.
2. Run application code.
3. Verify that the expected requests were made.## Example
```go
// Create a MockWebServer. These are lean enough that you can create a new
// instance for every unit test.
s := mockwebserver.New()// Start the server and get it's URL. You'll need this to make HTTP requests.
url := s.Start()
// Stop the server after the test finishes.
defer s.Stop()// Enqueue some responses.
s.Enqueue(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "https://giphy.com/gifs/sloth-WVLZLE4yGCQFi")
})// Exercise your application code, which should make those HTTP requests.
// Responses are returned in the same order that they are enqueued.
resp, err := http.Get(url + "/foo")// Assert your application code.
assert.Equal(t, err, nil)
assert.Equal(t, resp.StatusCode, 200)
assertBodyEqual(t, resp, "https://giphy.com/gifs/sloth-WVLZLE4yGCQFi\n")// Optional: confirm that your app made the HTTP requests you were expecting.
request := s.TakeRequest()
assert.Equal(t, request.URL.String(), "/foo")
```# License
MIT