Skip to content

Commit

Permalink
refactor: remove chi as a dep for pure go impls
Browse files Browse the repository at this point in the history
  • Loading branch information
fwilkerson committed Nov 27, 2024
1 parent bfe8892 commit b6cb420
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 50 deletions.
39 changes: 39 additions & 0 deletions adapters/humachi/humachi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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"`
Expand Down
39 changes: 0 additions & 39 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
}
3 changes: 1 addition & 2 deletions autoregister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 5 additions & 7 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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{
Expand Down

0 comments on commit b6cb420

Please sign in to comment.