Commit 9956630e authored by Dr Ian Preston's avatar Dr Ian Preston Committed by Steven Allen

add more p2p http proxy tests

License: MIT
Signed-off-by: default avatarIan Preston <ianopolous@protonmail.com>
parent 3b2ce4a8
......@@ -6,29 +6,50 @@ import (
"testing"
"github.com/ipfs/go-ipfs/thirdparty/assert"
protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
)
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) {
url := "http://localhost:5001/p2p/QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT/http/path/to/index.txt"
req, _ := http.NewRequest("GET", url, strings.NewReader(""))
for _, tc := range validtestCases {
url := tc.urlprefix + "/p2p/" + tc.target + tc.name + "/" + tc.path
req, _ := http.NewRequest("GET", url, strings.NewReader(""))
parsed, err := parseRequest(req)
if err != nil {
t.Fatal(err)
parsed, err := parseRequest(req)
if err != nil {
t.Fatal(err)
}
assert.True(parsed.httpPath == tc.path, t, "proxy request path")
assert.True(parsed.name == protocol.ID(tc.name), t, "proxy request name")
assert.True(parsed.target == tc.target, t, "proxy request peer-id")
}
assert.True(parsed.httpPath == "path/to/index.txt", t, "proxy request path")
assert.True(parsed.name == "/http", t, "proxy request name")
assert.True(parsed.target == "QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT", t, "proxy request peer-id")
}
var invalidtestCases = []string{
"http://localhost:5001/p2p/http/foobar",
}
func TestParseRequestInvalidPath(t *testing.T) {
url := "http://localhost:5001/p2p/http/foobar"
req, _ := http.NewRequest("GET", url, strings.NewReader(""))
for _, tc := range invalidtestCases {
url := tc
req, _ := http.NewRequest("GET", url, strings.NewReader(""))
_, err := parseRequest(req)
if err == nil {
t.Fail()
_, err := parseRequest(req)
if err == nil {
t.Fail()
}
}
assert.True(err.Error() == "Invalid request path '/p2p/http/foobar'", t, "fails with invalid path")
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment