82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
package main_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
home "source.toby3d.me/toby3d/home"
|
|
"source.toby3d.me/toby3d/home/internal/common"
|
|
"source.toby3d.me/toby3d/home/internal/domain"
|
|
)
|
|
|
|
func TestApp(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ln, err := net.Listen("tcp", "0.0.0.0:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = ln.Close() })
|
|
|
|
addr, _ := ln.Addr().(*net.TCPAddr)
|
|
t.Logf("running testing app on %s port", addr)
|
|
|
|
testConfig := domain.TestConfig(t)
|
|
testConfig.Host = addr.IP.String()
|
|
testConfig.Port = uint16(addr.Port)
|
|
|
|
ctx := context.Background()
|
|
|
|
app, err := home.NewApp(ctx, testConfig)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
go app.Run(ln)
|
|
t.Cleanup(func() { _ = app.Stop(ctx) })
|
|
|
|
for name, tc := range map[string]struct {
|
|
AcceptLanguage string
|
|
}{
|
|
"personal": {AcceptLanguage: "ru,en;q=0.7,eo;q=0.3"},
|
|
"mastodon-long": {AcceptLanguage: "en-GB,en-US;q=0.9,en;q=0.8,ru;q=0.7"},
|
|
"mastodon-short": {AcceptLanguage: "en-US,en;q=0.5"},
|
|
} {
|
|
name, tc := name, tc
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
req, err := http.NewRequest(http.MethodGet,
|
|
fmt.Sprintf("http://localhost:%d/", testConfig.Port), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
req.Header.Set(common.HeaderAcceptLanguage, tc.AcceptLanguage)
|
|
|
|
client := http.Client{Timeout: 500 * time.Millisecond}
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
t.Fatal("cannot make request:", err)
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if content := string(body); strings.Contains(content, "invalid argument") {
|
|
t.Errorf("response body contains error:\n%s", content)
|
|
}
|
|
})
|
|
}
|
|
}
|