mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 21:47:20 +00:00
Resolves #40396. No changes file because there should be no user visible changes. ## Testing - [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))
65 lines
2.1 KiB
Go
65 lines
2.1 KiB
Go
package client
|
|
|
|
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)
|
|
})
|
|
}
|
|
}
|