47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package apivalidate
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/ultisuite/ulti-backend/internal/api/apiresponse"
|
|
"github.com/ultisuite/ulti-backend/internal/api/query"
|
|
)
|
|
|
|
func TestDecodeJSONInvalidBody(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodPost, "/", bytes.NewBufferString("not-json"))
|
|
rec := httptest.NewRecorder()
|
|
|
|
err := DecodeJSON(rec, req, 1024, &struct{}{})
|
|
if err == nil {
|
|
t.Fatal("expected decode error")
|
|
}
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d", rec.Code)
|
|
}
|
|
|
|
var body apiresponse.ErrorBody
|
|
_ = json.NewDecoder(rec.Body).Decode(&body)
|
|
if body.Code != apiresponse.CodeInvalidRequest {
|
|
t.Fatalf("code = %q", body.Code)
|
|
}
|
|
}
|
|
|
|
func TestWriteQueryError(t *testing.T) {
|
|
req := httptest.NewRequest(http.MethodGet, "/?page=0", nil)
|
|
rec := httptest.NewRecorder()
|
|
|
|
_, err := query.ParseListRequest(req)
|
|
if err == nil {
|
|
t.Fatal("expected query error")
|
|
}
|
|
WriteQueryError(rec, req, err)
|
|
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("status = %d", rec.Code)
|
|
}
|
|
}
|