mirror of
https://github.com/ipfs/kubo.git
synced 2026-03-07 01:08:08 +08:00
[http_proxy_over_p2p] more tests, fix build
License: MIT Signed-off-by: Chris Boddy <chris@boddy.im>
This commit is contained in:
parent
90021c16d6
commit
c862ac1fb4
@ -1,4 +1,4 @@
|
||||
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 {
|
||||
45
core/corehttp/proxy_test.go
Normal file
45
core/corehttp/proxy_test.go
Normal file
@ -0,0 +1,45 @@
|
||||
package corehttp
|
||||
|
||||
import (
|
||||
"github.com/ipfs/go-ipfs/thirdparty/assert"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseRequest(t *testing.T) {
|
||||
url := "http://localhost:5001/proxy/http/QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT/test-name/path/to/index.txt"
|
||||
req, _ := http.NewRequest("GET", url, strings.NewReader(""))
|
||||
|
||||
parsed, err := parseRequest(req)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.True(parsed.httpPath == "path/to/index.txt", t, "proxy request path")
|
||||
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")
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
package p2p
|
||||
|
||||
import (
|
||||
"github.com/ipfs/go-ipfs/thirdparty/assert"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseRequest(t *testing.T) {
|
||||
url := "http://localhost:5001/proxy/http/QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT/test-name/path/to/index.txt"
|
||||
req, _ := http.NewRequest("GET", url, strings.NewReader(""))
|
||||
|
||||
parsed, err := parseRequest(req)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.True(parsed.httpPath == "path/to/index.txt", t, "proxy request path")
|
||||
assert.True(parsed.name == "test-name", t, "proxy request name")
|
||||
assert.True(parsed.target.Pretty() == "QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT", t, "proxy request peer-id")
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user