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"
2022-08-12 04:40:12 +00:00
"strings"
2022-06-11 21:17:43 +00:00
"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" )
2023-02-11 01:26:21 +00:00
if ! assert . NoError ( t , err ) {
t . FailNow ( )
}
2022-06-11 21:17:43 +00:00
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-11-12 19:37:20 +00:00
assert . EqualValues ( t , ` <a href="https://www.cabr2.de/">Temporary Redirect</a>. ` , strings . TrimSpace ( string ( getBytes ( resp . Body ) ) ) )
2022-06-11 21:17:43 +00:00
}
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
2023-01-04 05:08:06 +00:00
resp , err := getTestHTTPSClient ( ) . Get ( "https://cb_pages_tests.localhost.mock.directory:4430/images/827679288a.jpg" )
2022-06-11 21:17:43 +00:00
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
2023-01-04 05:08:06 +00:00
resp , err = getTestHTTPSClient ( ) . Get ( "https://cb_pages_tests.localhost.mock.directory:4430/pag/@master/" )
2022-06-11 21:17:43 +00:00
assert . NoError ( t , err )
2022-11-12 19:37:20 +00:00
if ! assert . NotNil ( t , resp ) {
2022-06-11 21:17:43 +00:00
t . FailNow ( )
}
2022-11-12 19:37:20 +00:00
assert . EqualValues ( t , http . StatusOK , resp . StatusCode )
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-11-12 19:37:20 +00:00
assert . Len ( t , resp . Header . Get ( "ETag" ) , 44 )
2022-07-08 11:39:24 +00:00
// access branch name contains '/'
2023-01-04 05:08:06 +00:00
resp , err = getTestHTTPSClient ( ) . Get ( "https://cb_pages_tests.localhost.mock.directory:4430/blumia/@docs~main/" )
2022-07-08 11:39:24 +00:00
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 )
2022-11-12 19:37:20 +00:00
assert . Len ( t , resp . Header . Get ( "ETag" ) , 44 )
2022-07-15 19:06:05 +00:00
2023-03-11 05:07:17 +00:00
// TODO: test get of non cacheable 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 )
2022-11-12 19:37:20 +00:00
if ! assert . NotNil ( t , resp ) {
2022-06-13 12:43:49 +00:00
t . FailNow ( )
}
2022-11-12 19:37:20 +00:00
assert . EqualValues ( t , http . StatusOK , resp . StatusCode )
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 ) )
}
2023-01-11 00:00:37 +00:00
func TestCustomDomainRedirects ( t * testing . T ) {
log . Println ( "=== TestCustomDomainRedirects ===" )
// test redirect from default pages domain to custom domain
resp , err := getTestHTTPSClient ( ) . Get ( "https://6543.localhost.mock.directory:4430/test_pages-server_custom-mock-domain/@main/README.md" )
assert . NoError ( t , err )
if ! assert . NotNil ( t , resp ) {
t . FailNow ( )
}
assert . EqualValues ( t , http . StatusTemporaryRedirect , resp . StatusCode )
assert . EqualValues ( t , "text/html; charset=utf-8" , resp . Header . Get ( "Content-Type" ) )
// TODO: custom port is not evaluated (witch does hurt tests & dev env only)
2023-02-09 17:30:06 +00:00
// assert.EqualValues(t, "https://mock-pages.codeberg-test.org:4430/@main/README.md", resp.Header.Get("Location"))
2023-01-11 00:00:37 +00:00
assert . EqualValues ( t , "https://mock-pages.codeberg-test.org/@main/README.md" , resp . Header . Get ( "Location" ) )
assert . EqualValues ( t , ` https:/codeberg.org/6543/test_pages-server_custom-mock-domain/src/branch/main/README.md; rel="canonical"; rel="canonical" ` , resp . Header . Get ( "Link" ) )
2023-02-09 17:30:06 +00:00
// test redirect from an custom domain to the primary custom domain (www.example.com -> example.com)
// regression test to https://codeberg.org/Codeberg/pages-server/issues/153
resp , err = getTestHTTPSClient ( ) . Get ( "https://mock-pages-redirect.codeberg-test.org:4430/README.md" )
assert . NoError ( t , err )
if ! assert . NotNil ( t , resp ) {
t . FailNow ( )
}
assert . EqualValues ( t , http . StatusTemporaryRedirect , resp . StatusCode )
assert . EqualValues ( t , "text/html; charset=utf-8" , resp . Header . Get ( "Content-Type" ) )
// TODO: custom port is not evaluated (witch does hurt tests & dev env only)
// assert.EqualValues(t, "https://mock-pages.codeberg-test.org:4430/README.md", resp.Header.Get("Location"))
assert . EqualValues ( t , "https://mock-pages.codeberg-test.org/README.md" , resp . Header . Get ( "Location" ) )
2023-01-11 00:00:37 +00:00
}
2023-02-11 03:12:42 +00:00
func TestRawCustomDomain ( t * testing . T ) {
log . Println ( "=== TestRawCustomDomain ===" )
// test raw domain response for custom domain branch
resp , err := getTestHTTPSClient ( ) . Get ( "https://raw.localhost.mock.directory:4430/cb_pages_tests/raw-test/example" ) // need cb_pages_tests fork
assert . NoError ( t , err )
if ! assert . NotNil ( t , resp ) {
t . FailNow ( )
}
assert . EqualValues ( t , http . StatusOK , resp . StatusCode )
assert . EqualValues ( t , "text/plain; charset=utf-8" , resp . Header . Get ( "Content-Type" ) )
assert . EqualValues ( t , "76" , resp . Header . Get ( "Content-Length" ) )
assert . EqualValues ( t , 76 , getSize ( resp . Body ) )
}
func TestRawIndex ( t * testing . T ) {
log . Println ( "=== TestRawCustomDomain ===" )
// test raw domain response for index.html
resp , err := getTestHTTPSClient ( ) . Get ( "https://raw.localhost.mock.directory:4430/cb_pages_tests/raw-test/@branch-test/index.html" ) // need cb_pages_tests fork
assert . NoError ( t , err )
if ! assert . NotNil ( t , resp ) {
t . FailNow ( )
}
assert . EqualValues ( t , http . StatusOK , resp . StatusCode )
assert . EqualValues ( t , "text/plain; charset=utf-8" , resp . Header . Get ( "Content-Type" ) )
assert . EqualValues ( t , "597" , resp . Header . Get ( "Content-Length" ) )
assert . EqualValues ( t , 597 , 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
2023-01-04 05:08:06 +00:00
resp , err := getTestHTTPSClient ( ) . Get ( "https://cb_pages_tests.localhost.mock.directory:4430/pages-404-demo/blah" )
2022-06-12 01:50:00 +00:00
assert . NoError ( t , err )
2022-11-12 19:37:20 +00:00
if ! assert . NotNil ( t , resp ) {
2022-06-12 01:50:00 +00:00
t . FailNow ( )
}
2022-11-12 19:37:20 +00:00
assert . EqualValues ( t , http . StatusNotFound , resp . StatusCode )
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-08-12 04:40:12 +00:00
func TestFollowSymlink ( t * testing . T ) {
log . Printf ( "=== TestFollowSymlink ===\n" )
2023-03-11 05:07:17 +00:00
// file symlink
2023-01-04 05:08:06 +00:00
resp , err := getTestHTTPSClient ( ) . Get ( "https://cb_pages_tests.localhost.mock.directory:4430/tests_for_pages-server/@main/link" )
2022-08-12 04:40:12 +00:00
assert . NoError ( t , err )
2022-11-12 19:37:20 +00:00
if ! assert . NotNil ( t , resp ) {
2022-08-12 04:40:12 +00:00
t . FailNow ( )
}
2022-11-12 19:37:20 +00:00
assert . EqualValues ( t , http . StatusOK , resp . StatusCode )
2022-08-12 04:40:12 +00:00
assert . EqualValues ( t , "application/octet-stream" , resp . Header . Get ( "Content-Type" ) )
assert . EqualValues ( t , "4" , resp . Header . Get ( "Content-Length" ) )
body := getBytes ( resp . Body )
assert . EqualValues ( t , 4 , len ( body ) )
assert . EqualValues ( t , "abc\n" , string ( body ) )
2023-03-11 05:07:17 +00:00
// relative file links (../index.html file in this case)
resp , err = getTestHTTPSClient ( ) . Get ( "https://cb_pages_tests.localhost.mock.directory:4430/tests_for_pages-server/@main/dir_aim/some/" )
assert . NoError ( t , err )
if ! assert . NotNil ( t , resp ) {
t . FailNow ( )
}
assert . EqualValues ( t , http . StatusOK , resp . StatusCode )
assert . EqualValues ( t , "text/html; charset=utf-8" , resp . Header . Get ( "Content-Type" ) )
assert . EqualValues ( t , "an index\n" , string ( getBytes ( resp . Body ) ) )
2022-08-12 04:40:12 +00:00
}
func TestLFSSupport ( t * testing . T ) {
log . Printf ( "=== TestLFSSupport ===\n" )
2023-01-04 05:08:06 +00:00
resp , err := getTestHTTPSClient ( ) . Get ( "https://cb_pages_tests.localhost.mock.directory:4430/tests_for_pages-server/@main/lfs.txt" )
2022-08-12 04:40:12 +00:00
assert . NoError ( t , err )
2022-11-12 19:37:20 +00:00
if ! assert . NotNil ( t , resp ) {
2022-08-12 04:40:12 +00:00
t . FailNow ( )
}
2022-11-12 19:37:20 +00:00
assert . EqualValues ( t , http . StatusOK , resp . StatusCode )
2022-08-12 04:40:12 +00:00
body := strings . TrimSpace ( string ( getBytes ( resp . Body ) ) )
assert . EqualValues ( t , 12 , len ( body ) )
assert . EqualValues ( t , "actual value" , body )
}
2022-11-12 19:37:20 +00:00
func TestGetOptions ( t * testing . T ) {
log . Println ( "=== TestGetOptions ===" )
2022-11-15 15:15:11 +00:00
req , _ := http . NewRequest ( http . MethodOptions , "https://mock-pages.codeberg-test.org:4430/README.md" , http . NoBody )
2022-11-12 19:37:20 +00:00
resp , err := getTestHTTPSClient ( ) . Do ( req )
assert . NoError ( t , err )
if ! assert . NotNil ( t , resp ) {
t . FailNow ( )
}
assert . EqualValues ( t , http . StatusNoContent , resp . StatusCode )
assert . EqualValues ( t , "GET, HEAD, OPTIONS" , resp . Header . Get ( "Allow" ) )
}
2023-02-14 02:23:28 +00:00
func TestHttpRedirect ( t * testing . T ) {
log . Println ( "=== TestHttpRedirect ===" )
resp , err := getTestHTTPSClient ( ) . Get ( "http://mock-pages.codeberg-test.org:8880/README.md" )
assert . NoError ( t , err )
if ! assert . NotNil ( t , resp ) {
t . FailNow ( )
}
assert . EqualValues ( t , http . StatusMovedPermanently , resp . StatusCode )
assert . EqualValues ( t , "text/html; charset=utf-8" , resp . Header . Get ( "Content-Type" ) )
assert . EqualValues ( t , "https://mock-pages.codeberg-test.org:4430/README.md" , resp . Header . Get ( "Location" ) )
}
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 } ,
} ,
}
}
2022-08-12 04:40:12 +00:00
func getBytes ( stream io . Reader ) [ ] byte {
buf := new ( bytes . Buffer )
_ , _ = buf . ReadFrom ( stream )
return buf . Bytes ( )
}
2022-06-11 21:17:43 +00:00
func getSize ( stream io . Reader ) int {
buf := new ( bytes . Buffer )
_ , _ = buf . ReadFrom ( stream )
return buf . Len ( )
}