http_test.go 2.58 KB
Newer Older
verokarhu's avatar
verokarhu committed
1 2 3
package http

import (
verokarhu's avatar
verokarhu committed
4
	"bytes"
verokarhu's avatar
verokarhu committed
5
	"errors"
verokarhu's avatar
verokarhu committed
6 7
	"io"
	"io/ioutil"
verokarhu's avatar
verokarhu committed
8 9
	"net/http"
	"net/http/httptest"
verokarhu's avatar
verokarhu committed
10
	"strings"
verokarhu's avatar
verokarhu committed
11 12
	"testing"

verokarhu's avatar
verokarhu committed
13 14
	dag "github.com/jbenet/go-ipfs/merkledag"
	u "github.com/jbenet/go-ipfs/util"
verokarhu's avatar
verokarhu committed
15 16
)

verokarhu's avatar
verokarhu committed
17 18 19 20 21
type test struct {
	url      string
	code     int
	reqbody  string
	respbody string
verokarhu's avatar
verokarhu committed
22 23
}

verokarhu's avatar
verokarhu committed
24 25 26
func TestServeHTTP(t *testing.T) {
	testhandler := &handler{&testIpfsHandler{}}
	tests := []test{
verokarhu's avatar
verokarhu committed
27 28 29
		{"/ipfs/", http.StatusInternalServerError, "", ""},
		{"/ipfs/hash", http.StatusOK, "", "some fine data"},
		{"/ipfs/hash2", http.StatusInternalServerError, "", ""},
verokarhu's avatar
verokarhu committed
30
	}
31

verokarhu's avatar
verokarhu committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45
	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.respbody {
			t.Error("expected body:", test.respbody)
			t.Error("received body:", resp.Body)
		}
	}
verokarhu's avatar
verokarhu committed
46 47
}

verokarhu's avatar
verokarhu committed
48
func TestPostHandler(t *testing.T) {
49
	testhandler := &handler{&testIpfsHandler{}}
verokarhu's avatar
verokarhu committed
50
	tests := []test{
verokarhu's avatar
verokarhu committed
51 52 53
		{"/ifps/", http.StatusInternalServerError, "", ""},
		{"/ipfs/", http.StatusInternalServerError, "something that causes an error in adding to DAG", ""},
		{"/ipfs/", http.StatusCreated, "some fine data", "jSQBpNSebeYbPBjs1vp"},
verokarhu's avatar
verokarhu committed
54 55 56
	}

	for _, test := range tests {
verokarhu's avatar
verokarhu committed
57
		req, _ := http.NewRequest("POST", test.url, strings.NewReader(test.reqbody))
verokarhu's avatar
verokarhu committed
58
		resp := httptest.NewRecorder()
verokarhu's avatar
verokarhu committed
59
		testhandler.postHandler(resp, req)
verokarhu's avatar
verokarhu committed
60 61 62 63 64

		if resp.Code != test.code {
			t.Error("expected status code", test.code, "received", resp.Code)
		}

verokarhu's avatar
verokarhu committed
65 66
		if resp.Body.String() != test.respbody {
			t.Error("expected body:", test.respbody)
verokarhu's avatar
verokarhu committed
67 68 69 70
			t.Error("received body:", resp.Body)
		}
	}
}
verokarhu's avatar
verokarhu committed
71 72 73 74 75 76 77 78

type testIpfsHandler struct{}

func (i *testIpfsHandler) ResolvePath(path string) (*dag.Node, error) {
	if path == "/hash" {
		return &dag.Node{Data: []byte("some fine data")}, nil
	}

verokarhu's avatar
verokarhu committed
79 80 81 82
	if path == "/hash2" {
		return &dag.Node{Data: []byte("data that breaks dagreader")}, nil
	}

verokarhu's avatar
verokarhu committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	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("")
}
verokarhu's avatar
verokarhu committed
101 102 103 104 105 106 107 108

func (i *testIpfsHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
	if string(nd.Data) != "data that breaks dagreader" {
		return bytes.NewReader(nd.Data), nil
	}

	return nil, errors.New("")
}