func TestUntypedBindingTypesForValid(t *testing.T) { op2 := parametersForAllTypes("") binder := newUntypedRequestBinder(op2, nil, strfmt.Default) confirmed := true name := "thomas" friend := map[string]interface{}{"name": "toby", "age": float64(32)} id, age, score, factor := int64(7575), int32(348), float32(5.309), float64(37.403) requestID := 19394858 tags := []string{"one", "two", "three"} dt1 := time.Date(2014, 8, 9, 0, 0, 0, 0, time.UTC) planned := strfmt.Date{Time: dt1} dt2 := time.Date(2014, 10, 12, 8, 5, 5, 0, time.UTC) delivered := strfmt.DateTime{Time: dt2} picture := base64.URLEncoding.EncodeToString([]byte("hello")) uri, _ := url.Parse("http://localhost:8002/hello/7575") qs := uri.Query() qs.Add("name", name) qs.Add("confirmed", "true") qs.Add("age", "348") qs.Add("score", "5.309") qs.Add("factor", "37.403") qs.Add("tags", strings.Join(tags, ",")) qs.Add("planned", planned.String()) qs.Add("delivered", delivered.String()) qs.Add("picture", picture) req, _ := http.NewRequest("POST", uri.String()+"?"+qs.Encode(), bytes.NewBuffer([]byte(`{"name":"toby","age":32}`))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Request-Id", "19394858") data := make(map[string]interface{}) err := binder.Bind(req, RouteParams([]RouteParam{{"id", "7575"}}), httpkit.JSONConsumer(), &data) assert.NoError(t, err) assert.Equal(t, id, data["id"]) assert.Equal(t, name, data["name"]) assert.Equal(t, friend, data["friend"]) assert.EqualValues(t, requestID, data["X-Request-Id"]) assert.Equal(t, tags, data["tags"]) assert.Equal(t, planned, data["planned"]) assert.Equal(t, delivered, data["delivered"]) assert.Equal(t, confirmed, data["confirmed"]) assert.Equal(t, age, data["age"]) assert.Equal(t, factor, data["factor"]) assert.Equal(t, score, data["score"]) pb, _ := base64.URLEncoding.DecodeString(picture) assert.EqualValues(t, pb, data["picture"].(strfmt.Base64)) }
func TestParamBinding(t *testing.T) { actual := new(SomeOperationParams) val := reflect.ValueOf(actual).Elem() pName := "Name" fld := val.FieldByName(pName) binder := &untypedParamBinder{ parameter: spec.QueryParam(pName).Typed("string", "").WithDefault("some-name"), Name: pName, } err := binder.setFieldValue(fld, "some-name", "the name value", true) assert.NoError(t, err) assert.Equal(t, "the name value", actual.Name) err = binder.setFieldValue(fld, "some-name", "", true) assert.NoError(t, err) assert.Equal(t, "some-name", actual.Name) IntParamTest(t, "ID", val, 1, 1, func() interface{} { return actual.ID }) IntParamTest(t, "ID", val, nil, 0, func() interface{} { return actual.ID }) IntParamTest(t, "Age", val, 1, 1, func() interface{} { return actual.Age }) IntParamTest(t, "Age", val, nil, 0, func() interface{} { return actual.Age }) IntParamTest(t, "Visits", val, 1, 1, func() interface{} { return actual.Visits }) IntParamTest(t, "Visits", val, nil, 0, func() interface{} { return actual.Visits }) IntParamTest(t, "Count", val, 1, 1, func() interface{} { return actual.Count }) IntParamTest(t, "Count", val, nil, 0, func() interface{} { return actual.Count }) IntParamTest(t, "Seq", val, 1, 1, func() interface{} { return actual.Seq }) IntParamTest(t, "Seq", val, nil, 0, func() interface{} { return actual.Seq }) IntParamTest(t, "UID", val, uint64(1), 1, func() interface{} { return actual.UID }) IntParamTest(t, "UID", val, uint64(0), 0, func() interface{} { return actual.UID }) IntParamTest(t, "UAge", val, uint(1), 1, func() interface{} { return actual.UAge }) IntParamTest(t, "UAge", val, nil, 0, func() interface{} { return actual.UAge }) IntParamTest(t, "UVisits", val, uint32(1), 1, func() interface{} { return actual.UVisits }) IntParamTest(t, "UVisits", val, nil, 0, func() interface{} { return actual.UVisits }) IntParamTest(t, "UCount", val, uint16(1), 1, func() interface{} { return actual.UCount }) IntParamTest(t, "UCount", val, nil, 0, func() interface{} { return actual.UCount }) IntParamTest(t, "USeq", val, uint8(1), 1, func() interface{} { return actual.USeq }) IntParamTest(t, "USeq", val, nil, 0, func() interface{} { return actual.USeq }) FloatParamTest(t, "score", "Score", "float", val, 1.0, 1, func() interface{} { return actual.Score }) FloatParamTest(t, "score", "Score", "float", val, nil, 0, func() interface{} { return actual.Score }) FloatParamTest(t, "rate", "Rate", "double", val, 1.0, 1, func() interface{} { return actual.Rate }) FloatParamTest(t, "rate", "Rate", "double", val, nil, 0, func() interface{} { return actual.Rate }) pName = "Confirmed" confirmedField := val.FieldByName(pName) binder = &untypedParamBinder{ parameter: spec.QueryParam(pName).Typed("boolean", "").WithDefault(true), Name: pName, } for _, tv := range evaluatesAsTrue { err = binder.setFieldValue(confirmedField, true, tv, true) assert.NoError(t, err) assert.True(t, actual.Confirmed) } err = binder.setFieldValue(confirmedField, true, "", true) assert.NoError(t, err) assert.True(t, actual.Confirmed) err = binder.setFieldValue(confirmedField, true, "0", true) assert.NoError(t, err) assert.False(t, actual.Confirmed) pName = "Timestamp" timeField := val.FieldByName(pName) dt := strfmt.DateTime{Time: time.Date(2014, 3, 19, 2, 9, 0, 0, time.UTC)} binder = &untypedParamBinder{ parameter: spec.QueryParam(pName).Typed("string", "date-time").WithDefault(dt), Name: pName, } exp := strfmt.DateTime{Time: time.Date(2014, 5, 14, 2, 9, 0, 0, time.UTC)} err = binder.setFieldValue(timeField, dt, exp.String(), true) assert.NoError(t, err) assert.Equal(t, exp, actual.Timestamp) err = binder.setFieldValue(timeField, dt, "", true) assert.NoError(t, err) assert.Equal(t, dt, actual.Timestamp) err = binder.setFieldValue(timeField, dt, "yada", true) assert.Error(t, err) ddt := strfmt.Date{Time: time.Date(2014, 3, 19, 0, 0, 0, 0, time.UTC)} pName = "Birthdate" dateField := val.FieldByName(pName) binder = &untypedParamBinder{ parameter: spec.QueryParam(pName).Typed("string", "date").WithDefault(ddt), Name: pName, } expd := strfmt.Date{Time: time.Date(2014, 5, 14, 0, 0, 0, 0, time.UTC)} err = binder.setFieldValue(dateField, ddt, expd.String(), true) assert.NoError(t, err) assert.Equal(t, expd, actual.Birthdate) err = binder.setFieldValue(dateField, ddt, "", true) assert.NoError(t, err) assert.Equal(t, ddt, actual.Birthdate) err = binder.setFieldValue(dateField, ddt, "yada", true) assert.Error(t, err) fdt := &strfmt.DateTime{Time: time.Date(2014, 3, 19, 2, 9, 0, 0, time.UTC)} pName = "LastFailure" ftimeField := val.FieldByName(pName) binder = &untypedParamBinder{ parameter: spec.QueryParam(pName).Typed("string", "date").WithDefault(fdt), Name: pName, } fexp := &strfmt.DateTime{Time: time.Date(2014, 5, 14, 2, 9, 0, 0, time.UTC)} err = binder.setFieldValue(ftimeField, fdt, fexp.String(), true) assert.NoError(t, err) assert.Equal(t, fexp, actual.LastFailure) err = binder.setFieldValue(ftimeField, fdt, "", true) assert.NoError(t, err) assert.Equal(t, fdt, actual.LastFailure) err = binder.setFieldValue(ftimeField, fdt, "", true) assert.NoError(t, err) assert.Equal(t, fdt, actual.LastFailure) actual.LastFailure = nil err = binder.setFieldValue(ftimeField, fdt, "yada", true) assert.Error(t, err) assert.Nil(t, actual.LastFailure) pName = "Unsupported" unsupportedField := val.FieldByName(pName) binder = &untypedParamBinder{ parameter: spec.QueryParam(pName).Typed("string", ""), Name: pName, } err = binder.setFieldValue(unsupportedField, nil, "", true) assert.Error(t, err) }