diff --git a/adapters/humachi/humachi_test.go b/adapters/humachi/humachi_test.go index f9916834..9721c669 100644 --- a/adapters/humachi/humachi_test.go +++ b/adapters/humachi/humachi_test.go @@ -13,7 +13,9 @@ import ( "time" "github.com/danielgtaylor/huma/v2" + "github.com/danielgtaylor/huma/v2/humatest" "github.com/go-chi/chi/v5" + "github.com/stretchr/testify/assert" ) var lastModified = time.Now() @@ -314,6 +316,43 @@ func BenchmarkRawChiFast(b *testing.B) { } } +func TestChiRouterPrefix(t *testing.T) { + mux := chi.NewMux() + var api huma.API + mux.Route("/api", func(r chi.Router) { + config := huma.DefaultConfig("My API", "1.0.0") + config.Servers = []*huma.Server{{URL: "http://localhost:8888/api"}} + api = New(r, config) + }) + + type TestOutput struct { + Body struct { + Field string `json:"field"` + } + } + + // Register a simple hello world operation in the API. + huma.Get(api, "/test", func(ctx context.Context, input *struct{}) (*TestOutput, error) { + return &TestOutput{}, nil + }) + + // Create a test API around the underlying router to make easier requests. + tapi := humatest.Wrap(t, New(mux, huma.DefaultConfig("Test", "1.0.0"))) + + // The top-level router should respond to the full path even though the + // operation was registered with just `/test`. + resp := tapi.Get("/api/test") + assert.Equal(t, http.StatusOK, resp.Code) + + // The transformer should generate links with the full URL path. + assert.Contains(t, resp.Header().Get("Link"), "/api/schemas/TestOutputBody.json") + + // The docs HTML should point to the full URL including base path. + resp = tapi.Get("/api/docs") + assert.Equal(t, http.StatusOK, resp.Code) + assert.Contains(t, resp.Body.String(), "/api/openapi.yaml") +} + // func BenchmarkHumaV1Chi(t *testing.B) { // type GreetingInput struct { // ID string `path:"id"` diff --git a/api_test.go b/api_test.go index 87c9e186..4ffbe650 100644 --- a/api_test.go +++ b/api_test.go @@ -6,9 +6,7 @@ import ( "testing" "github.com/danielgtaylor/huma/v2" - "github.com/danielgtaylor/huma/v2/adapters/humachi" "github.com/danielgtaylor/huma/v2/humatest" - "github.com/go-chi/chi/v5" "github.com/stretchr/testify/assert" ) @@ -89,40 +87,3 @@ func TestContextValue(t *testing.T) { resp := api.Get("/test") assert.Equal(t, http.StatusNoContent, resp.Code) } - -func TestChiRouterPrefix(t *testing.T) { - mux := chi.NewMux() - var api huma.API - mux.Route("/api", func(r chi.Router) { - config := huma.DefaultConfig("My API", "1.0.0") - config.Servers = []*huma.Server{{URL: "http://localhost:8888/api"}} - api = humachi.New(r, config) - }) - - type TestOutput struct { - Body struct { - Field string `json:"field"` - } - } - - // Register a simple hello world operation in the API. - huma.Get(api, "/test", func(ctx context.Context, input *struct{}) (*TestOutput, error) { - return &TestOutput{}, nil - }) - - // Create a test API around the underlying router to make easier requests. - tapi := humatest.Wrap(t, humachi.New(mux, huma.DefaultConfig("Test", "1.0.0"))) - - // The top-level router should respond to the full path even though the - // operation was registered with just `/test`. - resp := tapi.Get("/api/test") - assert.Equal(t, http.StatusOK, resp.Code) - - // The transformer should generate links with the full URL path. - assert.Contains(t, resp.Header().Get("Link"), "/api/schemas/TestOutputBody.json") - - // The docs HTML should point to the full URL including base path. - resp = tapi.Get("/api/docs") - assert.Equal(t, http.StatusOK, resp.Code) - assert.Contains(t, resp.Body.String(), "/api/openapi.yaml") -} diff --git a/autoregister_test.go b/autoregister_test.go index 107c29c6..34ede32d 100644 --- a/autoregister_test.go +++ b/autoregister_test.go @@ -6,7 +6,6 @@ import ( "net/http" "github.com/danielgtaylor/huma/v2" - "github.com/go-chi/chi/v5" ) // Item represents a single item with a unique ID. @@ -40,7 +39,7 @@ func (s *ItemsHandler) RegisterListItems(api huma.API) { func ExampleAutoRegister() { // Create the router and API. - router := chi.NewMux() + router := http.NewServeMux() api := NewExampleAPI(router, huma.DefaultConfig("My Service", "1.0.0")) // Create the item handler and register all of its operations. diff --git a/huma_test.go b/huma_test.go index b3dd5b1d..73f907f9 100644 --- a/huma_test.go +++ b/huma_test.go @@ -17,18 +17,17 @@ import ( "testing" "time" - "github.com/go-chi/chi/v5" "github.com/google/uuid" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/danielgtaylor/huma/v2" - "github.com/danielgtaylor/huma/v2/adapters/humachi" + "github.com/danielgtaylor/huma/v2/adapters/humago" "github.com/danielgtaylor/huma/v2/humatest" ) var NewExampleAdapter = humatest.NewAdapter -var NewExampleAPI = humachi.New +var NewExampleAPI = humago.New // Recoverer is a really simple recovery middleware we can use during tests. func Recoverer(next http.Handler) http.Handler { @@ -1899,13 +1898,12 @@ Content of example2.txt. }, } { t.Run(feature.Name, func(t *testing.T) { - r := chi.NewRouter() - r.Use(Recoverer) + r := http.NewServeMux() config := huma.DefaultConfig("Features Test API", "1.0.0") if feature.Transformers != nil { config.Transformers = append(config.Transformers, feature.Transformers...) } - api := humatest.Wrap(t, humachi.New(r, config)) + api := humatest.Wrap(t, humago.New(r, config)) feature.Register(t, api) var body io.Reader = nil @@ -1917,7 +1915,7 @@ Content of example2.txt. req.Header.Set(k, v) } w := httptest.NewRecorder() - r.ServeHTTP(w, req) + Recoverer(r).ServeHTTP(w, req) b, _ := api.OpenAPI().YAML() t.Log(string(b)) b, _ = httputil.DumpResponse(w.Result(), true) diff --git a/humacli/humacli_test.go b/humacli/humacli_test.go index f4542896..e776829c 100644 --- a/humacli/humacli_test.go +++ b/humacli/humacli_test.go @@ -7,14 +7,12 @@ import ( "log" "net/http" "os" - "syscall" "testing" "time" "github.com/danielgtaylor/huma/v2" - "github.com/danielgtaylor/huma/v2/adapters/humachi" + "github.com/danielgtaylor/huma/v2/adapters/humago" "github.com/danielgtaylor/huma/v2/humacli" - "github.com/go-chi/chi/v5" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" ) @@ -33,8 +31,8 @@ func ExampleCLI() { opts.Debug, opts.Host, opts.Port) // Set up the router & API - router := chi.NewRouter() - api := humachi.New(router, huma.DefaultConfig("My API", "1.0.0")) + mux := http.NewServeMux() + api := humago.New(mux, huma.DefaultConfig("My API", "1.0.0")) huma.Register(api, huma.Operation{ OperationID: "hello", @@ -47,7 +45,7 @@ func ExampleCLI() { srv := &http.Server{ Addr: fmt.Sprintf("%s:%d", opts.Host, opts.Port), - Handler: router, + Handler: mux, // TODO: Set up timeouts! } @@ -214,9 +212,14 @@ func TestCLIShutdown(t *testing.T) { }) }) + p, err := os.FindProcess(os.Getpid()) + if err != nil { + t.Fatalf("failed to find process: %v", os.Getpid()) + } + go func() { time.Sleep(10 * time.Millisecond) - syscall.Kill(syscall.Getpid(), syscall.SIGINT) + p.Signal(os.Interrupt) }() cli.Root().SetArgs([]string{}) diff --git a/resolver_test.go b/resolver_test.go index a445b039..f4c16d39 100644 --- a/resolver_test.go +++ b/resolver_test.go @@ -10,7 +10,6 @@ import ( "strings" "github.com/danielgtaylor/huma/v2" - "github.com/go-chi/chi/v5" ) // Step 1: Create your input struct where you want to do additional validation. @@ -35,7 +34,7 @@ func (b *ExampleInputBody) Resolve(ctx huma.Context, prefix *huma.PathBuffer) [] func ExampleResolver() { // Create the API. - r := chi.NewRouter() + r := http.NewServeMux() api := NewExampleAPI(r, huma.DefaultConfig("Example API", "1.0.0")) huma.Register(api, huma.Operation{ diff --git a/sse/sse_example_test.go b/sse/sse_example_test.go index 2e47984e..b2c9380a 100644 --- a/sse/sse_example_test.go +++ b/sse/sse_example_test.go @@ -5,9 +5,8 @@ import ( "net/http" "github.com/danielgtaylor/huma/v2" - "github.com/danielgtaylor/huma/v2/adapters/humachi" + "github.com/danielgtaylor/huma/v2/adapters/humago" "github.com/danielgtaylor/huma/v2/sse" - "github.com/go-chi/chi/v5" ) func ExampleRegister_sse() { @@ -25,8 +24,8 @@ func ExampleRegister_sse() { type UserDeletedEvent UserEvent // 2. Set up the API. - router := chi.NewMux() - api := humachi.New(router, huma.DefaultConfig("My API", "1.0.0")) + mux := http.NewServeMux() + api := humago.New(mux, huma.DefaultConfig("My API", "1.0.0")) // 3. Register an SSE operation. sse.Register(api, huma.Operation{