@cryptotaxi247 / kubo / commits / 436f14b45

test for serveHTTP

verokarhu committed Sep 16, 2014 at 23:56 UTC 436f14b45d0f3b59e7e0c7fd145f3c7240e3f24b
2 files changed +58 -2
server/http/http.go
+7 -2
@@ -4,9 +4,10 @@ import (
4 "net/http"
5
6 "github.com/gorilla/mux"
7 + mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
8 core "github.com/jbenet/go-ipfs/core"
9 "github.com/jbenet/go-ipfs/importer"
9 - mh "github.com/jbenet/go-multihash"
10 + merkledag "github.com/jbenet/go-ipfs/merkledag"
11 )
12
13 type ipfsHandler struct {
@@ -26,7 +27,7 @@ func Serve(address string, node *core.IpfsNode) error {
27 func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
28 path := r.URL.Path
29
29 - nd, err := i.node.Resolver.ResolvePath(path)
30 + nd, err := resolvePath(path, i.node)
31 if err != nil {
32 w.WriteHeader(http.StatusInternalServerError)
33 return
@@ -53,3 +54,7 @@ func ipfsPostHandler(w http.ResponseWriter, r *http.Request, node *core.IpfsNode
54 w.WriteHeader(http.StatusCreated)
55 w.Write([]byte(mh.Multihash(k).B58String()))
56 }
57 +
58 +var resolvePath = func(path string, node *core.IpfsNode) (*merkledag.Node, error) {
59 + return node.Resolver.ResolvePath(path)
60 +}
server/http/http_test.go new
+51
@@ -0,0 +1,51 @@
1 +package http
2 +
3 +import (
4 + "errors"
5 + "net/http"
6 + "net/http/httptest"
7 + "testing"
8 +
9 + core "github.com/jbenet/go-ipfs/core"
10 + merkledag "github.com/jbenet/go-ipfs/merkledag"
11 +)
12 +
13 +type getTest struct {
14 + url string
15 + code int
16 + body string
17 +}
18 +
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 + }
24 +
25 + return nil, errors.New("")
26 + }
27 +}
28 +
29 +func TestServeHTTP(t *testing.T) {
30 + setup()
31 + testhandler := &ipfsHandler{}
32 + tests := []getTest{
33 + {"/", http.StatusInternalServerError, ""},
34 + {"/QmUxtEgtan9M7acwc8SXF3MGpgpD9Ya8ViLNGEXQ6n9vfA", http.StatusOK, "some fine data"},
35 + }
36 +
37 + for _, test := range tests {
38 + req, _ := http.NewRequest("GET", test.url, nil)
39 + resp := httptest.NewRecorder()
40 + testhandler.ServeHTTP(resp, req)
41 +
42 + if resp.Code != test.code {
43 + t.Error("expected status code", test.code, "received", resp.Code)
44 + }
45 +
46 + if resp.Body.String() != test.body {
47 + t.Error("expected body:", test.body)
48 + t.Error("received body:", resp.Body)
49 + }
50 + }
51 +}