code.gitea.io/gitea@v1.22.3/tests/integration/git_smart_http_test.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"io"
     8  	"net/http"
     9  	"net/url"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestGitSmartHTTP(t *testing.T) {
    16  	onGiteaRun(t, testGitSmartHTTP)
    17  }
    18  
    19  func testGitSmartHTTP(t *testing.T, u *url.URL) {
    20  	kases := []struct {
    21  		p    string
    22  		code int
    23  	}{
    24  		{
    25  			p:    "user2/repo1/info/refs",
    26  			code: http.StatusOK,
    27  		},
    28  		{
    29  			p:    "user2/repo1/HEAD",
    30  			code: http.StatusOK,
    31  		},
    32  		{
    33  			p:    "user2/repo1/objects/info/alternates",
    34  			code: http.StatusNotFound,
    35  		},
    36  		{
    37  			p:    "user2/repo1/objects/info/http-alternates",
    38  			code: http.StatusNotFound,
    39  		},
    40  		{
    41  			p:    "user2/repo1/../../custom/conf/app.ini",
    42  			code: http.StatusNotFound,
    43  		},
    44  		{
    45  			p:    "user2/repo1/objects/info/../../../../custom/conf/app.ini",
    46  			code: http.StatusNotFound,
    47  		},
    48  		{
    49  			p:    `user2/repo1/objects/info/..\..\..\..\custom\conf\app.ini`,
    50  			code: http.StatusBadRequest,
    51  		},
    52  	}
    53  
    54  	for _, kase := range kases {
    55  		t.Run(kase.p, func(t *testing.T) {
    56  			p := u.String() + kase.p
    57  			req, err := http.NewRequest("GET", p, nil)
    58  			assert.NoError(t, err)
    59  			req.SetBasicAuth("user2", userPassword)
    60  			resp, err := http.DefaultClient.Do(req)
    61  			assert.NoError(t, err)
    62  			defer resp.Body.Close()
    63  			assert.EqualValues(t, kase.code, resp.StatusCode)
    64  			_, err = io.ReadAll(resp.Body)
    65  			assert.NoError(t, err)
    66  		})
    67  	}
    68  }