grpc_api_configuration_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package descriptor
  2. import (
  3. "strings"
  4. "testing"
  5. )
  6. func TestLoadGrpcAPIServiceFromYAMLInvalidType(t *testing.T) {
  7. // Ideally this would fail but for now this test documents that it doesn't
  8. service, err := loadGrpcAPIServiceFromYAML([]byte(`type: not.the.right.type`), "invalidtype")
  9. if err != nil {
  10. t.Fatal(err)
  11. }
  12. if service == nil {
  13. t.Fatal("No service returned")
  14. }
  15. }
  16. func TestLoadGrpcAPIServiceFromYAMLSingleRule(t *testing.T) {
  17. service, err := loadGrpcAPIServiceFromYAML([]byte(`
  18. type: google.api.Service
  19. config_version: 3
  20. http:
  21. rules:
  22. - selector: grpctest.YourService.Echo
  23. post: /v1/myecho
  24. body: "*"
  25. `), "example")
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. if service.Http == nil {
  30. t.Fatal("HTTP is empty")
  31. }
  32. if len(service.Http.GetRules()) != 1 {
  33. t.Fatalf("Have %v rules instead of one. Got: %v", len(service.Http.GetRules()), service.Http.GetRules())
  34. }
  35. rule := service.Http.GetRules()[0]
  36. if rule.GetSelector() != "grpctest.YourService.Echo" {
  37. t.Errorf("Rule has unexpected selector '%v'", rule.GetSelector())
  38. }
  39. if rule.GetPost() != "/v1/myecho" {
  40. t.Errorf("Rule has unexpected post '%v'", rule.GetPost())
  41. }
  42. if rule.GetBody() != "*" {
  43. t.Errorf("Rule has unexpected body '%v'", rule.GetBody())
  44. }
  45. }
  46. func TestLoadGrpcAPIServiceFromYAMLRejectInvalidYAML(t *testing.T) {
  47. service, err := loadGrpcAPIServiceFromYAML([]byte(`
  48. type: google.api.Service
  49. config_version: 3
  50. http:
  51. rules:
  52. - selector: grpctest.YourService.Echo
  53. - post: thislinebreakstheselectorblockabovewiththeleadingdash
  54. body: "*"
  55. `), "invalidyaml")
  56. if err == nil {
  57. t.Fatal(err)
  58. }
  59. if !strings.Contains(err.Error(), "line 7") {
  60. t.Errorf("Expected yaml error to be detected in line 7. Got other error: %v", err)
  61. }
  62. if service != nil {
  63. t.Fatal("Service returned")
  64. }
  65. }
  66. func TestLoadGrpcAPIServiceFromYAMLMultipleWithAdditionalBindings(t *testing.T) {
  67. service, err := loadGrpcAPIServiceFromYAML([]byte(`
  68. type: google.api.Service
  69. config_version: 3
  70. http:
  71. rules:
  72. - selector: first.selector
  73. post: /my/post/path
  74. body: "*"
  75. additional_bindings:
  76. - post: /additional/post/path
  77. - put: /additional/put/{value}/path
  78. - delete: "{value}"
  79. - patch: "/additional/patch/{value}"
  80. - selector: some.other.service
  81. delete: foo
  82. `), "example")
  83. if err != nil {
  84. t.Fatalf("Failed to load service description from YAML: %v", err)
  85. }
  86. if service == nil {
  87. t.Fatal("No service returned")
  88. }
  89. if service.Http == nil {
  90. t.Fatal("HTTP is empty")
  91. }
  92. if len(service.Http.GetRules()) != 2 {
  93. t.Fatalf("%v service(s) returned when two were expected. Got: %v", len(service.Http.GetRules()), service.Http)
  94. }
  95. first := service.Http.GetRules()[0]
  96. if first.GetSelector() != "first.selector" {
  97. t.Errorf("first.selector has unexpected selector '%v'", first.GetSelector())
  98. }
  99. if first.GetBody() != "*" {
  100. t.Errorf("first.selector has unexpected body '%v'", first.GetBody())
  101. }
  102. if first.GetPost() != "/my/post/path" {
  103. t.Errorf("first.selector has unexpected post '%v'", first.GetPost())
  104. }
  105. if len(first.GetAdditionalBindings()) != 4 {
  106. t.Fatalf("first.selector has unexpected number of bindings %v instead of four. Got: %v", len(first.GetAdditionalBindings()), first.GetAdditionalBindings())
  107. }
  108. if first.GetAdditionalBindings()[0].GetPost() != "/additional/post/path" {
  109. t.Errorf("first.selector additional binding 0 has unexpected post '%v'", first.GetAdditionalBindings()[0].GetPost())
  110. }
  111. if first.GetAdditionalBindings()[1].GetPut() != "/additional/put/{value}/path" {
  112. t.Errorf("first.selector additional binding 1 has unexpected put '%v'", first.GetAdditionalBindings()[0].GetPost())
  113. }
  114. if first.GetAdditionalBindings()[2].GetDelete() != "{value}" {
  115. t.Errorf("first.selector additional binding 2 has unexpected delete '%v'", first.GetAdditionalBindings()[0].GetPost())
  116. }
  117. if first.GetAdditionalBindings()[3].GetPatch() != "/additional/patch/{value}" {
  118. t.Errorf("first.selector additional binding 3 has unexpected patch '%v'", first.GetAdditionalBindings()[0].GetPost())
  119. }
  120. second := service.Http.GetRules()[1]
  121. if second.GetSelector() != "some.other.service" {
  122. t.Errorf("some.other.service has unexpected selector '%v'", second.GetSelector())
  123. }
  124. if second.GetDelete() != "foo" {
  125. t.Errorf("some.other.service has unexpected delete '%v'", second.GetDelete())
  126. }
  127. if len(second.GetAdditionalBindings()) != 0 {
  128. t.Errorf("some.other.service has %v additional bindings when it should not have any. Got: %v", len(second.GetAdditionalBindings()), second.GetAdditionalBindings())
  129. }
  130. }