mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 19:24:14 +08:00
test for posthandler
This commit is contained in:
@ -1,32 +1,24 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
|
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
|
||||||
core "github.com/jbenet/go-ipfs/core"
|
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 {
|
type handler struct {
|
||||||
ipfs
|
ipfs
|
||||||
}
|
}
|
||||||
|
|
||||||
type ipfsHandler struct {
|
|
||||||
node *core.IpfsNode
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serve starts the http server
|
// Serve starts the http server
|
||||||
func Serve(address string, node *core.IpfsNode) error {
|
func Serve(address string, node *core.IpfsNode) error {
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { postHandler(w, r, node) }).Methods("POST")
|
handler := &handler{&ipfsHandler{node}}
|
||||||
r.PathPrefix("/").Handler(&handler{&ipfsHandler{node}}).Methods("GET")
|
r.HandleFunc("/", handler.postHandler).Methods("POST")
|
||||||
|
r.PathPrefix("/").Handler(handler).Methods("GET")
|
||||||
http.Handle("/", r)
|
http.Handle("/", r)
|
||||||
|
|
||||||
return http.ListenAndServe(address, nil)
|
return http.ListenAndServe(address, nil)
|
||||||
@ -45,16 +37,18 @@ func (i *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write(nd.Data)
|
w.Write(nd.Data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func postHandler(w http.ResponseWriter, r *http.Request, node *core.IpfsNode) {
|
func (i *handler) postHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
root, err := importer.NewDagFromReader(r.Body)
|
nd, err := i.NewDagFromReader(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
k, err := node.DAG.Add(root)
|
k, err := i.AddNodeToDAG(nd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +56,3 @@ func postHandler(w http.ResponseWriter, r *http.Request, node *core.IpfsNode) {
|
|||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
w.Write([]byte(mh.Multihash(k).B58String()))
|
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 (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"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 {
|
type test struct {
|
||||||
url string
|
url string
|
||||||
code int
|
code int
|
||||||
body string
|
reqbody string
|
||||||
}
|
respbody 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("")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServeHTTP(t *testing.T) {
|
func TestServeHTTP(t *testing.T) {
|
||||||
testhandler := &handler{&testIpfsHandler{}}
|
testhandler := &handler{&testIpfsHandler{}}
|
||||||
tests := []getTest{
|
tests := []test{
|
||||||
{"/", http.StatusInternalServerError, ""},
|
{"/", http.StatusInternalServerError, "", ""},
|
||||||
{"/QmUxtEgtan9M7acwc8SXF3MGpgpD9Ya8ViLNGEXQ6n9vfA", http.StatusOK, "some fine data"},
|
{"/hash", http.StatusOK, "", "some fine data"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
@ -41,9 +36,59 @@ func TestServeHTTP(t *testing.T) {
|
|||||||
t.Error("expected status code", test.code, "received", resp.Code)
|
t.Error("expected status code", test.code, "received", resp.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.Body.String() != test.body {
|
if resp.Body.String() != test.respbody {
|
||||||
t.Error("expected body:", test.body)
|
t.Error("expected body:", test.respbody)
|
||||||
t.Error("received body:", resp.Body)
|
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