[http_proxy_over_p2p] more tests, fix build
License: MIT Signed-off-by: Chris Boddy <chris@boddy.im>
Chris Boddy committed
Sep 26, 2018 at 22:34 UTC
c862ac1fb47c7833c2b614fafaf46a56d4e25343
3 files changed
+52
-27
core/corehttp/proxy.go
renamed
+7
-6
@@ -1,4 +1,4 @@
1
-package p2p
1
+package corehttp
2
3
import (
4
"bufio"
@@ -8,6 +8,7 @@ import (
8
"strings"
9
10
core "github.com/ipfs/go-ipfs/core"
11
+
12
protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
13
peer "gx/ipfs/QmbNepETomvmXfz1X5pHNFD2QuPqnqi47dTd94QJWSorQ3/go-libp2p-peer"
14
)
@@ -62,19 +63,19 @@ type proxyRequest struct {
63
}
64
65
// from the url path parse the peer-ID, name and http path
65
-// /http/$peer_id/$name/$http_path
66
+// /proxy/http/$peer_id/$name/$http_path
67
func parseRequest(request *http.Request) (*proxyRequest, error) {
68
path := request.URL.Path
69
70
split := strings.SplitN(path, "/", 6)
70
- if split[2] != "http" {
71
- return nil, fmt.Errorf("Invalid proxy request protocol '%s'", path)
72
- }
73
-
71
if len(split) < 6 {
72
return nil, fmt.Errorf("Invalid request path '%s'", path)
73
}
74
75
+ if split[2] != "http" {
76
+ return nil, fmt.Errorf("Invalid proxy request protocol '%s'", split[2])
77
+ }
78
+
79
peerID, err := peer.IDB58Decode(split[3])
80
81
if err != nil {
core/corehttp/proxy_test.go
new
+45
@@ -0,0 +1,45 @@
1
+package corehttp
2
+
3
+import (
4
+ "github.com/ipfs/go-ipfs/thirdparty/assert"
5
+ "net/http"
6
+ "strings"
7
+ "testing"
8
+)
9
+
10
+func TestParseRequest(t *testing.T) {
11
+ url := "http://localhost:5001/proxy/http/QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT/test-name/path/to/index.txt"
12
+ req, _ := http.NewRequest("GET", url, strings.NewReader(""))
13
+
14
+ parsed, err := parseRequest(req)
15
+ if err != nil {
16
+ t.Error(err)
17
+ }
18
+ assert.True(parsed.httpPath == "path/to/index.txt", t, "proxy request path")
19
+ assert.True(parsed.name == "test-name", t, "proxy request name")
20
+ assert.True(parsed.target.Pretty() == "QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT", t, "proxy request peer-id")
21
+}
22
+
23
+func TestParseRequestInvalidProtocol(t *testing.T) {
24
+ url := "http://localhost:5001/proxy/invalid/QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT/test-name/path/to/index.txt"
25
+ req, _ := http.NewRequest("GET", url, strings.NewReader(""))
26
+
27
+ _, err := parseRequest(req)
28
+ if err == nil {
29
+ t.Fail()
30
+ }
31
+
32
+ assert.True(err.Error() == "Invalid proxy request protocol 'invalid'", t, "fails with invalid proxy")
33
+}
34
+
35
+func TestParseRequestInvalidPath(t *testing.T) {
36
+ url := "http://localhost:5001/proxy/http/foobar"
37
+ req, _ := http.NewRequest("GET", url, strings.NewReader(""))
38
+
39
+ _, err := parseRequest(req)
40
+ if err == nil {
41
+ t.Fail()
42
+ }
43
+
44
+ assert.True(err.Error() == "Invalid request path '/proxy/http/foobar'", t, "fails with invalid path")
45
+}
p2p/proxy_test.go
deleted
-21
@@ -1,21 +0,0 @@
1
-package p2p
2
-
3
-import (
4
- "github.com/ipfs/go-ipfs/thirdparty/assert"
5
- "net/http"
6
- "strings"
7
- "testing"
8
-)
9
-
10
-func TestParseRequest(t *testing.T) {
11
- url := "http://localhost:5001/proxy/http/QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT/test-name/path/to/index.txt"
12
- req, _ := http.NewRequest("GET", url, strings.NewReader(""))
13
-
14
- parsed, err := parseRequest(req)
15
- if err != nil {
16
- t.Error(err)
17
- }
18
- assert.True(parsed.httpPath == "path/to/index.txt", t, "proxy request path")
19
- assert.True(parsed.name == "test-name", t, "proxy request name")
20
- assert.True(parsed.target.Pretty() == "QmT8JtU54XSmC38xSb1XHFSMm775VuTeajg7LWWWTAwzxT", t, "proxy request peer-id")
21
-}