# README
testserver
testserver is a framework for mocking http server. It integrates with Go's built-in testing
.
Installation
After go mod init <your_project_name>
and go mod tidy
just run:
go get github.com/peliseev/testserver
Building Mocks
The common way to use this framework is as follows:
- Create new instance of
TestServer
- Add some expectations
- Start
TestServer
- Create an instance of the object under test
- Call the methods under test
- Check the results
For example:
func TestFunc(t *testing.T) {
// Create new instance
ts := testserver.New(t)
// Add some expectations
ts.Add(EXPECT().Method("GET").Path("/api/profile").
RespWithStatus(200).
RespWithBody(profile))
ts.Add(EXPECT().Method("POST").Path("/api/order").
RespWithStatus(200).
RespWithBody(done).Times(2))
...
// Start test Server
ts.Start()
defer ts.Stop()
// Create an instance of the object under test
service := order.New(ts.URL)
// Call the method under test
gotRs, gotErr := service.CreateOrder(args...)
// Check the results
if wantRs != gotRs {
t.Error()
}
if wantErr != gotErr {
t.Error()
}
}
You can find more examples of usage here
# Structs
ExpectationBuilder help to build expectations for http request and response.
TestServer is a structure that contains a set of expectations and a server from the httptest package.