mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 21:39:40 +02:00
44 lines
782 B
Go
44 lines
782 B
Go
|
package request_test
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
"net/http/httptest"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/portainer/portainer/pkg/libhttp/request"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
type requestPayload struct {
|
||
|
FirstName string `json:"first_name"`
|
||
|
LastName string `json:"last_name"`
|
||
|
}
|
||
|
|
||
|
func (p *requestPayload) Validate(r *http.Request) error {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func Test_GetPayload(t *testing.T) {
|
||
|
|
||
|
payload := requestPayload{
|
||
|
FirstName: "John",
|
||
|
LastName: "Doe",
|
||
|
}
|
||
|
|
||
|
payloadJSON, err := json.Marshal(payload)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
r := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(payloadJSON))
|
||
|
newPayload, err := request.GetPayload[requestPayload](r)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
assert.Equal(t, payload, *newPayload)
|
||
|
}
|