Skip to content

Repository files navigation

OpenAPI response Go test helper

HTTP/API boundary test helper for http servers

It sends requests with the standard library *http.Client and validates responses with github.com/getkin/kin-openapi.

Install

go get github.com/responsibleapi/test-go

Usage

package apitest
import (
"context""fmt""net/http""os""testing""time"
responsibleapi "github.com/responsibleapi/test-go"
)
var (
apiBaseURLstringapiClient http.Clientresponsible*responsibleapi.Responsible
)
funcTestMain(m*testing.M) {
os.Exit(run(m))
}
funcrun(m*testing.M) int {
ctx, cancel:=context.WithTimeout(context.Background(), 30*time.Second)
defercancel()
env, err:=startTestEnv(ctx)
iferr!=nil {
fmt.Fprintf(os.Stderr, "start api test environment: %v\n", err)
return1
}
apiBaseURL="http://"+env.server.PublicAddr()
apiClient= http.Client{Timeout: 10*time.Second}
responsible, err=responsibleapi.LoadFromFile("../../openapi.yaml", responsibleapi.Options{
BaseURL: apiBaseURL,
Client: &apiClient,
})
iferr!=nil {
fmt.Fprintf(os.Stderr, "load openapi spec: %v\n", err)
return1
}
code:=m.Run()
iferr:=env.Close(); err!=nil {
fmt.Fprintf(os.Stderr, "close api test environment: %v\n", err)
code=1
}
returncode
}
funcTestGetShow(t*testing.T) {
t.Parallel()
opts:= responsibleapi.CheckOptions{
OperationID: "getShow",
Request: responsibleapi.RequestOptions{
PathParams: map[string]string{"show_id": "abc123"},
},
Expected: http.StatusOK,
}
resp:=responsible.Check(t, opts)
deferfunc() {
_=resp.Body.Close()
}()
}

ResponsibleAPI turns OpenAPI into SDKs, docs, MCP servers, and CLIs: https://responsibleapi.com

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Contributors

Languages