func TestAcceptParsing1(t *testing.T) {
	ctx := context.Background()
	r, err := http.NewRequest("GET", "/not/important", bytes.NewBuffer([]byte(string("{}"))))
	r.Header.Add("Content-Type", "application/json")
	r.Header.Add("Accept", "application/xml;q=0.7,application/json;q=0.8,application/gob;q=0.9")
	// text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

	t.Logf("Accept Header: %s\n", r.Header.Get("Accept"))

	req := new(request)
	req.embedMime = new(embedMime)

	def := encoding.Default()

	_, err = def.DecodeRequest(req)(ctx, r)
	if err != nil {
		t.Logf("Decoding Failed: %s\n", err)
		t.Fail()
	}

	if mime := req.GetMime(); mime != "application/gob" {
		t.Logf("mime != \"application/gob\": %s\n", mime)
		t.Fail()
	}
}
func TestGobResponseSniff1(t *testing.T) {
	var e request
	e.embedMime = new(embedMime)
	ctx := context.Background()

	response := new(http.Response)
	response.StatusCode = 200

	b := []byte{0x37, 0xff, 0x81, 0x03, 0x01, 0x01, 0x07, 0x72, 0x65,
		0x71, 0x75, 0x65, 0x73, 0x74, 0x01, 0xff, 0x82, 0x00,
		0x01, 0x04, 0x01, 0x03, 0x53, 0x74, 0x72, 0x01, 0x0c,
		0x00, 0x01, 0x03, 0x4e, 0x75, 0x6d, 0x01, 0x08, 0x00,
		0x01, 0x04, 0x42, 0x6f, 0x6f, 0x6c, 0x01, 0x02, 0x00,
		0x01, 0x04, 0x4e, 0x75, 0x6c, 0x6c, 0x01, 0x10, 0x00,
		0x00, 0x00, 0x0e, 0xff, 0x82, 0x01, 0x03, 0x62, 0x61,
		0x72, 0x01, 0xfe, 0x24, 0x40, 0x01, 0x01, 0x00}

	buf := bytes.NewBuffer(b)

	response.Body = ioutil.NopCloser(buf)
	response.ContentLength = int64(buf.Len())

	def := encoding.Default()

	e1, err := def.DecodeResponse(&e)(ctx, response)
	if err != nil {
		t.Logf("Decode Request Failed: %s\n", err)
		t.Fail()
	}

	if e1 != &e {
		t.Logf("Returned Result is NOT the same value: %#v\n", e1)
		t.Fail()
	}

	if e.Str != "bar" {
		t.Logf("e.Str != \"bar\": \"%s\"\n", e.Str)
		t.Fail()
	}

	if e.Num != 10.0 {
		t.Logf("e.Num != 10.0: %f\n", e.Num)
		t.Fail()
	}

	if !e.Bool {
		t.Logf("!e.Bool: %f\n", e.Bool)
		t.Fail()
	}

	if e.Null != nil {
		t.Logf("e.Null != nil: %f\n", e.Null)
		t.Fail()
	}
}
func TestDecodeCustomDecodableErrorGob(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.Gob(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/gob")

	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)
	}
}
func TestGobRequestSniff1(t *testing.T) {
	var e request
	e.embedMime = new(embedMime)
	ctx := context.Background()

	b := []byte{0x37, 0xff, 0x81, 0x03, 0x01, 0x01, 0x07, 0x72, 0x65,
		0x71, 0x75, 0x65, 0x73, 0x74, 0x01, 0xff, 0x82, 0x00,
		0x01, 0x04, 0x01, 0x03, 0x53, 0x74, 0x72, 0x01, 0x0c,
		0x00, 0x01, 0x03, 0x4e, 0x75, 0x6d, 0x01, 0x08, 0x00,
		0x01, 0x04, 0x42, 0x6f, 0x6f, 0x6c, 0x01, 0x02, 0x00,
		0x01, 0x04, 0x4e, 0x75, 0x6c, 0x6c, 0x01, 0x10, 0x00,
		0x00, 0x00, 0x0e, 0xff, 0x82, 0x01, 0x03, 0x62, 0x61,
		0x72, 0x01, 0xfe, 0x24, 0x40, 0x01, 0x01, 0x00}

	buf := bytes.NewBuffer(b)
	request, err := http.NewRequest("GET", "/test", buf)
	if err != nil {
		panic(err)
	}

	def := encoding.Default()

	e1, err := def.DecodeRequest(&e)(ctx, request)
	if err != nil {
		t.Logf("Decode Request Failed: %s\n", err)
		t.Fail()
	}

	if e1 != &e {
		t.Logf("Returned Result is NOT the same value: %#v\n", e1)
		t.Fail()
	}

	if e.Str != "bar" {
		t.Logf("e.Str != \"bar\": \"%s\"\n", e.Str)
		t.Fail()
	}

	if e.Num != 10.0 {
		t.Logf("e.Num != 10.0: %f\n", e.Num)
		t.Fail()
	}

	if !e.Bool {
		t.Logf("!e.Bool: %t\n", e.Bool)
		t.Fail()
	}

	if e.Null != nil {
		t.Logf("e.Null != nil: %s\n", e.Null)
		t.Fail()
	}
}
func TestXMLResponseSniff1(t *testing.T) {
	var e request
	e.embedMime = new(embedMime)
	ctx := context.Background()

	response := new(http.Response)
	response.StatusCode = 200

	str := "<request><str>bar</str><num>10.0</num><bool>true</bool><null>null</null></request>"
	t.Logf("Data: %s\n", str)

	buf := bytes.NewBuffer([]byte(str))

	response.Body = ioutil.NopCloser(buf)
	response.ContentLength = int64(buf.Len())

	def := encoding.Default()

	e1, err := def.DecodeResponse(&e)(ctx, response)
	if err != nil {
		t.Logf("Decode Request Failed: %s\n", err)
		t.Fail()
	}

	if e1 != &e {
		t.Logf("Returned Result is NOT the same value: %#v\n", e1)
		t.Fail()
	}

	if e.Str != "bar" {
		t.Logf("e.Str != \"bar\": \"%s\"\n", e.Str)
		t.Fail()
	}

	if e.Num != 10.0 {
		t.Logf("e.Num != 10.0: %f\n", e.Num)
		t.Fail()
	}

	if !e.Bool {
		t.Logf("!e.Bool: %f\n", e.Bool)
		t.Fail()
	}

	if e.Null != nil {
		t.Logf("e.Null != nil: %f\n", e.Null)
		t.Fail()
	}
}
func TestXMLRequestSniff1(t *testing.T) {
	var e request
	e.embedMime = new(embedMime)
	ctx := context.Background()

	str := "<request><str>bar</str><num>10.0</num><bool>true</bool><null>null</null></request>"
	t.Logf("Data: %s\n", str)

	buf := bytes.NewBuffer([]byte(str))
	request, err := http.NewRequest("GET", "/test", buf)
	if err != nil {
		panic(err)
	}

	def := encoding.Default()

	e1, err := def.DecodeRequest(&e)(ctx, request)
	if err != nil {
		t.Logf("Decode Request Failed: %s\n", err)
		t.Fail()
	}

	if e1 != &e {
		t.Logf("Returned Result is NOT the same value: %#v\n", e1)
		t.Fail()
	}

	if e.Str != "bar" {
		t.Logf("e.Str != \"bar\": \"%s\"\n", e.Str)
		t.Fail()
	}

	if e.Num != 10.0 {
		t.Logf("e.Num != 10.0: %f\n", e.Num)
		t.Fail()
	}

	if !e.Bool {
		t.Logf("!e.Bool: %f\n", e.Bool)
		t.Fail()
	}

	if e.Null != nil {
		t.Logf("e.Null != nil: %f\n", e.Null)
		t.Fail()
	}
}
func TestDecodeErrorXML(t *testing.T) {
	buf := new(bytes.Buffer)
	rw := createResponseWriter(buf)
	ctx := context.Background()

	// server error...
	rw.WriteHeader(500)
	err := encoding.XML(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/xml")

	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)
	}
}
func TestXMLEncodeDecodeRequest(t *testing.T) {
	req := &request{
		Str:  "foo",
		Num:  1.5,
		Bool: true,
		Null: nil,
	}
	ctx := context.Background()

	req.embedMime = new(embedMime)
	req.SetMime("application/xml")

	ri, err := http.NewRequest("GET", "/does/not/matter", nil)
	if err != nil {
		panic(err)
	}

	err = encoding.Default().EncodeRequest()(ctx, ri, req)
	if err != nil {
		t.Log("Error Encoding Request: %s\n", err)
		t.Fail()
	}

	buf := new(bytes.Buffer)
	ri.Body = ioutil.NopCloser(io.TeeReader(ri.Body, buf))

	resp := new(request)
	resp.embedMime = new(embedMime)

	_, err = encoding.Default().DecodeRequest(resp)(ctx, ri)

	str := "<request><str>foo</str><num>1.5</num><bool>true</bool></request>"
	if s := buf.String(); s != str {
		t.Logf("Encoding Does not match: %s\n", s)
		t.Fail()
	}

	if err != nil {
		t.Logf("Request Decode Failed: %s\n", err)
		t.Fail()
	}

	if req.Str != resp.Str {
		t.Logf("req.Str != resp.Str \"%s\" vs \"%s\"\n", req.Str, resp.Str)
		t.Fail()
	}

	if req.Num != resp.Num {
		t.Logf("req.Num != resp.Num %f vs %f\n", req.Num, resp.Num)
		t.Fail()
	}

	if req.Bool != resp.Bool {
		t.Logf("req.Bool != resp.Bool %t vs %t\n", req.Bool, resp.Bool)
		t.Fail()
	}

	if req.Null != resp.Null {
		t.Logf("req.Null != resp.Null %s vs %s\n", req.Null, resp.Null)
		t.Fail()
	}
}
func TestGobEncodeDecodeResponse(t *testing.T) {
	req := &request{
		Str:  "foo",
		Num:  1.5,
		Bool: true,
		Null: false,
	}
	ctx := context.Background()
	req.embedMime = new(embedMime)
	req.SetMime("application/gob")

	buf := new(bytes.Buffer)
	ri := createResponseWriter(buf)

	err := encoding.Default().EncodeResponse()(ctx, ri, req)
	if err != nil {
		t.Log("Error Encoding Request: %s\n", err)
		t.Fail()
	}

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

	resp := new(request)
	resp.embedMime = new(embedMime)

	byts := []byte{0x37, 0xff, 0x81, 0x03, 0x01, 0x01, 0x07, 0x72, 0x65,
		0x71, 0x75, 0x65, 0x73, 0x74, 0x01, 0xff, 0x82, 0x00,
		0x01, 0x04, 0x01, 0x03, 0x53, 0x74, 0x72, 0x01, 0x0c,
		0x00, 0x01, 0x03, 0x4e, 0x75, 0x6d, 0x01, 0x08, 0x00,
		0x01, 0x04, 0x42, 0x6f, 0x6f, 0x6c, 0x01, 0x02, 0x00,
		0x01, 0x04, 0x4e, 0x75, 0x6c, 0x6c, 0x01, 0x10, 0x00,
		0x00, 0x00, 0x18, 0xff, 0x82, 0x01, 0x03, 0x66, 0x6f,
		0x6f, 0x01, 0xfe, 0xf8, 0x3f, 0x01, 0x01, 0x01, 0x04,
		0x62, 0x6f, 0x6f, 0x6c, 0x02, 0x02, 0x00, 0x00, 0x00}

	b := buf.Bytes()

	if bl1, bl2 := len(byts), len(b); bl1 != bl2 {
		t.Logf("bl1, bl2 := len(byts), len(b); bl1 != bl2: %d vs %d\n", bl1, bl2)
		t.Fail()
	}

	for i := 0; i < len(b); i++ {
		if b[i] != byts[i] {
			t.Logf("b[%d] != byts[%d]: %d vs %s", i, i, b[i], byts[i])
			t.Fail()
		}
	}

	_, err = encoding.Default().DecodeResponse(resp)(ctx, ro)

	if err != nil {
		t.Logf("Request Decode Failed: %s\n", err)
		t.Fail()
	}

	if req.Str != resp.Str {
		t.Logf("req.Str != resp.Str \"%s\" vs \"%s\"\n", req.Str, resp.Str)
		t.Fail()
	}

	if req.Num != resp.Num {
		t.Logf("req.Num != resp.Num %f vs %f\n", req.Num, resp.Num)
		t.Fail()
	}

	if req.Bool != resp.Bool {
		t.Logf("req.Bool != resp.Bool %t vs %t\n", req.Bool, resp.Bool)
		t.Fail()
	}

	if req.Null != resp.Null {
		t.Logf("req.Null != resp.Null %s vs %s\n", req.Null, resp.Null)
		t.Fail()
	}
}
func TestXMLEncodeDecodeResponse(t *testing.T) {
	req := &request{
		Str:  "foo",
		Num:  1.5,
		Bool: true,
		Null: nil,
	}
	ctx := context.Background()
	req.embedMime = new(embedMime)
	req.SetMime("application/xml")

	buf := new(bytes.Buffer)
	ri := createResponseWriter(buf)

	err := encoding.Default().EncodeResponse()(ctx, ri, req)
	if err != nil {
		t.Log("Error Encoding Request: %s\n", err)
		t.Fail()
	}

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

	resp := new(request)
	resp.embedMime = new(embedMime)

	str := "<request><str>foo</str><num>1.5</num><bool>true</bool></request>"
	if s := buf.String(); s != str {
		t.Logf("Encoding Does not match: %s\n", s)
		t.Fail()
	}

	_, err = encoding.Default().DecodeResponse(resp)(ctx, ro)

	if err != nil {
		t.Logf("Request Decode Failed: %s\n", err)
		t.Fail()
	}

	if req.Str != resp.Str {
		t.Logf("req.Str != resp.Str \"%s\" vs \"%s\"\n", req.Str, resp.Str)
		t.Fail()
	}

	if req.Num != resp.Num {
		t.Logf("req.Num != resp.Num %f vs %f\n", req.Num, resp.Num)
		t.Fail()
	}

	if req.Bool != resp.Bool {
		t.Logf("req.Bool != resp.Bool %t vs %t\n", req.Bool, resp.Bool)
		t.Fail()
	}

	if req.Null != resp.Null {
		t.Logf("req.Null != resp.Null %s vs %s\n", req.Null, resp.Null)
		t.Fail()
	}
}
func TestJSONEncodeDecodeRequest(t *testing.T) {
	req := &request{
		Str:  "foo",
		Num:  1.5,
		Bool: true,
		Null: false,
	}
	ctx := context.Background()
	req.embedMime = new(embedMime)
	req.SetMime("application/json")

	ri, err := http.NewRequest("GET", "/does/not/matter", nil)
	if err != nil {
		panic(err)
	}

	err = encoding.Default().EncodeRequest()(ctx, ri, req)
	if err != nil {
		t.Log("Error Encoding Request: %s\n", err)
		t.Fail()
	}

	buf := new(bytes.Buffer)
	ri.Body = ioutil.NopCloser(io.TeeReader(ri.Body, buf))

	resp := new(request)
	resp.embedMime = new(embedMime)

	_, err = encoding.Default().DecodeRequest(resp)(ctx, ri)

	str := "{\"str\":\"foo\",\"num\":1.5,\"bool\":true,\"null\":false}\n" // trailing new-line?
	if s := buf.String(); s != str {
		t.Logf("Encoding Does not match: %s\n", s)
		t.Fail()
	}

	if err != nil {
		t.Logf("Request Decode Failed: %s\n", err)
		t.Fail()
	}

	if req.Str != resp.Str {
		t.Logf("req.Str != resp.Str \"%s\" vs \"%s\"\n", req.Str, resp.Str)
		t.Fail()
	}

	if req.Num != resp.Num {
		t.Logf("req.Num != resp.Num %f vs %f\n", req.Num, resp.Num)
		t.Fail()
	}

	if req.Bool != resp.Bool {
		t.Logf("req.Bool != resp.Bool %t vs %t\n", req.Bool, resp.Bool)
		t.Fail()
	}

	if req.Null != resp.Null {
		t.Logf("req.Null != resp.Null %s vs %s\n", req.Null, resp.Null)
		t.Fail()
	}
}
func TestGobEncodeDecodeRequest(t *testing.T) {
	req := &request{
		Str:  "foo",
		Num:  1.5,
		Bool: true,
		Null: nil,
	}
	ctx := context.Background()
	req.embedMime = new(embedMime)
	req.SetMime("application/xml")

	ri, err := http.NewRequest("GET", "/does/not/matter", nil)
	if err != nil {
		panic(err)
	}

	err = encoding.Default().EncodeRequest()(ctx, ri, req)
	if err != nil {
		t.Log("Error Encoding Request: %s\n", err)
		t.Fail()
	}

	buf := new(bytes.Buffer)
	ri.Body = ioutil.NopCloser(io.TeeReader(ri.Body, buf))

	resp := new(request)
	resp.embedMime = new(embedMime)

	_, err = encoding.Default().DecodeRequest(resp)(ctx, ri)

	byts := []byte{0x3c, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x3e,
		0x3c, 0x73, 0x74, 0x72, 0x3e, 0x66, 0x6f, 0x6f, 0x3c,
		0x2f, 0x73, 0x74, 0x72, 0x3e, 0x3c, 0x6e, 0x75, 0x6d,
		0x3e, 0x31, 0x2e, 0x35, 0x3c, 0x2f, 0x6e, 0x75, 0x6d,
		0x3e, 0x3c, 0x62, 0x6f, 0x6f, 0x6c, 0x3e, 0x74, 0x72,
		0x75, 0x65, 0x3c, 0x2f, 0x62, 0x6f, 0x6f, 0x6c, 0x3e,
		0x3c, 0x2f, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
		0x3e}

	b := buf.Bytes()

	if bl1, bl2 := len(byts), len(b); bl1 != bl2 {
		t.Logf("bl1, bl2 := len(byts), len(b); bl1 != bl2: %d vs %d\n", bl1, bl2)
		t.Fail()
	}

	for i := 0; i < len(b); i++ {
		if b[i] != byts[i] {
			t.Logf("b[%d] != byts[%d]: %d vs %s", i, i, b[i], byts[i])
			t.Fail()
		}
	}

	if err != nil {
		t.Logf("Request Decode Failed: %s\n", err)
		t.Fail()
	}

	if req.Str != resp.Str {
		t.Logf("req.Str != resp.Str \"%s\" vs \"%s\"\n", req.Str, resp.Str)
		t.Fail()
	}

	if req.Num != resp.Num {
		t.Logf("req.Num != resp.Num %f vs %f\n", req.Num, resp.Num)
		t.Fail()
	}

	if req.Bool != resp.Bool {
		t.Logf("req.Bool != resp.Bool %t vs %t\n", req.Bool, resp.Bool)
		t.Fail()
	}

	if req.Null != resp.Null {
		t.Logf("req.Null != resp.Null %s vs %s\n", req.Null, resp.Null)
		t.Fail()
	}
}