2022-06-11 21:17:43 +00:00
|
|
|
//go:build integration
|
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/tls"
|
|
|
|
"io"
|
2022-08-12 03:06:26 +00:00
|
|
|
"log"
|
2022-06-11 21:17:43 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetRedirect(t *testing.T) {
|
2022-08-12 03:06:26 +00:00
|
|
|
log.Println("=== TestGetRedirect ===")
|
2022-06-11 21:17:43 +00:00
|
|
|
// 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()
|
|
|
|
}
|
2022-06-14 16:23:34 +00:00
|
|
|
assert.EqualValues(t, "https://www.cabr2.de/", resp.Header.Get("Location"))
|
2022-06-11 21:17:43 +00:00
|
|
|
assert.EqualValues(t, 0, getSize(resp.Body))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContent(t *testing.T) {
|
2022-08-12 03:06:26 +00:00
|
|
|
log.Println("=== TestGetContent ===")
|
2022-06-11 21:17:43 +00:00
|
|
|
// 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()
|
|
|
|
}
|
2022-06-14 16:23:34 +00:00
|
|
|
assert.EqualValues(t, "image/jpeg", resp.Header.Get("Content-Type"))
|
|
|
|
assert.EqualValues(t, "124635", resp.Header.Get("Content-Length"))
|
2022-06-11 21:17:43 +00:00
|
|
|
assert.EqualValues(t, 124635, getSize(resp.Body))
|
2022-06-14 16:23:34 +00:00
|
|
|
assert.Len(t, resp.Header.Get("ETag"), 42)
|
2022-06-11 21:17:43 +00:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
2022-06-14 16:23:34 +00:00
|
|
|
assert.EqualValues(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type"))
|
2022-06-11 21:17:43 +00:00
|
|
|
assert.True(t, getSize(resp.Body) > 1000)
|
2022-06-14 16:23:34 +00:00
|
|
|
assert.Len(t, resp.Header.Get("ETag"), 42)
|
2022-07-08 11:39:24 +00:00
|
|
|
|
|
|
|
// access branch name contains '/'
|
|
|
|
resp, err = getTestHTTPSClient().Get("https://blumia.localhost.mock.directory:4430/pages-server-integration-tests/@docs~main/")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.EqualValues(t, http.StatusOK, resp.StatusCode) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
assert.EqualValues(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type"))
|
|
|
|
assert.True(t, getSize(resp.Body) > 100)
|
|
|
|
assert.Len(t, resp.Header.Get("ETag"), 42)
|
2022-07-15 19:06:05 +00:00
|
|
|
|
|
|
|
// TODO: test get of non cachable content (content size > fileCacheSizeLimit)
|
2022-06-11 21:17:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 12:43:49 +00:00
|
|
|
func TestCustomDomain(t *testing.T) {
|
2022-08-12 03:06:26 +00:00
|
|
|
log.Println("=== TestCustomDomain ===")
|
2022-06-13 12:43:49 +00:00
|
|
|
resp, err := getTestHTTPSClient().Get("https://mock-pages.codeberg-test.org:4430/README.md")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.EqualValues(t, http.StatusOK, resp.StatusCode) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2022-06-14 16:23:34 +00:00
|
|
|
assert.EqualValues(t, "text/markdown; charset=utf-8", resp.Header.Get("Content-Type"))
|
|
|
|
assert.EqualValues(t, "106", resp.Header.Get("Content-Length"))
|
2022-06-13 12:43:49 +00:00
|
|
|
assert.EqualValues(t, 106, getSize(resp.Body))
|
|
|
|
}
|
|
|
|
|
2022-06-12 01:50:00 +00:00
|
|
|
func TestGetNotFound(t *testing.T) {
|
2022-08-12 03:06:26 +00:00
|
|
|
log.Println("=== TestGetNotFound ===")
|
2022-06-12 01:50:00 +00:00
|
|
|
// test custom not found pages
|
|
|
|
resp, err := getTestHTTPSClient().Get("https://crystal.localhost.mock.directory:4430/pages-404-demo/blah")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !assert.EqualValues(t, http.StatusNotFound, resp.StatusCode) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2022-06-14 16:23:34 +00:00
|
|
|
assert.EqualValues(t, "text/html; charset=utf-8", resp.Header.Get("Content-Type"))
|
|
|
|
assert.EqualValues(t, "37", resp.Header.Get("Content-Length"))
|
2022-06-12 01:50:00 +00:00
|
|
|
assert.EqualValues(t, 37, getSize(resp.Body))
|
|
|
|
}
|
|
|
|
|
2022-06-11 21:17:43 +00:00
|
|
|
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()
|
|
|
|
}
|