2023-06-16 09:32:43 +03:00
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package httplib
import (
2024-05-07 11:26:13 +03:00
"context"
"net/http"
2023-06-16 09:32:43 +03:00
"testing"
"code.gitea.io/gitea/modules/setting"
2024-03-21 15:02:34 +03:00
"code.gitea.io/gitea/modules/test"
2023-06-16 09:32:43 +03:00
"github.com/stretchr/testify/assert"
)
2024-03-21 15:02:34 +03:00
func TestIsRelativeURL ( t * testing . T ) {
defer test . MockVariableValue ( & setting . AppURL , "http://localhost:3000/sub/" ) ( )
defer test . MockVariableValue ( & setting . AppSubURL , "/sub" ) ( )
rel := [ ] string {
"" ,
"foo" ,
"/" ,
"/foo?k=%20#abc" ,
}
for _ , s := range rel {
assert . True ( t , IsRelativeURL ( s ) , "rel = %q" , s )
}
abs := [ ] string {
"//" ,
"\\\\" ,
"/\\" ,
"\\/" ,
"mailto:a@b.com" ,
"https://test.com" ,
}
for _ , s := range abs {
assert . False ( t , IsRelativeURL ( s ) , "abs = %q" , s )
}
}
2023-06-16 09:32:43 +03:00
2024-05-07 11:26:13 +03:00
func TestMakeAbsoluteURL ( t * testing . T ) {
defer test . MockVariableValue ( & setting . Protocol , "http" ) ( )
2024-05-19 17:56:08 +03:00
defer test . MockVariableValue ( & setting . AppURL , "http://cfg-host/sub/" ) ( )
2024-05-07 11:26:13 +03:00
defer test . MockVariableValue ( & setting . AppSubURL , "/sub" ) ( )
ctx := context . Background ( )
2024-05-19 17:56:08 +03:00
assert . Equal ( t , "http://cfg-host/sub/" , MakeAbsoluteURL ( ctx , "" ) )
2024-06-15 06:43:57 +03:00
assert . Equal ( t , "http://cfg-host/foo" , MakeAbsoluteURL ( ctx , "foo" ) )
assert . Equal ( t , "http://cfg-host/foo" , MakeAbsoluteURL ( ctx , "/foo" ) )
2024-05-07 11:26:13 +03:00
assert . Equal ( t , "http://other/foo" , MakeAbsoluteURL ( ctx , "http://other/foo" ) )
ctx = context . WithValue ( ctx , RequestContextKey , & http . Request {
Host : "user-host" ,
} )
2024-06-15 06:43:57 +03:00
assert . Equal ( t , "http://cfg-host/foo" , MakeAbsoluteURL ( ctx , "/foo" ) )
2024-05-07 11:26:13 +03:00
ctx = context . WithValue ( ctx , RequestContextKey , & http . Request {
Host : "user-host" ,
Header : map [ string ] [ ] string {
"X-Forwarded-Host" : { "forwarded-host" } ,
} ,
} )
2024-06-15 06:43:57 +03:00
assert . Equal ( t , "http://cfg-host/foo" , MakeAbsoluteURL ( ctx , "/foo" ) )
2024-05-07 11:26:13 +03:00
ctx = context . WithValue ( ctx , RequestContextKey , & http . Request {
Host : "user-host" ,
Header : map [ string ] [ ] string {
"X-Forwarded-Host" : { "forwarded-host" } ,
"X-Forwarded-Proto" : { "https" } ,
} ,
} )
2024-09-20 17:57:55 +03:00
assert . Equal ( t , "https://user-host/foo" , MakeAbsoluteURL ( ctx , "/foo" ) )
2024-05-07 11:26:13 +03:00
}
2024-03-21 15:02:34 +03:00
func TestIsCurrentGiteaSiteURL ( t * testing . T ) {
defer test . MockVariableValue ( & setting . AppURL , "http://localhost:3000/sub/" ) ( )
defer test . MockVariableValue ( & setting . AppSubURL , "/sub" ) ( )
2024-05-07 11:26:13 +03:00
ctx := context . Background ( )
2024-03-21 15:02:34 +03:00
good := [ ] string {
"?key=val" ,
"/sub" ,
"/sub/" ,
"/sub/foo" ,
"/sub/foo/" ,
"http://localhost:3000/sub?key=val" ,
"http://localhost:3000/sub/" ,
2023-06-16 09:32:43 +03:00
}
2024-03-21 15:02:34 +03:00
for _ , s := range good {
2024-05-07 11:26:13 +03:00
assert . True ( t , IsCurrentGiteaSiteURL ( ctx , s ) , "good = %q" , s )
2024-03-21 15:02:34 +03:00
}
bad := [ ] string {
2024-03-21 23:32:40 +03:00
"." ,
"foo" ,
2024-03-21 15:02:34 +03:00
"/" ,
"//" ,
"\\\\" ,
"/foo" ,
"http://localhost:3000/sub/.." ,
"http://localhost:3000/other" ,
"http://other/" ,
}
for _ , s := range bad {
2024-05-07 11:26:13 +03:00
assert . False ( t , IsCurrentGiteaSiteURL ( ctx , s ) , "bad = %q" , s )
2024-03-21 15:02:34 +03:00
}
setting . AppURL = "http://localhost:3000/"
setting . AppSubURL = ""
2024-05-07 11:26:13 +03:00
assert . False ( t , IsCurrentGiteaSiteURL ( ctx , "//" ) )
assert . False ( t , IsCurrentGiteaSiteURL ( ctx , "\\\\" ) )
assert . False ( t , IsCurrentGiteaSiteURL ( ctx , "http://localhost" ) )
assert . True ( t , IsCurrentGiteaSiteURL ( ctx , "http://localhost:3000?key=val" ) )
ctx = context . WithValue ( ctx , RequestContextKey , & http . Request {
Host : "user-host" ,
Header : map [ string ] [ ] string {
"X-Forwarded-Host" : { "forwarded-host" } ,
"X-Forwarded-Proto" : { "https" } ,
} ,
} )
assert . True ( t , IsCurrentGiteaSiteURL ( ctx , "http://localhost:3000" ) )
2024-09-20 17:57:55 +03:00
assert . True ( t , IsCurrentGiteaSiteURL ( ctx , "https://user-host" ) )
assert . False ( t , IsCurrentGiteaSiteURL ( ctx , "https://forwarded-host" ) )
2023-06-16 09:32:43 +03:00
}