errors_test.go 3.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
package http

import (
	"context"
	"fmt"
	"io/ioutil"
	"net/http"
	"runtime"
	"strings"
	"testing"

12
	cmds "github.com/ipfs/go-ipfs-cmds"
13 14 15 16
)

func TestErrors(t *testing.T) {
	type testcase struct {
Steven Allen's avatar
Steven Allen committed
17
		opts       cmds.OptMap
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
		path       []string
		bodyStr    string
		status     string
		errTrailer string
	}

	tcs := []testcase{
		{
			path: []string{"version"},
			bodyStr: `{` +
				`"Version":"0.1.2",` +
				`"Commit":"c0mm17",` +
				`"Repo":"4",` +
				`"System":"` + runtime.GOARCH + "/" + runtime.GOOS + `",` +
				`"Golang":"` + runtime.Version() + `"}` + "\n",
			status: "200 OK",
		},

		// TODO this error should be sent as a value, because it is non-200
		{
			path:    []string{"error"},
			status:  "500 Internal Server Error",
			bodyStr: `{"Message":"an error occurred","Code":0,"Type":"error"}` + "\n",
		},

		{
			path:       []string{"lateerror"},
			status:     "200 OK",
			bodyStr:    `"some value"` + "\n",
			errTrailer: "an error occurred",
		},

Steven Allen's avatar
Steven Allen committed
50 51
		{
			path: []string{"encode"},
Steven Allen's avatar
Steven Allen committed
52
			opts: cmds.OptMap{
Steven Allen's avatar
Steven Allen committed
53 54 55 56 57 58 59 60
				cmds.EncLong: cmds.Text,
			},
			status:  "500 Internal Server Error",
			bodyStr: "an error occurred",
		},

		{
			path: []string{"lateencode"},
Steven Allen's avatar
Steven Allen committed
61
			opts: cmds.OptMap{
Steven Allen's avatar
Steven Allen committed
62 63 64 65 66 67 68
				cmds.EncLong: cmds.Text,
			},
			status:     "200 OK",
			bodyStr:    "hello\n",
			errTrailer: "an error occurred",
		},

Steven Allen's avatar
Steven Allen committed
69 70
		{
			path: []string{"protoencode"},
Steven Allen's avatar
Steven Allen committed
71
			opts: cmds.OptMap{
Steven Allen's avatar
Steven Allen committed
72 73 74 75 76 77 78 79
				cmds.EncLong: cmds.Protobuf,
			},
			status:  "500 Internal Server Error",
			bodyStr: `{"Message":"an error occurred","Code":0,"Type":"error"}` + "\n",
		},

		{
			path: []string{"protolateencode"},
Steven Allen's avatar
Steven Allen committed
80
			opts: cmds.OptMap{
Steven Allen's avatar
Steven Allen committed
81 82 83 84 85 86 87 88 89 90
				cmds.EncLong: cmds.Protobuf,
			},
			status:     "200 OK",
			bodyStr:    "hello\n",
			errTrailer: "an error occurred",
		},

		{
			// bad encoding
			path: []string{"error"},
Steven Allen's avatar
Steven Allen committed
91
			opts: cmds.OptMap{
Steven Allen's avatar
Steven Allen committed
92 93 94
				cmds.EncLong: "foobar",
			},
			status:  "400 Bad Request",
95
			bodyStr: "invalid encoding: foobar\n",
Steven Allen's avatar
Steven Allen committed
96 97
		},

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
		{
			path:    []string{"doubleclose"},
			status:  "200 OK",
			bodyStr: `"some value"` + "\n",
		},

		{
			path:    []string{"single"},
			status:  "200 OK",
			bodyStr: `"some value"` + "\n",
		},

		{
			path:    []string{"reader"},
			status:  "200 OK",
			bodyStr: "the reader call returns a reader.",
		},
	}

	mkTest := func(tc testcase) func(*testing.T) {
		return func(t *testing.T) {
119
			_, srv := getTestServer(t, nil, nil) // handler_test:/^func getTestServer/
120
			c := NewClient(srv.URL)
Steven Allen's avatar
Steven Allen committed
121
			req, err := cmds.NewRequest(context.Background(), tc.path, tc.opts, nil, nil, cmdRoot)
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
			if err != nil {
				t.Fatal(err)
			}

			httpReq, err := c.(*client).toHTTPRequest(req)
			if err != nil {
				t.Fatal("unexpected error:", err)
			}

			httpClient := http.DefaultClient

			res, err := httpClient.Do(httpReq)
			if err != nil {
				t.Fatal("unexpected error", err)
			}

			if res.Status != tc.status {
				t.Errorf("expected status %v, got %v", tc.status, res.Status)
			}

			body, err := ioutil.ReadAll(res.Body)
			if err != nil {
				t.Fatal("err reading response body", err)
			}

			if bodyStr := string(body); bodyStr != tc.bodyStr {
				t.Errorf("expected body string \n\n%v\n\n, got\n\n%v", tc.bodyStr, bodyStr)
			}

			if errTrailer := res.Trailer.Get(StreamErrHeader); errTrailer != tc.errTrailer {
				t.Errorf("expected error header %q, got %q", tc.errTrailer, errTrailer)
			}
		}
	}

	for i, tc := range tcs {
		t.Run(fmt.Sprintf("%d-%s", i, strings.Join(tc.path, "/")), mkTest(tc))
	}
}
161 162 163 164 165 166 167 168 169 170 171 172

func TestUnhandledMethod(t *testing.T) {
	tc := httpTestCase{
		Method:         "GET",
		HandledMethods: []string{"POST"},
		Code:           http.StatusMethodNotAllowed,
		ResHeaders: map[string]string{
			"Allow": "POST",
		},
	}
	tc.test(t)
}