-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split transform/marshal, reorg tests & dogfood humatest
- Loading branch information
1 parent
158ea48
commit 0a90edd
Showing
9 changed files
with
459 additions
and
482 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,69 @@ | ||
package huma | ||
package huma_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/danielgtaylor/huma/v2" | ||
"github.com/danielgtaylor/huma/v2/humatest" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBlankConfig(t *testing.T) { | ||
adapter := &testAdapter{chi.NewMux()} | ||
adapter := humatest.NewAdapter(chi.NewMux()) | ||
|
||
assert.NotPanics(t, func() { | ||
NewAPI(Config{}, adapter) | ||
huma.NewAPI(huma.Config{}, adapter) | ||
}) | ||
} | ||
|
||
// ExampleAdapter_handle demonstrates how to use the adapter directly | ||
// instead of using the `huma.Register` convenience function to add a new | ||
// operation and handler to the API. | ||
// | ||
// Note that you are responsible for defining all of the operation details, | ||
// including the parameter and response definitions & schemas. | ||
func ExampleAdapter_handle() { | ||
// Create an adapter for your chosen router. | ||
adapter := NewExampleAdapter(chi.NewMux()) | ||
|
||
// Register an operation with a custom handler. | ||
adapter.Handle(&huma.Operation{ | ||
OperationID: "example-operation", | ||
Method: "GET", | ||
Path: "/example/{name}", | ||
Summary: "Example operation", | ||
Parameters: []*huma.Param{ | ||
{ | ||
Name: "name", | ||
In: "path", | ||
Description: "Name to return", | ||
Required: true, | ||
Schema: &huma.Schema{ | ||
Type: "string", | ||
}, | ||
}, | ||
}, | ||
Responses: map[string]*huma.Response{ | ||
"200": { | ||
Description: "OK", | ||
Content: map[string]*huma.MediaType{ | ||
"text/plain": { | ||
Schema: &huma.Schema{ | ||
Type: "string", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, func(ctx huma.Context) { | ||
// Get the `name` path parameter. | ||
name := ctx.Param("name") | ||
|
||
// Set the response content type, status code, and body. | ||
ctx.SetHeader("Content-Type", "text/plain; charset=utf-8") | ||
ctx.SetStatus(http.StatusOK) | ||
ctx.BodyWriter().Write([]byte("Hello, " + name)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.