2020-07-25 00:55:46 +02:00
|
|
|
package completion
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2021-05-16 22:45:36 +02:00
|
|
|
"net/http"
|
2020-07-25 00:55:46 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"gopkg.in/h2non/gock.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
const samplePackageResp = `
|
|
|
|
# AUR package list, generated on Fri, 24 Jul 2020 22:05:22 GMT
|
|
|
|
cytadela
|
|
|
|
bitefusion
|
|
|
|
globs-svn
|
|
|
|
ri-li
|
|
|
|
globs-benchmarks-svn
|
|
|
|
dunelegacy
|
|
|
|
lumina
|
|
|
|
eternallands-sound
|
|
|
|
`
|
|
|
|
|
|
|
|
const expectPackageCompletion = `cytadela AUR
|
|
|
|
bitefusion AUR
|
|
|
|
globs-svn AUR
|
|
|
|
ri-li AUR
|
|
|
|
globs-benchmarks-svn AUR
|
|
|
|
dunelegacy AUR
|
|
|
|
lumina AUR
|
|
|
|
eternallands-sound AUR
|
|
|
|
`
|
|
|
|
|
|
|
|
func Test_createAURList(t *testing.T) {
|
|
|
|
defer gock.Off()
|
|
|
|
|
|
|
|
gock.New("https://aur.archlinux.org").
|
|
|
|
Get("/packages.gz").
|
|
|
|
Reply(200).
|
|
|
|
BodyString(samplePackageResp)
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-07-25 00:55:46 +02:00
|
|
|
out := &bytes.Buffer{}
|
2021-05-16 22:45:36 +02:00
|
|
|
err := createAURList(&http.Client{}, "https://aur.archlinux.org", out)
|
2020-07-25 00:55:46 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
gotOut := out.String()
|
|
|
|
assert.Equal(t, expectPackageCompletion, gotOut)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_createAURListHTTPError(t *testing.T) {
|
|
|
|
defer gock.Off()
|
|
|
|
|
|
|
|
gock.New("https://aur.archlinux.org").
|
|
|
|
Get("/packages.gz").
|
|
|
|
ReplyError(errors.New("Not available"))
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-07-25 00:55:46 +02:00
|
|
|
out := &bytes.Buffer{}
|
2021-05-16 22:45:36 +02:00
|
|
|
err := createAURList(&http.Client{}, "https://aur.archlinux.org", out)
|
2020-07-25 00:55:46 +02:00
|
|
|
assert.EqualError(t, err, "Get \"https://aur.archlinux.org/packages.gz\": Not available")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_createAURListStatusError(t *testing.T) {
|
|
|
|
defer gock.Off()
|
|
|
|
|
|
|
|
gock.New("https://aur.archlinux.org").
|
|
|
|
Get("/packages.gz").
|
|
|
|
Reply(503).
|
|
|
|
BodyString(samplePackageResp)
|
|
|
|
out := &bytes.Buffer{}
|
2021-05-16 22:45:36 +02:00
|
|
|
err := createAURList(&http.Client{}, "https://aur.archlinux.org", out)
|
2020-07-25 00:55:46 +02:00
|
|
|
assert.EqualError(t, err, "invalid status code: 503")
|
|
|
|
}
|