提交 fcf635ba 作者: Chris Boddy 提交者: Steven Allen

[http_proxy_over_p2p] more tests, fix build

License: MIT
Signed-off-by: 's avatarChris Boddy <chris@boddy.im>
上级 d4d00247
package p2p
package corehttp
import (
"bufio"
......@@ -8,6 +8,7 @@ import (
"strings"
core "github.com/ipfs/go-ipfs/core"
protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
peer "gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
)
......@@ -62,19 +63,19 @@ type proxyRequest struct {
}
// from the url path parse the peer-ID, name and http path
// /http/$peer_id/$name/$http_path
// /proxy/http/$peer_id/$name/$http_path
func parseRequest(request *http.Request) (*proxyRequest, error) {
path := request.URL.Path
split := strings.SplitN(path, "/", 6)
if split[2] != "http" {
return nil, fmt.Errorf("Invalid proxy request protocol '%s'", path)
}
if len(split) < 6 {
return nil, fmt.Errorf("Invalid request path '%s'", path)
}
if split[2] != "http" {
return nil, fmt.Errorf("Invalid proxy request protocol '%s'", split[2])
}
peerID, err := peer.IDB58Decode(split[3])
if err != nil {
......
package p2p
package corehttp
import (
"github.com/ipfs/go-ipfs/thirdparty/assert"
......@@ -19,3 +19,27 @@ func TestParseRequest(t *testing.T) {
assert.True(parsed.name == "test-name", t, "proxy request name")
assert.True(parsed.target.Pretty() == "QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT", t, "proxy request peer-id")
}
func TestParseRequestInvalidProtocol(t *testing.T) {
url := "http://localhost:5001/proxy/invalid/QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT/test-name/path/to/index.txt"
req, _ := http.NewRequest("GET", url, strings.NewReader(""))
_, err := parseRequest(req)
if err == nil {
t.Fail()
}
assert.True(err.Error() == "Invalid proxy request protocol 'invalid'", t, "fails with invalid proxy")
}
func TestParseRequestInvalidPath(t *testing.T) {
url := "http://localhost:5001/proxy/http/foobar"
req, _ := http.NewRequest("GET", url, strings.NewReader(""))
_, err := parseRequest(req)
if err == nil {
t.Fail()
}
assert.True(err.Error() == "Invalid request path '/proxy/http/foobar'", t, "fails with invalid path")
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论