Working on backend tests
This commit is contained in:
parent
0420cc0552
commit
1b2017b3ae
4 changed files with 55 additions and 19 deletions
|
@ -43,16 +43,11 @@ func createTask(c *gin.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
func status(c *gin.Context, downloader youtube.Downloader) error {
|
||||
id, err := downloader.ExtractVideoID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
func getTask(c *gin.Context, id string) error {
|
||||
task, ok := tasks[id]
|
||||
if ok {
|
||||
c.JSON(http.StatusOK, task)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -97,8 +92,8 @@ func main() {
|
|||
|
||||
r.POST("/task", createTask)
|
||||
|
||||
r.GET("/status", func(c *gin.Context) {
|
||||
if err := status(c, youtube_dl.NewYoutubeDlDownloader(c.Query("url"), "yt-dlp")); err != nil {
|
||||
r.GET("/task/:id", func(c *gin.Context) {
|
||||
if err := getTask(c, c.Param("id")); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
|
|
|
@ -8,6 +8,8 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.andreafazzi.eu/andrea/youtube-dl-service/task"
|
||||
"git.andreafazzi.eu/andrea/youtube-dl-service/youtube"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/remogatto/prettytest"
|
||||
)
|
||||
|
@ -27,14 +29,54 @@ func TestRunner(t *testing.T) {
|
|||
// Test the creation of a new task. A new task is created with a POST
|
||||
// request to the endpoint.
|
||||
func (t *testSuite) TestCreateTask() {
|
||||
video, err := postTask("https://www.youtube.com/watch?v=AVIBLFl28vo")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
t.Equal("AVIBLFl28vo", video.ID)
|
||||
}
|
||||
|
||||
// Test the response of a GET /tasks/:id requests.
|
||||
func (t *testSuite) TestGetTask() {
|
||||
video, err := postTask("https://www.youtube.com/watch?v=AVIBLFl28vo")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Create a new request
|
||||
req, err := http.NewRequest("GET", "/task/"+video.ID, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Create an http recorder
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
// Set the context and call the handler
|
||||
c, _ := gin.CreateTestContext(rr)
|
||||
c.Request = req
|
||||
|
||||
getTask(c, video.ID)
|
||||
|
||||
// Read the response and assert it.
|
||||
task := new(task.Task)
|
||||
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &task); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
t.Equal("AVIBLFl28vo", task.ID)
|
||||
}
|
||||
|
||||
func postTask(ytUrl string) (*youtube.Video, error) {
|
||||
// Set form values
|
||||
form := url.Values{}
|
||||
form.Add("url", "https://www.youtube.com/watch?v=AVIBLFl28vo")
|
||||
form.Add("url", ytUrl)
|
||||
|
||||
// Create a new request
|
||||
req, err := http.NewRequest("POST", "/task", strings.NewReader(form.Encode()))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
@ -49,13 +91,11 @@ func (t *testSuite) TestCreateTask() {
|
|||
createTask(c)
|
||||
|
||||
// Read the response and assert it.
|
||||
var data struct {
|
||||
Id string
|
||||
video := new(youtube.Video)
|
||||
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &video); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &data); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
t.Equal("AVIBLFl28vo", data.Id)
|
||||
return video, nil
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ const (
|
|||
type Tasks map[string]*Task
|
||||
|
||||
type Task struct {
|
||||
Id string
|
||||
ID string
|
||||
Status int
|
||||
Filename string
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ func (d *YoutubeDlDownloader) GetVideo() (*youtube.Video, error) {
|
|||
|
||||
func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tasks) {
|
||||
tasks[video.ID] = &task.Task{
|
||||
ID: video.ID,
|
||||
Status: task.StatusDownloading,
|
||||
}
|
||||
|
||||
|
@ -51,7 +52,7 @@ func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tas
|
|||
|
||||
log.Printf("Download of video ID %s COMPLETED.", video.ID)
|
||||
tasks[video.ID] = &task.Task{
|
||||
Id: video.ID,
|
||||
ID: video.ID,
|
||||
Status: task.StatusCompleted,
|
||||
Filename: video.Filename,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue