mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-30 18:13:54 +08:00
test for posthandler
This commit is contained in:
@ -1,32 +1,24 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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"
|
||||
merkledag "github.com/jbenet/go-ipfs/merkledag"
|
||||
)
|
||||
|
||||
type ipfs interface {
|
||||
ResolvePath(string) (*merkledag.Node, error)
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
ipfs
|
||||
}
|
||||
|
||||
type ipfsHandler struct {
|
||||
node *core.IpfsNode
|
||||
}
|
||||
|
||||
// Serve starts the http server
|
||||
func Serve(address string, node *core.IpfsNode) error {
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { postHandler(w, r, node) }).Methods("POST")
|
||||
r.PathPrefix("/").Handler(&handler{&ipfsHandler{node}}).Methods("GET")
|
||||
handler := &handler{&ipfsHandler{node}}
|
||||
r.HandleFunc("/", handler.postHandler).Methods("POST")
|
||||
r.PathPrefix("/").Handler(handler).Methods("GET")
|
||||
http.Handle("/", r)
|
||||
|
||||
return http.ListenAndServe(address, nil)
|
||||
@ -45,16 +37,18 @@ func (i *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write(nd.Data)
|
||||
}
|
||||
|
||||
func postHandler(w http.ResponseWriter, r *http.Request, node *core.IpfsNode) {
|
||||
root, err := importer.NewDagFromReader(r.Body)
|
||||
func (i *handler) postHandler(w http.ResponseWriter, r *http.Request) {
|
||||
nd, err := i.NewDagFromReader(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
k, err := node.DAG.Add(root)
|
||||
k, err := i.AddNodeToDAG(nd)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -62,7 +56,3 @@ func postHandler(w http.ResponseWriter, r *http.Request, node *core.IpfsNode) {
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
w.Write([]byte(mh.Multihash(k).B58String()))
|
||||
}
|
||||
|
||||
func (i *ipfsHandler) ResolvePath(path string) (*merkledag.Node, error) {
|
||||
return i.node.Resolver.ResolvePath(path)
|
||||
}
|
||||
|
@ -2,34 +2,29 @@ package http
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
merkledag "github.com/jbenet/go-ipfs/merkledag"
|
||||
dag "github.com/jbenet/go-ipfs/merkledag"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
)
|
||||
|
||||
type getTest struct {
|
||||
url string
|
||||
code int
|
||||
body string
|
||||
}
|
||||
|
||||
type testIpfsHandler struct{}
|
||||
|
||||
func (i *testIpfsHandler) ResolvePath(path string) (*merkledag.Node, error) {
|
||||
if path == "/QmUxtEgtan9M7acwc8SXF3MGpgpD9Ya8ViLNGEXQ6n9vfA" {
|
||||
return &merkledag.Node{Data: []byte("some fine data")}, nil
|
||||
}
|
||||
|
||||
return nil, errors.New("")
|
||||
type test struct {
|
||||
url string
|
||||
code int
|
||||
reqbody string
|
||||
respbody string
|
||||
}
|
||||
|
||||
func TestServeHTTP(t *testing.T) {
|
||||
testhandler := &handler{&testIpfsHandler{}}
|
||||
tests := []getTest{
|
||||
{"/", http.StatusInternalServerError, ""},
|
||||
{"/QmUxtEgtan9M7acwc8SXF3MGpgpD9Ya8ViLNGEXQ6n9vfA", http.StatusOK, "some fine data"},
|
||||
tests := []test{
|
||||
{"/", http.StatusInternalServerError, "", ""},
|
||||
{"/hash", http.StatusOK, "", "some fine data"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
@ -41,9 +36,59 @@ func TestServeHTTP(t *testing.T) {
|
||||
t.Error("expected status code", test.code, "received", resp.Code)
|
||||
}
|
||||
|
||||
if resp.Body.String() != test.body {
|
||||
t.Error("expected body:", test.body)
|
||||
if resp.Body.String() != test.respbody {
|
||||
t.Error("expected body:", test.respbody)
|
||||
t.Error("received body:", resp.Body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostHandler(t *testing.T) {
|
||||
testhandler := &handler{&testIpfsHandler{}}
|
||||
tests := []test{
|
||||
{"/", http.StatusInternalServerError, "", ""},
|
||||
{"/", http.StatusInternalServerError, "something that causes an error in adding to DAG", ""},
|
||||
{"/", http.StatusCreated, "some fine data", "jSQBpNSebeYbPBjs1vp"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
req, _ := http.NewRequest("POST", test.url, strings.NewReader(test.reqbody))
|
||||
resp := httptest.NewRecorder()
|
||||
testhandler.postHandler(resp, req)
|
||||
|
||||
if resp.Code != test.code {
|
||||
t.Error("expected status code", test.code, "received", resp.Code)
|
||||
}
|
||||
|
||||
if resp.Body.String() != test.respbody {
|
||||
t.Error("expected body:", test.respbody)
|
||||
t.Error("received body:", resp.Body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type testIpfsHandler struct{}
|
||||
|
||||
func (i *testIpfsHandler) ResolvePath(path string) (*dag.Node, error) {
|
||||
if path == "/hash" {
|
||||
return &dag.Node{Data: []byte("some fine data")}, nil
|
||||
}
|
||||
|
||||
return nil, errors.New("")
|
||||
}
|
||||
|
||||
func (i *testIpfsHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) {
|
||||
if data, err := ioutil.ReadAll(r); err == nil {
|
||||
return &dag.Node{Data: data}, nil
|
||||
}
|
||||
|
||||
return nil, errors.New("")
|
||||
}
|
||||
|
||||
func (i *testIpfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
|
||||
if len(nd.Data) != 0 && string(nd.Data) != "something that causes an error in adding to DAG" {
|
||||
return u.Key(nd.Data), nil
|
||||
}
|
||||
|
||||
return "", errors.New("")
|
||||
}
|
||||
|
32
server/http/ipfs.go
Normal file
32
server/http/ipfs.go
Normal file
@ -0,0 +1,32 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
core "github.com/jbenet/go-ipfs/core"
|
||||
"github.com/jbenet/go-ipfs/importer"
|
||||
dag "github.com/jbenet/go-ipfs/merkledag"
|
||||
u "github.com/jbenet/go-ipfs/util"
|
||||
)
|
||||
|
||||
type ipfs interface {
|
||||
ResolvePath(string) (*dag.Node, error)
|
||||
NewDagFromReader(io.Reader) (*dag.Node, error)
|
||||
AddNodeToDAG(nd *dag.Node) (u.Key, error)
|
||||
}
|
||||
|
||||
type ipfsHandler struct {
|
||||
node *core.IpfsNode
|
||||
}
|
||||
|
||||
func (i *ipfsHandler) ResolvePath(path string) (*dag.Node, error) {
|
||||
return i.node.Resolver.ResolvePath(path)
|
||||
}
|
||||
|
||||
func (i *ipfsHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) {
|
||||
return importer.NewDagFromReader(r)
|
||||
}
|
||||
|
||||
func (i *ipfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
|
||||
return i.node.DAG.Add(nd)
|
||||
}
|
Reference in New Issue
Block a user