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 {
|
func getTask(c *gin.Context, id string) error {
|
||||||
id, err := downloader.ExtractVideoID()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
task, ok := tasks[id]
|
task, ok := tasks[id]
|
||||||
if ok {
|
if ok {
|
||||||
c.JSON(http.StatusOK, task)
|
c.JSON(http.StatusOK, task)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,8 +92,8 @@ func main() {
|
||||||
|
|
||||||
r.POST("/task", createTask)
|
r.POST("/task", createTask)
|
||||||
|
|
||||||
r.GET("/status", func(c *gin.Context) {
|
r.GET("/task/:id", func(c *gin.Context) {
|
||||||
if err := status(c, youtube_dl.NewYoutubeDlDownloader(c.Query("url"), "yt-dlp")); err != nil {
|
if err := getTask(c, c.Param("id")); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -8,6 +8,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.andreafazzi.eu/andrea/youtube-dl-service/task"
|
||||||
|
"git.andreafazzi.eu/andrea/youtube-dl-service/youtube"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/remogatto/prettytest"
|
"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
|
// Test the creation of a new task. A new task is created with a POST
|
||||||
// request to the endpoint.
|
// request to the endpoint.
|
||||||
func (t *testSuite) TestCreateTask() {
|
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
|
// Set form values
|
||||||
form := url.Values{}
|
form := url.Values{}
|
||||||
form.Add("url", "https://www.youtube.com/watch?v=AVIBLFl28vo")
|
form.Add("url", ytUrl)
|
||||||
|
|
||||||
// Create a new request
|
// Create a new request
|
||||||
req, err := http.NewRequest("POST", "/task", strings.NewReader(form.Encode()))
|
req, err := http.NewRequest("POST", "/task", strings.NewReader(form.Encode()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
@ -49,13 +91,11 @@ func (t *testSuite) TestCreateTask() {
|
||||||
createTask(c)
|
createTask(c)
|
||||||
|
|
||||||
// Read the response and assert it.
|
// Read the response and assert it.
|
||||||
var data struct {
|
video := new(youtube.Video)
|
||||||
Id string
|
|
||||||
|
if err := json.Unmarshal(rr.Body.Bytes(), &video); err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.Unmarshal(rr.Body.Bytes(), &data); err != nil {
|
return video, nil
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Equal("AVIBLFl28vo", data.Id)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ const (
|
||||||
type Tasks map[string]*Task
|
type Tasks map[string]*Task
|
||||||
|
|
||||||
type Task struct {
|
type Task struct {
|
||||||
Id string
|
ID string
|
||||||
Status int
|
Status int
|
||||||
Filename string
|
Filename string
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ func (d *YoutubeDlDownloader) GetVideo() (*youtube.Video, error) {
|
||||||
|
|
||||||
func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tasks) {
|
func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tasks) {
|
||||||
tasks[video.ID] = &task.Task{
|
tasks[video.ID] = &task.Task{
|
||||||
|
ID: video.ID,
|
||||||
Status: task.StatusDownloading,
|
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)
|
log.Printf("Download of video ID %s COMPLETED.", video.ID)
|
||||||
tasks[video.ID] = &task.Task{
|
tasks[video.ID] = &task.Task{
|
||||||
Id: video.ID,
|
ID: video.ID,
|
||||||
Status: task.StatusCompleted,
|
Status: task.StatusCompleted,
|
||||||
Filename: video.Filename,
|
Filename: video.Filename,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue