Пример #1
0
func TestRequestInfo(t *testing.T) {
	t.SkipNow()
	path := "/foo/bar"
	desc := "this is a description yo"
	// Test failure
	if _, err := NewRequestInfo(reflect.TypeOf(35), path, desc, nil); err == nil {
		t.Errorf("RequestInfo on non struct should fail")
	}

	ri, err := NewRequestInfo(reflect.TypeOf(MockHandler{}), path, desc, nil)
	if err != nil {
		t.Error(err)
	}

	if ri.Group != "foo" {
		t.Errorf("Bad group '%s'", ri.Group)
	}
	if ri.Path != path {
		t.Errorf("Wrong path, expected %s, got '%s'", path, ri.Path)
	}
	if ri.Description != desc {
		t.Errorf("Wrong desc, expected %s, got '%s'", desc, ri.Description)
	}

	if len(ri.Params) != len(expected) {
		t.Errorf("Wrong number of params, expected %d, got %d", len(expected), len(ri.Params))
	}

	for i := range ri.Params {
		if !reflect.DeepEqual(ri.Params[i], expected[i]) {
			t.Errorf("Wrong param match %d: \ngot: %#v\nexp: %#v", i, ri.Params[i], expected[i])
		}
	}

	// Test conversion to swagger
	sw := ri.ToSwagger()
	if sw.Description != ri.Description {
		t.Errorf("Unmatching descriptions")
	}
	if len(sw.Parameters) != len(ri.Params) {
		t.Errorf("Unmatching parameters")
	}

	for i := range ri.Params {

		p := ri.Params[i]
		s := sw.Parameters[i]

		if p.Name != s.Name || p.RawDefault != s.Default || s.Type != swagger.TypeOf(p.Kind, swagger.String) {
			t.Errorf("Unmatching param %s", p.Name)
		}

	}
}
Пример #2
0
// ToSwagger converts the paramInfo into a swagger Param - they are almost the same, but kept separate
// for decoupling purposes.
func (p ParamInfo) ToSwagger() swagger.Param {
	ret := swagger.Param{
		Name:        p.Name,
		Description: p.Description,

		Required:  p.Required,
		Format:    p.Format,
		Default:   p.Default,
		Max:       p.Max,
		Min:       p.Min,
		MaxLength: p.MaxLength,
		MinLength: p.MinLength,
		Pattern:   p.Pattern,
		Enum:      p.Options,
		In:        p.In,
		Global:    p.Global,
	}

	ret.Type, ret.Items = swagger.TypeOf(p.Type, swagger.String)
	return ret
}