Skip to content

Commit 4c7c4b0

Browse files
committed
tests
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 9e187b7 commit 4c7c4b0

21 files changed

+286
-212
lines changed

core/http/endpoints/explorer/dashboard.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/labstack/echo/v4"
1010
"github.com/mudler/LocalAI/core/explorer"
11-
"github.com/mudler/LocalAI/core/http/utils"
11+
"github.com/mudler/LocalAI/core/http/middleware"
1212
"github.com/mudler/LocalAI/internal"
1313
)
1414

@@ -17,7 +17,7 @@ func Dashboard() echo.HandlerFunc {
1717
summary := map[string]interface{}{
1818
"Title": "LocalAI API - " + internal.PrintableVersion(),
1919
"Version": internal.PrintableVersion(),
20-
"BaseURL": utils.BaseURL(c),
20+
"BaseURL": middleware.BaseURL(c),
2121
}
2222

2323
contentType := c.Request().Header.Get("Content-Type")

core/http/endpoints/localai/backend.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/google/uuid"
99
"github.com/mudler/LocalAI/core/config"
1010
"github.com/mudler/LocalAI/core/gallery"
11-
"github.com/mudler/LocalAI/core/http/utils"
11+
"github.com/mudler/LocalAI/core/http/middleware"
1212
"github.com/mudler/LocalAI/core/schema"
1313
"github.com/mudler/LocalAI/core/services"
1414
"github.com/mudler/LocalAI/pkg/system"
@@ -82,7 +82,7 @@ func (mgs *BackendEndpointService) ApplyBackendEndpoint() echo.HandlerFunc {
8282
Galleries: mgs.galleries,
8383
}
8484

85-
return c.JSON(200, schema.BackendResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%sbackends/jobs/%s", utils.BaseURL(c), uuid.String())})
85+
return c.JSON(200, schema.BackendResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%sbackends/jobs/%s", middleware.BaseURL(c), uuid.String())})
8686
}
8787
}
8888

@@ -106,7 +106,7 @@ func (mgs *BackendEndpointService) DeleteBackendEndpoint() echo.HandlerFunc {
106106
return err
107107
}
108108

109-
return c.JSON(200, schema.BackendResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%sbackends/jobs/%s", utils.BaseURL(c), uuid.String())})
109+
return c.JSON(200, schema.BackendResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%sbackends/jobs/%s", middleware.BaseURL(c), uuid.String())})
110110
}
111111
}
112112

core/http/endpoints/localai/edit_model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/labstack/echo/v4"
1010
"github.com/mudler/LocalAI/core/config"
11-
httpUtils "github.com/mudler/LocalAI/core/http/utils"
11+
httpUtils "github.com/mudler/LocalAI/core/http/middleware"
1212
"github.com/mudler/LocalAI/internal"
1313
"github.com/mudler/LocalAI/pkg/utils"
1414

core/http/endpoints/localai/edit_model_test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package localai_test
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"io"
67
"net/http"
78
"net/http/httptest"
@@ -16,6 +17,14 @@ import (
1617
. "github.com/onsi/gomega"
1718
)
1819

20+
// testRenderer is a simple renderer for tests that returns JSON
21+
type testRenderer struct{}
22+
23+
func (t *testRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
24+
// For tests, just return the data as JSON
25+
return json.NewEncoder(w).Encode(data)
26+
}
27+
1928
var _ = Describe("Edit Model test", func() {
2029

2130
var tempDir string
@@ -41,9 +50,12 @@ var _ = Describe("Edit Model test", func() {
4150
//modelLoader := model.NewModelLoader(systemState, true)
4251
modelConfigLoader := config.NewModelConfigLoader(systemState.Model.ModelsPath)
4352

44-
// Define Echo app.
53+
// Define Echo app and register all routes upfront
4554
app := echo.New()
55+
// Set up a simple renderer for the test
56+
app.Renderer = &testRenderer{}
4657
app.POST("/import-model", ImportModelEndpoint(modelConfigLoader, applicationConfig))
58+
app.GET("/edit-model/:name", GetEditModelPage(modelConfigLoader, applicationConfig))
4759

4860
requestBody := bytes.NewBufferString(`{"name": "foo", "backend": "foo", "model": "foo"}`)
4961

@@ -57,16 +69,15 @@ var _ = Describe("Edit Model test", func() {
5769
Expect(string(body)).To(ContainSubstring("Model configuration created successfully"))
5870
Expect(rec.Code).To(Equal(http.StatusOK))
5971

60-
app.GET("/edit-model/:name", GetEditModelPage(modelConfigLoader, applicationConfig))
61-
requestBody = bytes.NewBufferString(`{"name": "foo", "parameters": { "model": "foo"}}`)
62-
63-
req = httptest.NewRequest("GET", "/edit-model/foo", requestBody)
72+
req = httptest.NewRequest("GET", "/edit-model/foo", nil)
6473
rec = httptest.NewRecorder()
6574
app.ServeHTTP(rec, req)
6675

6776
body, err = io.ReadAll(rec.Body)
6877
Expect(err).ToNot(HaveOccurred())
69-
Expect(string(body)).To(ContainSubstring(`"model":"foo"`))
78+
// The response contains the model configuration with backend field
79+
Expect(string(body)).To(ContainSubstring(`"backend":"foo"`))
80+
Expect(string(body)).To(ContainSubstring(`"name":"foo"`))
7081
Expect(rec.Code).To(Equal(http.StatusOK))
7182
})
7283
})

core/http/endpoints/localai/gallery.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/google/uuid"
99
"github.com/mudler/LocalAI/core/config"
1010
"github.com/mudler/LocalAI/core/gallery"
11-
"github.com/mudler/LocalAI/core/http/utils"
11+
"github.com/mudler/LocalAI/core/http/middleware"
1212
"github.com/mudler/LocalAI/core/schema"
1313
"github.com/mudler/LocalAI/core/services"
1414
"github.com/mudler/LocalAI/pkg/system"
@@ -85,7 +85,7 @@ func (mgs *ModelGalleryEndpointService) ApplyModelGalleryEndpoint() echo.Handler
8585
BackendGalleries: mgs.backendGalleries,
8686
}
8787

88-
return c.JSON(200, schema.GalleryResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%smodels/jobs/%s", utils.BaseURL(c), uuid.String())})
88+
return c.JSON(200, schema.GalleryResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%smodels/jobs/%s", middleware.BaseURL(c), uuid.String())})
8989
}
9090
}
9191

@@ -108,7 +108,7 @@ func (mgs *ModelGalleryEndpointService) DeleteModelGalleryEndpoint() echo.Handle
108108
return err
109109
}
110110

111-
return c.JSON(200, schema.GalleryResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%smodels/jobs/%s", utils.BaseURL(c), uuid.String())})
111+
return c.JSON(200, schema.GalleryResponse{ID: uuid.String(), StatusURL: fmt.Sprintf("%smodels/jobs/%s", middleware.BaseURL(c), uuid.String())})
112112
}
113113
}
114114

core/http/endpoints/localai/import_model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/mudler/LocalAI/core/config"
1515
"github.com/mudler/LocalAI/core/gallery"
1616
"github.com/mudler/LocalAI/core/gallery/importers"
17-
httpUtils "github.com/mudler/LocalAI/core/http/utils"
17+
httpUtils "github.com/mudler/LocalAI/core/http/middleware"
1818
"github.com/mudler/LocalAI/core/schema"
1919
"github.com/mudler/LocalAI/core/services"
2020
"github.com/mudler/LocalAI/pkg/utils"

core/http/endpoints/localai/video.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10+
"net/url"
1011
"os"
1112
"path/filepath"
1213
"strings"
1314
"time"
1415

15-
"github.com/labstack/echo/v4"
1616
"github.com/google/uuid"
17+
"github.com/labstack/echo/v4"
1718
"github.com/mudler/LocalAI/core/config"
1819
"github.com/mudler/LocalAI/core/http/middleware"
19-
"github.com/mudler/LocalAI/core/http/utils"
2020
"github.com/mudler/LocalAI/core/schema"
2121

2222
"github.com/mudler/LocalAI/core/backend"
@@ -165,7 +165,7 @@ func VideoEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfi
165165
return err
166166
}
167167

168-
baseURL := utils.BaseURL(c)
168+
baseURL := middleware.BaseURL(c)
169169

170170
fn, err := backend.VideoGeneration(
171171
height,
@@ -202,7 +202,10 @@ func VideoEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfi
202202
item.B64JSON = base64.StdEncoding.EncodeToString(data)
203203
} else {
204204
base := filepath.Base(output)
205-
item.URL = baseURL + "/generated-videos/" + base
205+
item.URL, err = url.JoinPath(baseURL, "generated-videos", base)
206+
if err != nil {
207+
return err
208+
}
206209
}
207210

208211
id := uuid.New().String()

core/http/endpoints/localai/welcome.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/labstack/echo/v4"
77
"github.com/mudler/LocalAI/core/config"
88
"github.com/mudler/LocalAI/core/gallery"
9-
"github.com/mudler/LocalAI/core/http/utils"
9+
"github.com/mudler/LocalAI/core/http/middleware"
1010
"github.com/mudler/LocalAI/core/services"
1111
"github.com/mudler/LocalAI/internal"
1212
"github.com/mudler/LocalAI/pkg/model"
@@ -45,7 +45,7 @@ func WelcomeEndpoint(appConfig *config.ApplicationConfig,
4545
summary := map[string]interface{}{
4646
"Title": "LocalAI API - " + internal.PrintableVersion(),
4747
"Version": internal.PrintableVersion(),
48-
"BaseURL": utils.BaseURL(c),
48+
"BaseURL": middleware.BaseURL(c),
4949
"Models": modelsWithoutConfig,
5050
"ModelsConfig": modelConfigs,
5151
"GalleryConfig": galleryConfigs,
@@ -58,7 +58,9 @@ func WelcomeEndpoint(appConfig *config.ApplicationConfig,
5858

5959
contentType := c.Request().Header.Get("Content-Type")
6060
accept := c.Request().Header.Get("Accept")
61-
if strings.Contains(contentType, "application/json") || !strings.Contains(accept, "text/html") {
61+
// Default to HTML if Accept header is empty (browser behavior)
62+
// Only return JSON if explicitly requested or Content-Type is application/json
63+
if strings.Contains(contentType, "application/json") || (accept != "" && !strings.Contains(accept, "text/html")) {
6264
// The client expects a JSON response
6365
return c.JSON(200, summary)
6466
} else {

core/http/endpoints/openai/image.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10+
"net/url"
1011
"os"
1112
"path/filepath"
1213
"strconv"
1314
"strings"
1415
"time"
1516

16-
"github.com/labstack/echo/v4"
1717
"github.com/google/uuid"
18+
"github.com/labstack/echo/v4"
1819
"github.com/mudler/LocalAI/core/config"
1920
"github.com/mudler/LocalAI/core/http/middleware"
20-
"github.com/mudler/LocalAI/core/http/utils"
2121
"github.com/mudler/LocalAI/core/schema"
2222

2323
"github.com/mudler/LocalAI/core/backend"
@@ -189,7 +189,7 @@ func ImageEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfi
189189
return err
190190
}
191191

192-
baseURL := utils.BaseURL(c)
192+
baseURL := middleware.BaseURL(c)
193193

194194
// Use the first input image as src if available, otherwise use the original src
195195
inputSrc := src
@@ -216,7 +216,10 @@ func ImageEndpoint(cl *config.ModelConfigLoader, ml *model.ModelLoader, appConfi
216216
item.B64JSON = base64.StdEncoding.EncodeToString(data)
217217
} else {
218218
base := filepath.Base(output)
219-
item.URL = baseURL + "/generated-images/" + base
219+
item.URL, err = url.JoinPath(baseURL, "generated-images", base)
220+
if err != nil {
221+
return err
222+
}
220223
}
221224

222225
result = append(result, *item)

core/http/middleware/auth.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/labstack/echo/v4"
1010
"github.com/labstack/echo/v4/middleware"
1111
"github.com/mudler/LocalAI/core/config"
12-
"github.com/mudler/LocalAI/core/http/utils"
1312
"github.com/mudler/LocalAI/core/schema"
1413
)
1514

@@ -115,7 +114,7 @@ func getApiKeyErrorHandler(applicationConfig *config.ApplicationConfig) func(err
115114
}
116115

117116
return c.Render(http.StatusUnauthorized, "views/login", map[string]interface{}{
118-
"BaseURL": utils.BaseURL(c),
117+
"BaseURL": BaseURL(c),
119118
})
120119
}
121120
if applicationConfig.OpaqueErrors {

0 commit comments

Comments
 (0)