openapi_configuration_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package descriptor
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/options"
  6. )
  7. func TestLoadOpenAPIConfigFromYAMLRejectInvalidYAML(t *testing.T) {
  8. config, err := loadOpenAPIConfigFromYAML([]byte(`
  9. openapiOptions:
  10. file:
  11. - file: test.proto
  12. - option:
  13. schemes:
  14. - HTTP
  15. - HTTPS
  16. - WSS
  17. securityDefinitions:
  18. security:
  19. ApiKeyAuth:
  20. type: TYPE_API_KEY
  21. in: IN_HEADER
  22. name: "X-API-Key"
  23. `), "invalidyaml")
  24. if err == nil {
  25. t.Fatal(err)
  26. }
  27. if !strings.Contains(err.Error(), "line 4") {
  28. t.Errorf("Expected yaml error to be detected in line 4. Got other error: %v", err)
  29. }
  30. if config != nil {
  31. t.Fatal("Config returned")
  32. }
  33. }
  34. func TestLoadOpenAPIConfigFromYAML(t *testing.T) {
  35. config, err := loadOpenAPIConfigFromYAML([]byte(`
  36. openapiOptions:
  37. file:
  38. - file: test.proto
  39. option:
  40. schemes:
  41. - HTTP
  42. - HTTPS
  43. - WSS
  44. securityDefinitions:
  45. security:
  46. ApiKeyAuth:
  47. type: TYPE_API_KEY
  48. in: IN_HEADER
  49. name: "X-API-Key"
  50. `), "openapi_options")
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. if config.OpenapiOptions == nil {
  55. t.Fatal("OpenAPIOptions is empty")
  56. }
  57. opts := config.OpenapiOptions
  58. if numFileOpts := len(opts.File); numFileOpts != 1 {
  59. t.Fatalf("expected 1 file option but got %d", numFileOpts)
  60. }
  61. fileOpt := opts.File[0]
  62. if fileOpt.File != "test.proto" {
  63. t.Fatalf("file option has unexpected binding %s", fileOpt.File)
  64. }
  65. swaggerOpt := fileOpt.Option
  66. if swaggerOpt == nil {
  67. t.Fatal("expected option to be set")
  68. }
  69. if numSchemes := len(swaggerOpt.Schemes); numSchemes != 3 {
  70. t.Fatalf("expected 3 schemes but got %d", numSchemes)
  71. }
  72. if swaggerOpt.Schemes[0] != options.Scheme_HTTP {
  73. t.Fatalf("expected first scheme to be HTTP but got %s", swaggerOpt.Schemes[0])
  74. }
  75. if swaggerOpt.Schemes[1] != options.Scheme_HTTPS {
  76. t.Fatalf("expected second scheme to be HTTPS but got %s", swaggerOpt.Schemes[1])
  77. }
  78. if swaggerOpt.Schemes[2] != options.Scheme_WSS {
  79. t.Fatalf("expected third scheme to be WSS but got %s", swaggerOpt.Schemes[2])
  80. }
  81. if swaggerOpt.SecurityDefinitions == nil {
  82. t.Fatal("expected securityDefinitions to be set")
  83. }
  84. if numSecOpts := len(swaggerOpt.SecurityDefinitions.Security); numSecOpts != 1 {
  85. t.Fatalf("expected 1 security option but got %d", numSecOpts)
  86. }
  87. secOpt, ok := swaggerOpt.SecurityDefinitions.Security["ApiKeyAuth"]
  88. if !ok {
  89. t.Fatal("no SecurityScheme for key \"ApiKeyAuth\"")
  90. }
  91. if secOpt.Type != options.SecurityScheme_TYPE_API_KEY {
  92. t.Fatalf("expected scheme type to be TYPE_API_KEY but got %s", secOpt.Type)
  93. }
  94. if secOpt.In != options.SecurityScheme_IN_HEADER {
  95. t.Fatalf("expected scheme in to be IN_HEADER but got %s", secOpt.In)
  96. }
  97. if secOpt.Name != "X-API-Key" {
  98. t.Fatalf("expected name to be X-API-Key but got %s", secOpt.Name)
  99. }
  100. }