44 lines
972 B
Go
44 lines
972 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestPostBookReadHandler_Ok(t *testing.T) {
|
|
userBookJson :=
|
|
`{
|
|
"bookId": 6
|
|
}`
|
|
testPostBookReadHandler(t, userBookJson, http.StatusOK)
|
|
}
|
|
|
|
func TestPostBookReadHandler_IDDoesNotExist(t *testing.T) {
|
|
userBookJson :=
|
|
`{
|
|
"bookId": 46546
|
|
}`
|
|
testPostBookReadHandler(t, userBookJson, http.StatusBadRequest)
|
|
}
|
|
|
|
func testPostBookReadHandler(t *testing.T, userBookJson string, expectedCode int) {
|
|
router := testutils.TestSetup()
|
|
w := httptest.NewRecorder()
|
|
|
|
token := testutils.ConnectDemo2User(router)
|
|
req, _ := http.NewRequest("POST", "/book/read",
|
|
strings.NewReader(string(userBookJson)))
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
router.ServeHTTP(w, req)
|
|
log.Printf("%s\n", w.Body.String())
|
|
assert.Equal(t, expectedCode, w.Code)
|
|
|
|
}
|