1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-30 18:13:54 +08:00

test for serveHTTP

This commit is contained in:
verokarhu
2014-09-16 23:56:36 +03:00
parent c4d55c7bb7
commit 436f14b45d
2 changed files with 58 additions and 2 deletions

View File

@ -4,9 +4,10 @@ import (
"net/http"
"github.com/gorilla/mux"
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
core "github.com/jbenet/go-ipfs/core"
"github.com/jbenet/go-ipfs/importer"
mh "github.com/jbenet/go-multihash"
merkledag "github.com/jbenet/go-ipfs/merkledag"
)
type ipfsHandler struct {
@ -26,7 +27,7 @@ func Serve(address string, node *core.IpfsNode) error {
func (i *ipfsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
nd, err := i.node.Resolver.ResolvePath(path)
nd, err := resolvePath(path, i.node)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
@ -53,3 +54,7 @@ func ipfsPostHandler(w http.ResponseWriter, r *http.Request, node *core.IpfsNode
w.WriteHeader(http.StatusCreated)
w.Write([]byte(mh.Multihash(k).B58String()))
}
var resolvePath = func(path string, node *core.IpfsNode) (*merkledag.Node, error) {
return node.Resolver.ResolvePath(path)
}

51
server/http/http_test.go Normal file
View File

@ -0,0 +1,51 @@
package http
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
core "github.com/jbenet/go-ipfs/core"
merkledag "github.com/jbenet/go-ipfs/merkledag"
)
type getTest struct {
url string
code int
body string
}
func setup() {
resolvePath = func(path string, node *core.IpfsNode) (*merkledag.Node, error) {
if path == "/QmUxtEgtan9M7acwc8SXF3MGpgpD9Ya8ViLNGEXQ6n9vfA" {
return &merkledag.Node{Data: []byte("some fine data")}, nil
}
return nil, errors.New("")
}
}
func TestServeHTTP(t *testing.T) {
setup()
testhandler := &ipfsHandler{}
tests := []getTest{
{"/", http.StatusInternalServerError, ""},
{"/QmUxtEgtan9M7acwc8SXF3MGpgpD9Ya8ViLNGEXQ6n9vfA", http.StatusOK, "some fine data"},
}
for _, test := range tests {
req, _ := http.NewRequest("GET", test.url, nil)
resp := httptest.NewRecorder()
testhandler.ServeHTTP(resp, req)
if resp.Code != test.code {
t.Error("expected status code", test.code, "received", resp.Code)
}
if resp.Body.String() != test.body {
t.Error("expected body:", test.body)
t.Error("received body:", resp.Body)
}
}
}