code.gitea.io/gitea@v1.22.3/modules/httplib/url_test.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package httplib
     5  
     6  import (
     7  	"context"
     8  	"net/http"
     9  	"testing"
    10  
    11  	"code.gitea.io/gitea/modules/setting"
    12  	"code.gitea.io/gitea/modules/test"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestIsRelativeURL(t *testing.T) {
    18  	defer test.MockVariableValue(&setting.AppURL, "http://localhost:3000/sub/")()
    19  	defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
    20  	rel := []string{
    21  		"",
    22  		"foo",
    23  		"/",
    24  		"/foo?k=%20#abc",
    25  	}
    26  	for _, s := range rel {
    27  		assert.True(t, IsRelativeURL(s), "rel = %q", s)
    28  	}
    29  	abs := []string{
    30  		"//",
    31  		"\\\\",
    32  		"/\\",
    33  		"\\/",
    34  		"mailto:a@b.com",
    35  		"https://test.com",
    36  	}
    37  	for _, s := range abs {
    38  		assert.False(t, IsRelativeURL(s), "abs = %q", s)
    39  	}
    40  }
    41  
    42  func TestMakeAbsoluteURL(t *testing.T) {
    43  	defer test.MockVariableValue(&setting.Protocol, "http")()
    44  	defer test.MockVariableValue(&setting.AppURL, "http://cfg-host/sub/")()
    45  	defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
    46  
    47  	ctx := context.Background()
    48  	assert.Equal(t, "http://cfg-host/sub/", MakeAbsoluteURL(ctx, ""))
    49  	assert.Equal(t, "http://cfg-host/foo", MakeAbsoluteURL(ctx, "foo"))
    50  	assert.Equal(t, "http://cfg-host/foo", MakeAbsoluteURL(ctx, "/foo"))
    51  	assert.Equal(t, "http://other/foo", MakeAbsoluteURL(ctx, "http://other/foo"))
    52  
    53  	ctx = context.WithValue(ctx, RequestContextKey, &http.Request{
    54  		Host: "user-host",
    55  	})
    56  	assert.Equal(t, "http://cfg-host/foo", MakeAbsoluteURL(ctx, "/foo"))
    57  
    58  	ctx = context.WithValue(ctx, RequestContextKey, &http.Request{
    59  		Host: "user-host",
    60  		Header: map[string][]string{
    61  			"X-Forwarded-Host": {"forwarded-host"},
    62  		},
    63  	})
    64  	assert.Equal(t, "http://cfg-host/foo", MakeAbsoluteURL(ctx, "/foo"))
    65  
    66  	ctx = context.WithValue(ctx, RequestContextKey, &http.Request{
    67  		Host: "user-host",
    68  		Header: map[string][]string{
    69  			"X-Forwarded-Host":  {"forwarded-host"},
    70  			"X-Forwarded-Proto": {"https"},
    71  		},
    72  	})
    73  	assert.Equal(t, "https://forwarded-host/foo", MakeAbsoluteURL(ctx, "/foo"))
    74  }
    75  
    76  func TestIsCurrentGiteaSiteURL(t *testing.T) {
    77  	defer test.MockVariableValue(&setting.AppURL, "http://localhost:3000/sub/")()
    78  	defer test.MockVariableValue(&setting.AppSubURL, "/sub")()
    79  	ctx := context.Background()
    80  	good := []string{
    81  		"?key=val",
    82  		"/sub",
    83  		"/sub/",
    84  		"/sub/foo",
    85  		"/sub/foo/",
    86  		"http://localhost:3000/sub?key=val",
    87  		"http://localhost:3000/sub/",
    88  	}
    89  	for _, s := range good {
    90  		assert.True(t, IsCurrentGiteaSiteURL(ctx, s), "good = %q", s)
    91  	}
    92  	bad := []string{
    93  		".",
    94  		"foo",
    95  		"/",
    96  		"//",
    97  		"\\\\",
    98  		"/foo",
    99  		"http://localhost:3000/sub/..",
   100  		"http://localhost:3000/other",
   101  		"http://other/",
   102  	}
   103  	for _, s := range bad {
   104  		assert.False(t, IsCurrentGiteaSiteURL(ctx, s), "bad = %q", s)
   105  	}
   106  
   107  	setting.AppURL = "http://localhost:3000/"
   108  	setting.AppSubURL = ""
   109  	assert.False(t, IsCurrentGiteaSiteURL(ctx, "//"))
   110  	assert.False(t, IsCurrentGiteaSiteURL(ctx, "\\\\"))
   111  	assert.False(t, IsCurrentGiteaSiteURL(ctx, "http://localhost"))
   112  	assert.True(t, IsCurrentGiteaSiteURL(ctx, "http://localhost:3000?key=val"))
   113  
   114  	ctx = context.WithValue(ctx, RequestContextKey, &http.Request{
   115  		Host: "user-host",
   116  		Header: map[string][]string{
   117  			"X-Forwarded-Host":  {"forwarded-host"},
   118  			"X-Forwarded-Proto": {"https"},
   119  		},
   120  	})
   121  	assert.True(t, IsCurrentGiteaSiteURL(ctx, "http://localhost:3000"))
   122  	assert.True(t, IsCurrentGiteaSiteURL(ctx, "https://forwarded-host"))
   123  }