fleet/server/service/base_client_errors_test.go

66 lines
2.1 KiB
Go
Raw Normal View History

Improved orbit debug logs when response contains a large HTML page. (#33195) Resolves #33219 Note: this only fixes orbit. The issue remains on osquery: [#33019](https://github.com/fleetdm/fleet/issues/33019) # Checklist for submitter - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually ## fleetd/orbit/Fleet Desktop - [x] Verified compatibility with the latest released version of Fleet (see [Must rule](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/workflows/fleetd-development-and-release-strategy.md)) - [x] Verified that fleetd runs on macOS, Linux and Windows - [x] Verified auto-update works from the released version of component to the new version (see [tools/tuf/test](../tools/tuf/test/README.md)) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - Bug Fixes - Improved error messages when servers return HTML instead of JSON. - Truncates oversized responses in logs to prevent overwhelming output while preserving context. - More robust parsing of non-JSON error responses. - Documentation - Added changelog entry noting enhanced debug logging for large HTML responses. - Tests - Added tests covering HTML, plain text, empty, long, and invalid JSON error bodies to validate error message handling. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-09-19 22:00:19 +00:00
package service
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestExtractServerErrorText(t *testing.T) {
tests := []struct {
name string
body string
expected string
}{
{
name: "valid JSON error",
body: `{"message": "Something went wrong", "errors": [{"name": "error1", "reason": "invalid input"}]}`,
expected: "Something went wrong: invalid input",
},
{
name: "403 Forbidden HTML",
body: `<!DOCTYPE html><html><head><title>403 Forbidden</title></head><body><h1>403 Forbidden</h1><p>You don't have permission to access this resource.</p></body></html>`,
expected: "server returned HTML instead of JSON response, body: <!DOCTYPE html><html><head><title>403 Forbidden</title></head><body><h1>403 Forbidden</h1><p>You don't have permission to access this resource.</p></body></html>",
},
{
name: "HTML with uppercase tags",
body: `<HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>Server Error</BODY></HTML>`,
expected: "server returned HTML instead of JSON response, body: <HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY>Server Error</BODY></HTML>",
},
{
name: "long HTML gets truncated",
body: `<!DOCTYPE html><html><head><title>Error Page</title></head><body>` + strings.Repeat("A", 200) + `</body></html>`,
expected: "server returned HTML instead of JSON response, body: <!DOCTYPE html><html><head><title>Error Page</title></head><body>" + strings.Repeat("A", 135) + "...",
},
{
name: "plain text error",
body: "Connection refused",
expected: "Connection refused",
},
{
name: "empty response",
body: "",
expected: "empty response body",
},
{
name: "long plain text truncated",
body: strings.Repeat("a", 250),
expected: strings.Repeat("a", 200) + "...",
},
{
name: "invalid JSON",
body: `{invalid json}`,
expected: "{invalid json}",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reader := strings.NewReader(tt.body)
result := extractServerErrorText(reader)
assert.Equal(t, tt.expected, result)
})
}
}