...

Source file src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor/grpc_api_configuration_test.go

Documentation: github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor

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

View as plain text