mirror of
https://github.com/ollama/ollama.git
synced 2025-11-13 14:57:50 +01:00
non-json response tests for client do
This commit is contained in:
@@ -55,6 +55,7 @@ func TestClientFromEnvironment(t *testing.T) {
|
|||||||
type testError struct {
|
type testError struct {
|
||||||
message string
|
message string
|
||||||
statusCode int
|
statusCode int
|
||||||
|
raw bool // if true, write message as-is instead of JSON encoding
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e testError) Error() string {
|
func (e testError) Error() string {
|
||||||
@@ -196,6 +197,7 @@ func TestClientDo(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
response any
|
response any
|
||||||
wantErr string
|
wantErr string
|
||||||
|
wantStatusCode int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "immediate error response",
|
name: "immediate error response",
|
||||||
@@ -204,6 +206,7 @@ func TestClientDo(t *testing.T) {
|
|||||||
statusCode: http.StatusBadRequest,
|
statusCode: http.StatusBadRequest,
|
||||||
},
|
},
|
||||||
wantErr: "test error message",
|
wantErr: "test error message",
|
||||||
|
wantStatusCode: http.StatusBadRequest,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "server error response",
|
name: "server error response",
|
||||||
@@ -212,6 +215,7 @@ func TestClientDo(t *testing.T) {
|
|||||||
statusCode: http.StatusInternalServerError,
|
statusCode: http.StatusInternalServerError,
|
||||||
},
|
},
|
||||||
wantErr: "internal error",
|
wantErr: "internal error",
|
||||||
|
wantStatusCode: http.StatusInternalServerError,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "successful response",
|
name: "successful response",
|
||||||
@@ -223,6 +227,26 @@ func TestClientDo(t *testing.T) {
|
|||||||
Success: true,
|
Success: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "plain text error response",
|
||||||
|
response: testError{
|
||||||
|
message: "internal server error",
|
||||||
|
statusCode: http.StatusInternalServerError,
|
||||||
|
raw: true,
|
||||||
|
},
|
||||||
|
wantErr: "internal server error",
|
||||||
|
wantStatusCode: http.StatusInternalServerError,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "HTML error page",
|
||||||
|
response: testError{
|
||||||
|
message: "<html><body>404 Not Found</body></html>",
|
||||||
|
statusCode: http.StatusNotFound,
|
||||||
|
raw: true,
|
||||||
|
},
|
||||||
|
wantErr: "<html><body>404 Not Found</body></html>",
|
||||||
|
wantStatusCode: http.StatusNotFound,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
@@ -230,12 +254,17 @@ func TestClientDo(t *testing.T) {
|
|||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if errResp, ok := tc.response.(testError); ok {
|
if errResp, ok := tc.response.(testError); ok {
|
||||||
w.WriteHeader(errResp.statusCode)
|
w.WriteHeader(errResp.statusCode)
|
||||||
|
if !errResp.raw {
|
||||||
err := json.NewEncoder(w).Encode(map[string]string{
|
err := json.NewEncoder(w).Encode(map[string]string{
|
||||||
"error": errResp.message,
|
"error": errResp.message,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("failed to encode error response:", err)
|
t.Fatal("failed to encode error response:", err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Write raw message (simulates non-JSON error responses)
|
||||||
|
fmt.Fprint(w, errResp.message)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,6 +290,15 @@ func TestClientDo(t *testing.T) {
|
|||||||
if err.Error() != tc.wantErr {
|
if err.Error() != tc.wantErr {
|
||||||
t.Errorf("error message mismatch: got %q, want %q", err.Error(), tc.wantErr)
|
t.Errorf("error message mismatch: got %q, want %q", err.Error(), tc.wantErr)
|
||||||
}
|
}
|
||||||
|
if tc.wantStatusCode != 0 {
|
||||||
|
if statusErr, ok := err.(StatusError); ok {
|
||||||
|
if statusErr.StatusCode != tc.wantStatusCode {
|
||||||
|
t.Errorf("status code mismatch: got %d, want %d", statusErr.StatusCode, tc.wantStatusCode)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Errorf("expected StatusError, got %T", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user