route_builder_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package restful
  2. import (
  3. "testing"
  4. )
  5. func TestRouteBuilder_PathParameter(t *testing.T) {
  6. p := &Parameter{&ParameterData{Name: "name", Description: "desc"}}
  7. p.AllowMultiple(true)
  8. p.DataType("int")
  9. p.Required(true)
  10. values := map[string]string{"a": "b"}
  11. p.AllowableValues(values)
  12. p.bePath()
  13. b := new(RouteBuilder)
  14. b.function = dummy
  15. b.Param(p)
  16. r := b.Build()
  17. if !r.ParameterDocs[0].Data().AllowMultiple {
  18. t.Error("AllowMultiple invalid")
  19. }
  20. if r.ParameterDocs[0].Data().DataType != "int" {
  21. t.Error("dataType invalid")
  22. }
  23. if !r.ParameterDocs[0].Data().Required {
  24. t.Error("required invalid")
  25. }
  26. if r.ParameterDocs[0].Data().Kind != PathParameterKind {
  27. t.Error("kind invalid")
  28. }
  29. if r.ParameterDocs[0].Data().AllowableValues["a"] != "b" {
  30. t.Error("allowableValues invalid")
  31. }
  32. if b.ParameterNamed("name") == nil {
  33. t.Error("access to parameter failed")
  34. }
  35. }
  36. func TestRouteBuilder(t *testing.T) {
  37. json := "application/json"
  38. b := new(RouteBuilder)
  39. b.To(dummy)
  40. b.Path("/routes").Method("HEAD").Consumes(json).Produces(json)
  41. r := b.Build()
  42. if r.Path != "/routes" {
  43. t.Error("path invalid")
  44. }
  45. if r.Produces[0] != json {
  46. t.Error("produces invalid")
  47. }
  48. if r.Consumes[0] != json {
  49. t.Error("consumes invalid")
  50. }
  51. if r.Operation != "dummy" {
  52. t.Error("Operation not set")
  53. }
  54. }