Files
bibliomane/searchbook_test.go
2025-10-14 00:29:53 +02:00

38 lines
802 B
Go

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
"github.com/stretchr/testify/assert"
)
type bookSearchGet struct {
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
}
func TestSearchBook(t *testing.T) {
router := testutils.TestSetup()
token := testutils.ConnectDemoUser(router)
req, _ := http.NewRequest("GET", "/search/san", nil)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
var books []bookSearchGet
err := json.Unmarshal(w.Body.Bytes(), &books)
if err != nil {
log.Fatal(err)
}
assert.Equal(t, 200, w.Code)
assert.Equal(t, 2, len(books))
}