Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
37 lines
881 B
Go
37 lines
881 B
Go
package xhttp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"git.cw.tr/mukan-network/mukan-ignite/ignite/pkg/errors"
|
|
)
|
|
|
|
func TestResponseJSON(t *testing.T) {
|
|
w := httptest.NewRecorder()
|
|
data := map[string]interface{}{"a": 1}
|
|
require.NoError(t, ResponseJSON(w, http.StatusCreated, data))
|
|
resp := w.Result()
|
|
defer resp.Body.Close() // Ensure the response body is closed
|
|
|
|
require.Equal(t, http.StatusCreated, resp.StatusCode)
|
|
require.Equal(t, "application/json", resp.Header.Get("Content-Type"))
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
dataJSON, err := json.Marshal(data)
|
|
require.NoError(t, err)
|
|
require.Equal(t, dataJSON, body)
|
|
}
|
|
|
|
func TestNewErrorResponse(t *testing.T) {
|
|
require.Equal(t, ErrorResponseBody{
|
|
Error: ErrorResponse{
|
|
Message: "error",
|
|
},
|
|
}, NewErrorResponse(errors.New("error")))
|
|
}
|