Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
37 lines
870 B
Go
37 lines
870 B
Go
package xhttp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/ignite/cli/v29/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")))
|
|
}
|