func TestDecodeCustomDecodableErrorJSON(t *testing.T) {
	buf := new(bytes.Buffer)
	rw := createResponseWriter(buf)
	ctx := context.Background()

	testErr := CustomDecodableError{
		Code:   50,
		Reason: "Halp",
	}

	// server error...
	rw.WriteHeader(500)
	err := encoding.JSON(0).EncodeResponse()(ctx, rw, &testErr)
	if err != nil {
		t.Fatalf("Unable to Encode Response: %s", err)
	}

	t.Logf("Body Content: %s", buf.String())

	ro := new(http.Response)
	ro.StatusCode = rw.statusCode
	ro.Body = ioutil.NopCloser(buf)
	ro.Header = make(http.Header)
	ro.Header.Set("Content-Type", "application/json")

	resp := new(request)
	r, err := encoding.Default().DecodeResponse(resp)(ctx, ro)
	if err != nil {
		t.Fatalf("Unable to Decode Response: %s", err)
	}

	t.Logf("Decode Result: %#v", r)
	if got, want := reflect.TypeOf(r), reflect.TypeOf(testErr); got != want {
		t.Fatalf("Type Of:\ngot:\n%s\nwant:\n%s", got, want)
	}

	castErr, ok := r.(CustomDecodableError)
	if !ok {
		t.Fatal("Unable to cast returned response into an error")
	}

	if got, want := castErr.Error(), testErr.Error(); got != want {
		t.Errorf(".Error():\ngot:\n\t%s\nwant:\n\t%s", got, want)
	}

	if got, want := castErr.Code, testErr.Code; got != want {
		t.Errorf("castErr.Code:\ngot:\n\t%d\nwant:\n\t%d", got, want)
	}

	if got, want := castErr.Reason, testErr.Reason; got != want {
		t.Errorf("castErr.Reason:\ngot:\n\t%s\nwant:\n\t%s", got, want)
	}
}
예제 #2
0
func main() {
	flag.Usage = usage
	flag.Parse()
	a := flag.Args()

	var mode = ""
	if len(a) > 0 {
		mode = a[0]
	}

	switch mode {
	case "server":
		var svc StringService
		options := []httptrans.ServerOption{httptrans.ServerErrorEncoder(func(ctx context.Context, err error, w http.ResponseWriter) {
			w.WriteHeader(500)
			encoding.JSON(1).EncodeResponse()(ctx, w, err)
		}),
		}
		trans.ServersForEndpointsWithOptions(svc, []trans.ServerLayer{}, options)
		http.ListenAndServe(args.httpPort, nil)
	case "client":
		if len(a) < 3 {
			usage()
			return
		}

		client := trans.NewClient("127.0.0.1" + args.httpPort)

		cmd := a[1]
		arg := a[2]
		switch cmd {
		case "uppercase":
			str, err := client.Uppercase(arg)
			fmt.Printf("\t\"%s\".Uppercase: \"%s\", err: %s\n", arg, str, err)
		case "count":
			count := client.Count(arg)
			fmt.Printf("\t\"%s\".Count: %d\n", arg, count)
		}

	default:
		usage()
	}

}
func TestDecodeErrorJSON(t *testing.T) {
	buf := new(bytes.Buffer)
	rw := createResponseWriter(buf)
	ctx := context.Background()

	// server error...
	rw.WriteHeader(500)
	err := encoding.JSON(0).EncodeResponse()(ctx, rw, http.ErrContentLength)
	if err != nil {
		t.Fatalf("Unable to Encode Response: %s", err)
	}

	t.Logf("Body Content: %s", buf.String())

	ro := new(http.Response)
	ro.StatusCode = rw.statusCode
	ro.Body = ioutil.NopCloser(buf)
	ro.Header = make(http.Header)
	ro.Header.Set("Content-Type", "application/json")

	resp := new(request)
	r, err := encoding.Default().DecodeResponse(resp)(ctx, ro)
	if err != nil {
		t.Fatalf("Unable to Decode Response: %s", err)
	}

	if got, want := reflect.TypeOf(r), reflect.TypeOf(encoding.WrapperError{}); got != want {
		t.Fatalf("Type Of:\ngot:\n%s\nwant:\n%s", got, want)
	}

	err, ok := r.(error)
	if !ok {
		t.Fatal("Unable to cast returned response into an error")
	}

	if got, want := err.Error(), http.ErrContentLength.Error(); got != want {
		t.Errorf(".Error():\ngot:\n\t%s\nwant:\n\t%s", got, want)
	}

	if got, want := r == http.ErrMissingContentLength, false; got != want {
		t.Errorf(".Error():\ngot:\n\t%t\nwant:\n\t%t", got, want)
	}
}