@cryptotaxi247 / kubo / commits / 65f908768

rewrote test to use separate test interface

verokarhu committed Sep 17, 2014 at 01:43 UTC 65f908768966629b29f19e27275f611b4555fc8e
2 files changed +22 -16
server/http/http.go
+15 -7
@@ -10,6 +10,14 @@ import (
10 merkledag "github.com/jbenet/go-ipfs/merkledag"
11 )
12
13 +type ipfs interface {
14 + ResolvePath(string) (*merkledag.Node, error)
15 +}
16 +
17 +type handler struct {
18 + ipfs
19 +}
20 +
21 type ipfsHandler struct {
22 node *core.IpfsNode
23 }
@@ -17,17 +25,17 @@ type ipfsHandler struct {
25 // Serve starts the http server
26 func Serve(address string, node *core.IpfsNode) error {
27 r := mux.NewRouter()
20 - r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { ipfsPostHandler(w, r, node) }).Methods("POST")
21 - r.PathPrefix("/").Handler(&ipfsHandler{node}).Methods("GET")
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")
30 http.Handle("/", r)
31
32 return http.ListenAndServe(address, nil)
33 }
34
27 -func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
35 +func (i *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
36 path := r.URL.Path
37
30 - nd, err := resolvePath(path, i.node)
38 + nd, err := i.ResolvePath(path)
39 if err != nil {
40 w.WriteHeader(http.StatusInternalServerError)
41 return
@@ -37,7 +45,7 @@ func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
45 w.Write(nd.Data)
46 }
47
40 -func ipfsPostHandler(w http.ResponseWriter, r *http.Request, node *core.IpfsNode) {
48 +func postHandler(w http.ResponseWriter, r *http.Request, node *core.IpfsNode) {
49 root, err := importer.NewDagFromReader(r.Body)
50 if err != nil {
51 w.WriteHeader(http.StatusInternalServerError)
@@ -55,6 +63,6 @@ func ipfsPostHandler(w http.ResponseWriter, r *http.Request, node *core.IpfsNode
63 w.Write([]byte(mh.Multihash(k).B58String()))
64 }
65
58 -var resolvePath = func(path string, node *core.IpfsNode) (*merkledag.Node, error) {
59 - return node.Resolver.ResolvePath(path)
66 +func (i *ipfsHandler) ResolvePath(path string) (*merkledag.Node, error) {
67 + return i.node.Resolver.ResolvePath(path)
68 }
server/http/http_test.go
+7 -9
@@ -6,7 +6,6 @@ import (
6 "net/http/httptest"
7 "testing"
8
9 - core "github.com/jbenet/go-ipfs/core"
9 merkledag "github.com/jbenet/go-ipfs/merkledag"
10 )
11
@@ -16,19 +15,18 @@ type getTest struct {
15 body string
16 }
17
19 -func setup() {
20 - resolvePath = func(path string, node *core.IpfsNode) (*merkledag.Node, error) {
21 - if path == "/QmUxtEgtan9M7acwc8SXF3MGpgpD9Ya8ViLNGEXQ6n9vfA" {
22 - return &merkledag.Node{Data: []byte("some fine data")}, nil
23 - }
18 +type testIpfsHandler struct{}
19
25 - return nil, errors.New("")
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 }
24 +
25 + return nil, errors.New("")
26 }
27
28 func TestServeHTTP(t *testing.T) {
30 - setup()
31 - testhandler := &ipfsHandler{}
29 + testhandler := &handler{&testIpfsHandler{}}
30 tests := []getTest{
31 {"/", http.StatusInternalServerError, ""},
32 {"/QmUxtEgtan9M7acwc8SXF3MGpgpD9Ya8ViLNGEXQ6n9vfA", http.StatusOK, "some fine data"},