hit is an http integration test framework written in golang.
It is designed to be flexible as possible, but to keep a simple to use interface for developers.
So lets get started!
go get -u github.com/Eun/go-hit
import (
"net/http"
. "github.com/Eun/go-hit"
)
func TestHttpBin(t *testing.T) {
Test(t,
Description("Post to httpbin.org"),
Get("https://httpbin.org/post"),
Expect().Status(http.StatusMethodNotAllowed),
)
}
Test(t,
Get("https://httpbin.org/post"),
Expect().Status(http.StatusMethodNotAllowed),
Expect().Headers().Contains("Content-Type"),
Expect().Body().Contains("Method Not Allowed"),
)
Test(t,
Post("https://httpbin.org/post"),
Send().Body("Hello HttpBin"),
Expect().Status(http.StatusOK),
Expect().Body().Contains("Hello HttpBin"),
)
Test(t,
Post("https://httpbin.org/post"),
Send().Headers().Set("Content-Type", "application/json"),
Send().Body().JSON(map[string][]string{"Foo": []string{"Bar", "Baz"}}),
Expect().Status(http.StatusOK),
Expect().Body().JSON().Equal("json.Foo.1", "Baz"),
)
Test(
Post(t, "https://httpbin.org/post"),
Debug(),
)
Although the following is hard to read it is possible to do!
Test(t,
Post("https://httpbin.org/post"),
Expect().Status(200),
Send("Hello World"),
Expect().Body().Contains("Hello"),
)
Test(t,
Get("https://httpbin.org/get"),
Send().Custom(func(hit Hit) {
hit.Request().Body().SetStringf("Hello %s", "World")
}),
Expect().Custom(func(hit Hit) {
if len(hit.Response().Body().String()) <= 0 {
t.FailNow()
}
}),
Custom(AfterExpectStep, func(Hit) {
fmt.Println("everything done")
}),
)
template := []IStep{
Post("https://httpbin.org/post"),
Send().Headers().Set("Content-Type", "application/json"),
Expect().Headers("Content-Type").Equal("application/json"),
}
Test(t,
append(template,
Send().Body().JSON("Hello World"),
)...,
)
Test(t,
append(template,
Send().Body().JSON("Hello Universe"),
)...,
)
More examples can be found in the examples directory
- Fixed a double run bug in
CombineSteps
(#3) - Better
Clear
functionality, you can now clear any previous step by prependingClear()
, eg.Do( Get("https://example.com"), Expect().Body("Hello World"), Clear().Expect(), // remove all previous Expect() steps // Clear().Expect().Body("Hello World"), // remove only the Expect().Body("Hello World") step ) // also works for CombineSteps Do( Post("https://example.com"), CombineSteps( Send().Body("Hello World"), Expect().Body("Hello World"), ), Clear().Expect(), )
- Simplified
Expect().Header()
use, no moreExpect().Headers()
, everything can now be done in theExpect().Header()
function. - More documentation and examples
hit.Do
andhit.MustDo
for inline steps.- Removal of inline steps (use
hit.Do
andhit.MustDo
)Do( Get("https://example.com"), Expect().Custon(func (hit Hit) { // Expect("Hello World") is invalid now // you must use MustDo() or Do() hit.MustDo( Expect("Hello World"), ) }), )
hit.InsertSteps
to insert steps during runtime