mirror of
https://github.com/ipfs/kubo.git
synced 2025-08-24 10:32:01 +08:00

* remove unneeded thirdparty packages Remove unnecessary packages from `thirdparty` in repo. - Remove `thirdparty/assert` (replaced by `github.com/stretchr/testify/require`) - Remove `thirdparty/dir` (replacd by `misc/fsutil`) - Remove `thirdparty/notifier` (unused)
52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package corehttp
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
protocol "github.com/libp2p/go-libp2p/core/protocol"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type TestCase struct {
|
|
urlprefix string
|
|
target string
|
|
name string
|
|
path string
|
|
}
|
|
|
|
var validtestCases = []TestCase{
|
|
{"http://localhost:5001", "QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT", "/http", "path/to/index.txt"},
|
|
{"http://localhost:5001", "QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT", "/x/custom/http", "path/to/index.txt"},
|
|
{"http://localhost:5001", "QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT", "/x/custom/http", "http/path/to/index.txt"},
|
|
}
|
|
|
|
func TestParseRequest(t *testing.T) {
|
|
for _, tc := range validtestCases {
|
|
url := tc.urlprefix + "/p2p/" + tc.target + tc.name + "/" + tc.path
|
|
req, _ := http.NewRequest(http.MethodGet, url, strings.NewReader(""))
|
|
|
|
parsed, err := parseRequest(req)
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.path, parsed.httpPath, "proxy request path")
|
|
require.Equal(t, protocol.ID(tc.name), parsed.name, "proxy request name")
|
|
require.Equal(t, tc.target, parsed.target, "proxy request peer-id")
|
|
}
|
|
}
|
|
|
|
var invalidtestCases = []string{
|
|
"http://localhost:5001/p2p/http/foobar",
|
|
"http://localhost:5001/p2p/QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT/x/custom/foobar",
|
|
}
|
|
|
|
func TestParseRequestInvalidPath(t *testing.T) {
|
|
for _, tc := range invalidtestCases {
|
|
url := tc
|
|
req, _ := http.NewRequest(http.MethodGet, url, strings.NewReader(""))
|
|
|
|
_, err := parseRequest(req)
|
|
require.Error(t, err)
|
|
}
|
|
}
|