Skip to content

Commit

Permalink
Merge pull request #383 from ssoroka/must-specify-openapi
Browse files Browse the repository at this point in the history
must specify openapi config
  • Loading branch information
danielgtaylor committed Apr 15, 2024
2 parents a3eb57c + fff0bc7 commit 808a95d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
3 changes: 1 addition & 2 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ func TestErrorResponses(t *testing.T) {
}

func TestNegotiateError(t *testing.T) {
_, api := humatest.New(t, huma.Config{})
_, api := humatest.New(t, huma.Config{OpenAPI: &huma.OpenAPI{Info: &huma.Info{Title: "Test API", Version: "1.0.0"}}})

req, _ := http.NewRequest("GET", "/", nil)
resp := httptest.NewRecorder()
ctx := humatest.NewContext(nil, req, resp)

require.Error(t, huma.WriteErr(api, ctx, 400, "bad request"))
}

Expand Down
5 changes: 5 additions & 0 deletions humatest/humatest.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ func Wrap(tb TB, api huma.API) TestAPI {
// to customize how the API is created. If no configuration is provided then
// a simple default configuration supporting `application/json` is used.
func New(tb TB, configs ...huma.Config) (http.Handler, TestAPI) {
for _, config := range configs {
if config.OpenAPI == nil {
panic("custom huma.Config structs must specify a value for OpenAPI")
}
}
if len(configs) == 0 {
configs = append(configs, huma.Config{
OpenAPI: &huma.OpenAPI{
Expand Down
6 changes: 6 additions & 0 deletions humatest/humatest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,9 @@ func TestDumpBodyError(t *testing.T) {
_, err = io.ReadAll(req.Body)
require.Error(t, err)
}

func TestOpenAPIRequired(t *testing.T) {
assert.PanicsWithValue(t, "custom huma.Config structs must specify a value for OpenAPI", func() {
New(t, huma.Config{})
})
}

0 comments on commit 808a95d

Please sign in to comment.