mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-24 22:06:57 +00:00
start server for integration tests in code
This commit is contained in:
parent
94bc8c854e
commit
a961655d2c
3 changed files with 102 additions and 73 deletions
|
@ -30,14 +30,4 @@ pipeline:
|
||||||
image: golang:1.18
|
image: golang:1.18
|
||||||
detach: true
|
detach: true
|
||||||
commands:
|
commands:
|
||||||
- go run . --verbose &
|
|
||||||
- pid=$!
|
|
||||||
- sleep 10s
|
|
||||||
- go test -tags integration codeberg.org/codeberg/pages/integration/...
|
- go test -tags integration codeberg.org/codeberg/pages/integration/...
|
||||||
- kill $pid
|
|
||||||
environment:
|
|
||||||
- ACME_API=https://acme.mock.directory
|
|
||||||
- ACME_ACCEPT_TERMS=true
|
|
||||||
- PAGES_DOMAIN=localhost.mock.directory
|
|
||||||
- RAW_DOMAIN=raw.localhost.mock.directory
|
|
||||||
- PORT=4430
|
|
||||||
|
|
69
integration/get_test.go
Normal file
69
integration/get_test.go
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
//go:build integration
|
||||||
|
// +build integration
|
||||||
|
|
||||||
|
package integration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"net/http/cookiejar"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetRedirect(t *testing.T) {
|
||||||
|
log.Println("== TestGetRedirect ==")
|
||||||
|
// test custom domain redirect
|
||||||
|
resp, err := getTestHTTPSClient().Get("https://calciumdibromid.localhost.mock.directory:4430")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
if !assert.EqualValues(t, http.StatusTemporaryRedirect, resp.StatusCode) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
assert.EqualValues(t, "https://www.cabr2.de/", resp.Header["Location"][0])
|
||||||
|
assert.EqualValues(t, 0, getSize(resp.Body))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetContent(t *testing.T) {
|
||||||
|
log.Println("== TestGetContent ==")
|
||||||
|
// test get image
|
||||||
|
resp, err := getTestHTTPSClient().Get("https://magiclike.localhost.mock.directory:4430/images/827679288a.jpg")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
if !assert.EqualValues(t, http.StatusOK, resp.StatusCode) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
assert.EqualValues(t, "image/jpeg", resp.Header["Content-Type"][0])
|
||||||
|
assert.EqualValues(t, "124635", resp.Header["Content-Length"][0])
|
||||||
|
assert.EqualValues(t, 124635, getSize(resp.Body))
|
||||||
|
|
||||||
|
// specify branch
|
||||||
|
resp, err = getTestHTTPSClient().Get("https://momar.localhost.mock.directory:4430/pag/@master/")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
if !assert.EqualValues(t, http.StatusOK, resp.StatusCode) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
assert.EqualValues(t, "text/html; charset=utf-8", resp.Header["Content-Type"][0])
|
||||||
|
assert.True(t, getSize(resp.Body) > 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTestHTTPSClient() *http.Client {
|
||||||
|
cookieJar, _ := cookiejar.New(nil)
|
||||||
|
return &http.Client{
|
||||||
|
Jar: cookieJar,
|
||||||
|
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
|
||||||
|
return http.ErrUseLastResponse
|
||||||
|
},
|
||||||
|
Transport: &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSize(stream io.Reader) int {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
buf.ReadFrom(stream)
|
||||||
|
return buf.Len()
|
||||||
|
}
|
|
@ -1,78 +1,48 @@
|
||||||
//go:build integration
|
|
||||||
// +build integration
|
|
||||||
|
|
||||||
package integration
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"context"
|
||||||
"crypto/tls"
|
"log"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"net/http/cookiejar"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"codeberg.org/codeberg/pages/cmd"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
func TestMain(m *testing.M) {
|
||||||
// if enableRunGitea() {
|
log.Println("=== TestMain: START Server ==")
|
||||||
// p, err := runGitea()
|
serverCtx, serverCancel := context.WithCancel(context.Background())
|
||||||
// if err != nil {
|
if err := startServer(serverCtx); err != nil {
|
||||||
// log.Fatal(err)
|
log.Fatalf("could not start server: %v", err)
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// defer func() {
|
|
||||||
// p.Kill()
|
|
||||||
// }()
|
|
||||||
// }
|
|
||||||
// log.Printf("testing with %v, %v, %v\n", getGiteaURL(), getGiteaUsername(), getGiteaPassword())
|
|
||||||
// exitCode := m.Run()
|
|
||||||
// os.Exit(exitCode)
|
|
||||||
|
|
||||||
// test custom domain redirect
|
|
||||||
resp, err := getTestHTTPSClient().Get("https://calciumdibromid.localhost.mock.directory:4430")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
if !assert.EqualValues(t, http.StatusTemporaryRedirect, resp.StatusCode) {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
}
|
||||||
assert.EqualValues(t, "https://www.cabr2.de/", resp.Header["Location"][0])
|
defer func() {
|
||||||
assert.EqualValues(t, 0, getSize(resp.Body))
|
serverCancel()
|
||||||
|
log.Println("=== TestMain: Server STOPED ==")
|
||||||
|
}()
|
||||||
|
|
||||||
// test get image
|
time.Sleep(30 * time.Second)
|
||||||
resp, err = getTestHTTPSClient().Get("https://magiclike.localhost.mock.directory:4430/images/827679288a.jpg")
|
|
||||||
assert.NoError(t, err)
|
m.Run()
|
||||||
if !assert.EqualValues(t, http.StatusOK, resp.StatusCode) {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
assert.EqualValues(t, "image/jpeg", resp.Header["Content-Type"][0])
|
|
||||||
assert.EqualValues(t, "124635", resp.Header["Content-Length"][0])
|
|
||||||
assert.EqualValues(t, 124635, getSize(resp.Body))
|
|
||||||
|
|
||||||
// specify branch
|
|
||||||
resp, err = getTestHTTPSClient().Get("https://momar.localhost.mock.directory:4430/pag/@master/")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
if !assert.EqualValues(t, http.StatusOK, resp.StatusCode) {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
assert.EqualValues(t, "text/html; charset=utf-8", resp.Header["Content-Type"][0])
|
|
||||||
assert.True(t, getSize(resp.Body) > 1000)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTestHTTPSClient() *http.Client {
|
func startServer(ctx context.Context) error {
|
||||||
cookieJar, _ := cookiejar.New(nil)
|
args := []string{
|
||||||
return &http.Client{
|
"--verbose",
|
||||||
Jar: cookieJar,
|
"--acme-accept-terms", "true",
|
||||||
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
|
"--acme-api-endpoint", "https://acme.mock.directory",
|
||||||
return http.ErrUseLastResponse
|
"--pages-domain", "localhost.mock.directory",
|
||||||
},
|
"--raw-domain", "raw.localhost.mock.directory",
|
||||||
Transport: &http.Transport{
|
"--port", "4430",
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func getSize(stream io.Reader) int {
|
app := cli.NewApp()
|
||||||
buf := new(bytes.Buffer)
|
app.Name = "pages-server"
|
||||||
buf.ReadFrom(stream)
|
app.Action = cmd.Serve
|
||||||
return buf.Len()
|
app.Flags = cmd.ServeFlags
|
||||||
|
|
||||||
|
go app.RunContext(ctx, args)
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue