jokes-bapak2/api/app/v1/handler/joke/joke_get_test.go

118 lines
2.4 KiB
Go
Raw Normal View History

package joke_test
import (
"io/ioutil"
"net/http"
"testing"
"time"
_ "github.com/joho/godotenv/autoload"
"github.com/stretchr/testify/assert"
)
2021-08-04 05:56:14 +00:00
/// Need to find some workaround for this test
func TestTodayJoke(t *testing.T) {
err := setup()
if err != nil {
t.Fatal(err)
}
defer cleanup()
req, _ := http.NewRequest("GET", "/today", nil)
res, err := app.Test(req, int(time.Minute * 2))
if err != nil {
t.Fatal(err)
}
2021-08-04 05:56:14 +00:00
assert.Equalf(t, false, err != nil, "today joke")
assert.Equalf(t, 200, res.StatusCode, "today joke")
assert.NotEqualf(t, 0, res.ContentLength, "today joke")
_, err = ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
2021-08-04 05:56:14 +00:00
assert.Nilf(t, err, "today joke")
}
func TestSingleJoke(t *testing.T) {
err := setup()
if err != nil {
t.Fatal(err)
}
2021-08-04 05:56:14 +00:00
defer cleanup()
req, _ := http.NewRequest("GET", "/", nil)
res, err := app.Test(req, int(time.Minute * 2))
if err != nil {
t.Fatal(err)
}
2021-08-04 05:56:14 +00:00
assert.Equalf(t, false, err != nil, "single joke")
assert.Equalf(t, 200, res.StatusCode, "single joke")
assert.NotEqualf(t, 0, res.ContentLength, "single joke")
_, err = ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
2021-08-04 05:56:14 +00:00
assert.Nilf(t, err, "single joke")
}
func TestJokeByID_200(t *testing.T) {
err := setup()
if err != nil {
t.Fatal(err)
}
2021-08-04 05:56:14 +00:00
defer cleanup()
req, _ := http.NewRequest("GET", "/id/1", nil)
res, err := app.Test(req, int(time.Minute * 2))
if err != nil {
t.Fatal(err)
}
2021-08-04 05:56:14 +00:00
assert.Equalf(t, false, err != nil, "joke by id")
assert.Equalf(t, 200, res.StatusCode, "joke by id")
assert.NotEqualf(t, 0, res.ContentLength, "joke by id")
_, err = ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
2021-08-04 05:56:14 +00:00
assert.Nilf(t, err, "joke by id")
}
func TestJokeByID_404(t *testing.T) {
err := setup()
if err != nil {
t.Fatal(err)
}
2021-08-04 05:56:14 +00:00
defer cleanup()
req, _ := http.NewRequest("GET", "/id/300", nil)
res, err := app.Test(req, int(time.Minute * 2))
if err != nil {
t.Fatal(err)
}
2021-08-04 05:56:14 +00:00
assert.Equalf(t, false, err != nil, "joke by id")
assert.Equalf(t, 404, res.StatusCode, "joke by id")
assert.NotEqualf(t, 0, res.ContentLength, "joke by id")
body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
2021-08-04 05:56:14 +00:00
assert.Nilf(t, err, "joke by id")
assert.Equalf(t, "Requested ID was not found.", string(body), "joke by id")
}