43 lines
1 KiB
Go
43 lines
1 KiB
Go
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 := "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<data><text>test</text></data>"
|
|
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)
|
|
}
|