youtube-dl-service/backend/main_test.go

62 lines
1.1 KiB
Go
Raw Normal View History

2021-11-12 08:54:02 +01:00
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/gin-gonic/gin"
"github.com/remogatto/prettytest"
)
// Start of setup
type testSuite struct {
prettytest.Suite
}
func TestRunner(t *testing.T) {
prettytest.Run(
t,
new(testSuite),
)
}
// Test the creation of a new task. A new task is created with a POST
// request to the endpoint.
func (t *testSuite) TestCreateTask() {
// Set form values
form := url.Values{}
form.Add("url", "https://www.youtube.com/watch?v=AVIBLFl28vo")
// Create a new request
req, err := http.NewRequest("POST", "/task", strings.NewReader(form.Encode()))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Create an http recorder
rr := httptest.NewRecorder()
// Set the context and call the handler
c, _ := gin.CreateTestContext(rr)
c.Request = req
createTask(c)
// Read the response and assert it.
var data struct {
Id string
}
if err := json.Unmarshal(rr.Body.Bytes(), &data); err != nil {
panic(err)
}
t.Equal("AVIBLFl28vo", data.Id)
}