示例#1
0
func TestIntrospectorBeforeUnknownField(t *testing.T) {
	object := struct {
		IntrospectorCompliant
		F int `field:"f"`
	}{}
	i := NewIntrospector(&object)
	i.Before()

	// It shouldn't change the state of the object

	copied := object
	object.SetField("missing", "field", 17)

	if copied.F != object.F {
		t.Errorf("Both objects are expected to be equal:\n%s", tests.Diff(copied, object))
	}

	object.SetField("field", "g", 17)

	if copied.F != object.F {
		t.Errorf("Both objects are expected to be equal:\n%s", tests.Diff(copied, object))
	}

	f := object.Field("missing", "field")

	if f != nil {
		t.Errorf("The value %#v is supposed to be nil", f)
	}
}
示例#2
0
func TestJSONBefore(t *testing.T) {
	json := `
	{
		"um": 1,
		"dois": "dois",
		"três": {
			"quatro": [1, 2, 3, 4, 5]
		}
	}
	`

	req, err := http.NewRequest("PUT", "/", strings.NewReader(json))

	if err != nil {
		t.Fatal(err)
	}

	handler := &mockJSONHandler{req: req}
	i := NewIntrospector(handler)
	i.Before()
	u := NewJSONCodec(handler)
	status := u.Before()

	if status != 0 {
		t.Errorf("Wrong status code. Expecting “0”; found “%d”", status)
	}

	expected := struct {
		Um   int
		Dois string
		Três struct {
			Quatro []int
		}
	}{
		Um:   1,
		Dois: "dois",
		Três: struct {
			Quatro []int
		}{
			Quatro: []int{1, 2, 3, 4, 5},
		},
	}

	if !reflect.DeepEqual(expected, handler.Request) {
		t.Error("Wrong request")
		t.Log(tests.Diff(expected, handler.Request))
	}
}