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: `
403 Forbidden403 Forbidden
You don't have permission to access this resource.
`,
expected: "server returned HTML instead of JSON response, body: 403 Forbidden403 Forbidden
You don't have permission to access this resource.
",
},
{
name: "HTML with uppercase tags",
body: `ErrorServer Error`,
expected: "server returned HTML instead of JSON response, body: ErrorServer Error",
},
{
name: "long HTML gets truncated",
body: `Error Page` + strings.Repeat("A", 200) + ``,
expected: "server returned HTML instead of JSON response, body: Error Page" + 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)
})
}
}