corehttp: add test for gateway with mocked namesys
License: MIT Signed-off-by: Kevin Wallace <kevin@pentabarf.net>
Kevin Wallace committed
Feb 8, 2015 at 12:49 UTC
e5abf0764c9c178bbce1b1ba47ddd3efedc40066
1 file changed
+125
core/corehttp/gateway_test.go
new
+125
@@ -0,0 +1,125 @@
1
+package corehttp
2
+
3
+import (
4
+ "errors"
5
+ "fmt"
6
+ "io/ioutil"
7
+ "net/http"
8
+ "net/http/httptest"
9
+ "strings"
10
+ "testing"
11
+
12
+ context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
13
+ b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
14
+ core "github.com/jbenet/go-ipfs/core"
15
+ coreunix "github.com/jbenet/go-ipfs/core/coreunix"
16
+ namesys "github.com/jbenet/go-ipfs/namesys"
17
+ ci "github.com/jbenet/go-ipfs/p2p/crypto"
18
+ repo "github.com/jbenet/go-ipfs/repo"
19
+ config "github.com/jbenet/go-ipfs/repo/config"
20
+ u "github.com/jbenet/go-ipfs/util"
21
+ testutil "github.com/jbenet/go-ipfs/util/testutil"
22
+)
23
+
24
+type mockNamesys map[string]string
25
+
26
+func (m mockNamesys) Resolve(ctx context.Context, name string) (value u.Key, err error) {
27
+ enc, ok := m[name]
28
+ if !ok {
29
+ return "", namesys.ErrResolveFailed
30
+ }
31
+ dec := b58.Decode(enc)
32
+ if len(dec) == 0 {
33
+ return "", fmt.Errorf("invalid b58 string for name %q: %q", name, enc)
34
+ }
35
+ return u.Key(dec), nil
36
+}
37
+
38
+func (m mockNamesys) CanResolve(name string) bool {
39
+ _, ok := m[name]
40
+ return ok
41
+}
42
+
43
+func (m mockNamesys) Publish(ctx context.Context, name ci.PrivKey, value u.Key) error {
44
+ return errors.New("not implemented for mockNamesys")
45
+}
46
+
47
+func newNodeWithMockNamesys(t *testing.T, ns mockNamesys) *core.IpfsNode {
48
+ c := config.Config{
49
+ Identity: config.Identity{
50
+ PeerID: "Qmfoo", // required by offline node
51
+ },
52
+ }
53
+ r := &repo.Mock{
54
+ C: c,
55
+ D: testutil.ThreadSafeCloserMapDatastore(),
56
+ }
57
+ n, err := core.NewIPFSNode(context.Background(), core.Offline(r))
58
+ if err != nil {
59
+ t.Fatal(err)
60
+ }
61
+ n.Namesys = ns
62
+ return n
63
+}
64
+
65
+func TestGatewayGet(t *testing.T) {
66
+ ns := mockNamesys{}
67
+ n := newNodeWithMockNamesys(t, ns)
68
+ k, err := coreunix.Add(n, strings.NewReader("fnord"))
69
+ if err != nil {
70
+ t.Fatal(err)
71
+ }
72
+ ns["example.com"] = k
73
+
74
+ h, err := makeHandler(n,
75
+ IPNSHostnameOption(),
76
+ GatewayOption(false),
77
+ )
78
+ if err != nil {
79
+ t.Fatal(err)
80
+ }
81
+
82
+ ts := httptest.NewServer(h)
83
+ defer ts.Close()
84
+
85
+ for _, test := range []struct {
86
+ host string
87
+ path string
88
+ status int
89
+ text string
90
+ }{
91
+ {"localhost:5001", "/", http.StatusNotFound, "404 page not found\n"},
92
+ {"localhost:5001", "/" + k, http.StatusNotFound, "404 page not found\n"},
93
+ {"localhost:5001", "/ipfs/" + k, http.StatusOK, "fnord"},
94
+ {"localhost:5001", "/ipns/nxdomain.example.com", http.StatusBadRequest, namesys.ErrResolveFailed.Error()},
95
+ {"localhost:5001", "/ipns/example.com", http.StatusOK, "fnord"},
96
+ {"example.com", "/", http.StatusOK, "fnord"},
97
+ } {
98
+ var c http.Client
99
+ r, err := http.NewRequest("GET", ts.URL+test.path, nil)
100
+ if err != nil {
101
+ t.Fatal(err)
102
+ }
103
+ r.Host = test.host
104
+ resp, err := c.Do(r)
105
+
106
+ urlstr := "http://" + test.host + test.path
107
+ if err != nil {
108
+ t.Errorf("error requesting %s: %s", urlstr, err)
109
+ continue
110
+ }
111
+ defer resp.Body.Close()
112
+ if resp.StatusCode != test.status {
113
+ t.Errorf("got %d, expected %d from %s", resp.StatusCode, test.status, urlstr)
114
+ continue
115
+ }
116
+ body, err := ioutil.ReadAll(resp.Body)
117
+ if err != nil {
118
+ t.Fatalf("error reading response from %s: %s", urlstr, err)
119
+ }
120
+ if string(body) != test.text {
121
+ t.Errorf("unexpected response body from %s: expected %q; got %q", urlstr, test.text, body)
122
+ continue
123
+ }
124
+ }
125
+}