youtube-dl-service/backend/main_test.go

61 lines
1.1 KiB
Go

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)
}