mirror of
https://github.com/googleapis/genai-toolbox.git
synced 2026-01-11 16:38:15 -05:00
fix: handle content-type correctly (#33)
Fixes a bug where the content-type wasn't being set correctly in the response type.
This commit is contained in:
@@ -15,9 +15,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
@@ -29,11 +27,13 @@ import (
|
||||
func apiRouter(s *Server) (chi.Router, error) {
|
||||
r := chi.NewRouter()
|
||||
|
||||
r.Use(middleware.AllowContentType("application/json"))
|
||||
r.Use(render.SetContentType(render.ContentTypeJSON))
|
||||
|
||||
r.Get("/toolset/", func(w http.ResponseWriter, r *http.Request) { toolsetHandler(s, w, r) })
|
||||
r.Get("/toolset/{toolsetName}", func(w http.ResponseWriter, r *http.Request) { toolsetHandler(s, w, r) })
|
||||
|
||||
r.Route("/tool/{toolName}", func(r chi.Router) {
|
||||
r.Use(middleware.AllowContentType("application/json"))
|
||||
r.Post("/invoke", func(w http.ResponseWriter, r *http.Request) { toolInvokeHandler(s, w, r) })
|
||||
})
|
||||
|
||||
@@ -45,16 +45,10 @@ func toolsetHandler(s *Server, w http.ResponseWriter, r *http.Request) {
|
||||
toolsetName := chi.URLParam(r, "toolsetName")
|
||||
toolset, ok := s.toolsets[toolsetName]
|
||||
if !ok {
|
||||
http.Error(w, fmt.Sprintf("Toolset %q does not exist", toolsetName), http.StatusNotFound)
|
||||
_ = render.Render(w, r, newErrResponse(fmt.Errorf("Toolset %q does not exist", toolsetName), http.StatusNotFound))
|
||||
return
|
||||
}
|
||||
b, err := json.Marshal(toolset.Manifest)
|
||||
if err != nil {
|
||||
log.Printf("unable to JSON the toolset manifest: %s", err)
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
_, _ = w.Write(b)
|
||||
render.JSON(w, r, toolset.Manifest)
|
||||
}
|
||||
|
||||
// toolInvokeHandler handles the API request to invoke a specific Tool.
|
||||
@@ -94,7 +88,7 @@ func toolInvokeHandler(s *Server, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var _ render.Renderer = &resultResponse{} // Renderer interface for managing response payloads.
|
||||
|
||||
// resultResponse is the response sent back when the tool was invocated succesffully.
|
||||
// resultResponse is the response sent back when the tool was invocated successfully.
|
||||
type resultResponse struct {
|
||||
Result string `json:"result"` // result of tool invocation
|
||||
}
|
||||
|
||||
@@ -129,6 +129,10 @@ func TestToolsetEndpoint(t *testing.T) {
|
||||
t.Fatalf("unexpected error during request: %s", err)
|
||||
}
|
||||
|
||||
if contentType := resp.Header.Get("Content-type"); contentType != "application/json" {
|
||||
t.Fatalf("unexpected content-type header: want %s, got %s", "application/json", contentType)
|
||||
}
|
||||
|
||||
if resp.StatusCode != tc.want.statusCode {
|
||||
t.Logf("response body: %s", body)
|
||||
t.Fatalf("unexpected status code: want %d, got %d", tc.want.statusCode, resp.StatusCode)
|
||||
|
||||
Reference in New Issue
Block a user