@cryptotaxi247 / kubo / commits / f320023c7

test for posthandler

verokarhu committed Sep 17, 2014 at 21:16 UTC f320023c7b1dfdf5f8c3bc0b15426ac83a669a30
3 files changed +105 -38
server/http/http.go
+9 -19
@@ -1,32 +1,24 @@
1 package http
2
3 import (
4 + "fmt"
5 "net/http"
6
7 "github.com/gorilla/mux"
8 mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
9 core "github.com/jbenet/go-ipfs/core"
9 - "github.com/jbenet/go-ipfs/importer"
10 - merkledag "github.com/jbenet/go-ipfs/merkledag"
10 )
11
13 -type ipfs interface {
14 - ResolvePath(string) (*merkledag.Node, error)
15 -}
16 -
12 type handler struct {
13 ipfs
14 }
15
21 -type ipfsHandler struct {
22 - node *core.IpfsNode
23 -}
24 -
16 // Serve starts the http server
17 func Serve(address string, node *core.IpfsNode) error {
18 r := mux.NewRouter()
28 - r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { postHandler(w, r, node) }).Methods("POST")
29 - r.PathPrefix("/").Handler(&handler{&ipfsHandler{node}}).Methods("GET")
19 + handler := &handler{&ipfsHandler{node}}
20 + r.HandleFunc("/", handler.postHandler).Methods("POST")
21 + r.PathPrefix("/").Handler(handler).Methods("GET")
22 http.Handle("/", r)
23
24 return http.ListenAndServe(address, nil)
@@ -45,16 +37,18 @@ func (i *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
37 w.Write(nd.Data)
38 }
39
48 -func postHandler(w http.ResponseWriter, r *http.Request, node *core.IpfsNode) {
49 - root, err := importer.NewDagFromReader(r.Body)
40 +func (i *handler) postHandler(w http.ResponseWriter, r *http.Request) {
41 + nd, err := i.NewDagFromReader(r.Body)
42 if err != nil {
43 w.WriteHeader(http.StatusInternalServerError)
44 + fmt.Println(err)
45 return
46 }
47
55 - k, err := node.DAG.Add(root)
48 + k, err := i.AddNodeToDAG(nd)
49 if err != nil {
50 w.WriteHeader(http.StatusInternalServerError)
51 + fmt.Println(err)
52 return
53 }
54
@@ -62,7 +56,3 @@ func postHandler(w http.ResponseWriter, r *http.Request, node *core.IpfsNode) {
56 w.WriteHeader(http.StatusCreated)
57 w.Write([]byte(mh.Multihash(k).B58String()))
58 }
65 -
66 -func (i *ipfsHandler) ResolvePath(path string) (*merkledag.Node, error) {
67 - return i.node.Resolver.ResolvePath(path)
68 -}
server/http/http_test.go
+64 -19
@@ -2,48 +2,93 @@ package http
2
3 import (
4 "errors"
5 + "io"
6 + "io/ioutil"
7 "net/http"
8 "net/http/httptest"
9 + "strings"
10 "testing"
11
9 - merkledag "github.com/jbenet/go-ipfs/merkledag"
12 + dag "github.com/jbenet/go-ipfs/merkledag"
13 + u "github.com/jbenet/go-ipfs/util"
14 )
15
12 -type getTest struct {
13 - url string
14 - code int
15 - body string
16 +type test struct {
17 + url string
18 + code int
19 + reqbody string
20 + respbody string
21 }
22
18 -type testIpfsHandler struct{}
19 -
20 -func (i *testIpfsHandler) ResolvePath(path string) (*merkledag.Node, error) {
21 - if path == "/QmUxtEgtan9M7acwc8SXF3MGpgpD9Ya8ViLNGEXQ6n9vfA" {
22 - return &merkledag.Node{Data: []byte("some fine data")}, nil
23 +func TestServeHTTP(t *testing.T) {
24 + testhandler := &handler{&testIpfsHandler{}}
25 + tests := []test{
26 + {"/", http.StatusInternalServerError, "", ""},
27 + {"/hash", http.StatusOK, "", "some fine data"},
28 }
29
25 - return nil, errors.New("")
30 + for _, test := range tests {
31 + req, _ := http.NewRequest("GET", test.url, nil)
32 + resp := httptest.NewRecorder()
33 + testhandler.ServeHTTP(resp, req)
34 +
35 + if resp.Code != test.code {
36 + t.Error("expected status code", test.code, "received", resp.Code)
37 + }
38 +
39 + if resp.Body.String() != test.respbody {
40 + t.Error("expected body:", test.respbody)
41 + t.Error("received body:", resp.Body)
42 + }
43 + }
44 }
45
28 -func TestServeHTTP(t *testing.T) {
46 +func TestPostHandler(t *testing.T) {
47 testhandler := &handler{&testIpfsHandler{}}
30 - tests := []getTest{
31 - {"/", http.StatusInternalServerError, ""},
32 - {"/QmUxtEgtan9M7acwc8SXF3MGpgpD9Ya8ViLNGEXQ6n9vfA", http.StatusOK, "some fine data"},
48 + tests := []test{
49 + {"/", http.StatusInternalServerError, "", ""},
50 + {"/", http.StatusInternalServerError, "something that causes an error in adding to DAG", ""},
51 + {"/", http.StatusCreated, "some fine data", "jSQBpNSebeYbPBjs1vp"},
52 }
53
54 for _, test := range tests {
36 - req, _ := http.NewRequest("GET", test.url, nil)
55 + req, _ := http.NewRequest("POST", test.url, strings.NewReader(test.reqbody))
56 resp := httptest.NewRecorder()
38 - testhandler.ServeHTTP(resp, req)
57 + testhandler.postHandler(resp, req)
58
59 if resp.Code != test.code {
60 t.Error("expected status code", test.code, "received", resp.Code)
61 }
62
44 - if resp.Body.String() != test.body {
45 - t.Error("expected body:", test.body)
63 + if resp.Body.String() != test.respbody {
64 + t.Error("expected body:", test.respbody)
65 t.Error("received body:", resp.Body)
66 }
67 }
68 }
69 +
70 +type testIpfsHandler struct{}
71 +
72 +func (i *testIpfsHandler) ResolvePath(path string) (*dag.Node, error) {
73 + if path == "/hash" {
74 + return &dag.Node{Data: []byte("some fine data")}, nil
75 + }
76 +
77 + return nil, errors.New("")
78 +}
79 +
80 +func (i *testIpfsHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) {
81 + if data, err := ioutil.ReadAll(r); err == nil {
82 + return &dag.Node{Data: data}, nil
83 + }
84 +
85 + return nil, errors.New("")
86 +}
87 +
88 +func (i *testIpfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
89 + if len(nd.Data) != 0 && string(nd.Data) != "something that causes an error in adding to DAG" {
90 + return u.Key(nd.Data), nil
91 + }
92 +
93 + return "", errors.New("")
94 +}
server/http/ipfs.go new
+32
@@ -0,0 +1,32 @@
1 +package http
2 +
3 +import (
4 + "io"
5 +
6 + core "github.com/jbenet/go-ipfs/core"
7 + "github.com/jbenet/go-ipfs/importer"
8 + dag "github.com/jbenet/go-ipfs/merkledag"
9 + u "github.com/jbenet/go-ipfs/util"
10 +)
11 +
12 +type ipfs interface {
13 + ResolvePath(string) (*dag.Node, error)
14 + NewDagFromReader(io.Reader) (*dag.Node, error)
15 + AddNodeToDAG(nd *dag.Node) (u.Key, error)
16 +}
17 +
18 +type ipfsHandler struct {
19 + node *core.IpfsNode
20 +}
21 +
22 +func (i *ipfsHandler) ResolvePath(path string) (*dag.Node, error) {
23 + return i.node.Resolver.ResolvePath(path)
24 +}
25 +
26 +func (i *ipfsHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) {
27 + return importer.NewDagFromReader(r)
28 +}
29 +
30 +func (i *ipfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
31 + return i.node.DAG.Add(nd)
32 +}