errors_test.go 4.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, false) // 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

func TestUnhandledMethod(t *testing.T) {
	tc := httpTestCase{
164 165 166
		Method:   "GET",
		AllowGet: false,
		Code:     http.StatusMethodNotAllowed,
167
		ResHeaders: map[string]string{
168
			"Allow": "POST, HEAD, OPTIONS",
169 170 171 172
		},
	}
	tc.test(t)
}
173 174 175 176

func TestDisallowedUserAgents(t *testing.T) {
	tcs := []httpTestCase{
		{
177
			// Block Mozilla* browsers that do not provide origins.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
			Method:   "POST",
			AllowGet: false,
			Code:     http.StatusForbidden,
			ReqHeaders: map[string]string{
				"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0",
			},
		},
		{
			// Do not block on GETs
			Method:   "GET",
			AllowGet: true,
			Code:     http.StatusOK,
			ReqHeaders: map[string]string{
				"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:10.0) Gecko/20100101 Firefox/10.0",
			},
		},
		{
195 196 197 198 199 200 201
			// Do not block a Mozilla* browser that provides an
			// allowed Origin
			Method:       "POST",
			AllowGet:     false,
			AllowOrigins: []string{"*"},
			Origin:       "null",
			Code:         http.StatusOK,
202 203 204 205 206 207 208 209 210 211
			ReqHeaders: map[string]string{
				"User-Agent": "Mozilla/5.0 (Linux; U; Android 4.1.1; en-gb; Build/KLP) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30",
			},
		},
	}

	for _, tc := range tcs {
		tc.test(t)
	}
}