move tests to dedicated package
This commit is contained in:
43
internal/apitest/post_book_read_test.go
Normal file
43
internal/apitest/post_book_read_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package apitest
|
||||
|
||||
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.StatusNotFound)
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user