package middleware import ( "encoding/xml" "net/http/httptest" "testing" "github.com/stretchr/testify/require" ) type testXMLData struct { XMLName xml.Name `xml:"data"` Text string `xml:"text"` } func TestEncodeResponse(t *testing.T) { w := httptest.NewRecorder() err := EncodeToResponse(w, []byte{}) require.Error(t, err) require.Contains(t, err.Error(), "encode xml response") err = EncodeToResponse(w, testXMLData{Text: "test"}) require.NoError(t, err) expectedXML := "\n\ntest" require.Equal(t, expectedXML, w.Body.String()) } func TestErrorResponse(t *testing.T) { errResp := ErrorResponse{Code: "invalid-code"} actual := errResp.Error() require.Contains(t, actual, "Error response code") errResp.Code = "AccessDenied" actual = errResp.Error() require.Equal(t, "Access Denied.", actual) errResp.Message = "Request body is empty." actual = errResp.Error() require.Equal(t, "Request body is empty.", actual) }