github.com/grpc-ecosystem/grpc-gateway/v2@v2.19.1/protoc-gen-openapiv2/internal/genopenapi/format_test.go (about)

     1  package genopenapi_test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopenapi"
    11  	"gopkg.in/yaml.v3"
    12  )
    13  
    14  func TestFormatValidate(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	testCases := [...]struct {
    18  		Format genopenapi.Format
    19  		Valid  bool
    20  	}{{
    21  		Format: genopenapi.FormatJSON,
    22  		Valid:  true,
    23  	}, {
    24  		Format: genopenapi.FormatYAML,
    25  		Valid:  true,
    26  	}, {
    27  		Format: genopenapi.Format("unknown"),
    28  		Valid:  false,
    29  	}, {
    30  		Format: genopenapi.Format(""),
    31  		Valid:  false,
    32  	}}
    33  
    34  	for _, tc := range testCases {
    35  		tc := tc
    36  
    37  		t.Run(string(tc.Format), func(t *testing.T) {
    38  			t.Parallel()
    39  
    40  			err := tc.Format.Validate()
    41  			switch {
    42  			case tc.Valid && err != nil:
    43  				t.Fatalf("expect no validation error, got: %s", err)
    44  			case !tc.Valid && err == nil:
    45  				t.Fatal("expect validation error, got nil")
    46  			}
    47  		})
    48  	}
    49  }
    50  
    51  func TestFormatEncode(t *testing.T) {
    52  	t.Parallel()
    53  
    54  	type contentDecoder interface {
    55  		Decode(v interface{}) error
    56  	}
    57  
    58  	testCases := [...]struct {
    59  		Format     genopenapi.Format
    60  		NewDecoder func(r io.Reader) contentDecoder
    61  	}{{
    62  		Format: genopenapi.FormatJSON,
    63  		NewDecoder: func(r io.Reader) contentDecoder {
    64  			return json.NewDecoder(r)
    65  		},
    66  	}, {
    67  		Format: genopenapi.FormatYAML,
    68  		NewDecoder: func(r io.Reader) contentDecoder {
    69  			return yaml.NewDecoder(r)
    70  		},
    71  	}}
    72  
    73  	for _, tc := range testCases {
    74  		tc := tc
    75  
    76  		t.Run(string(tc.Format), func(t *testing.T) {
    77  			t.Parallel()
    78  
    79  			expParams := map[string]string{
    80  				"hello": "world",
    81  			}
    82  
    83  			var buf bytes.Buffer
    84  			enc, err := tc.Format.NewEncoder(&buf)
    85  			if err != nil {
    86  				t.Fatalf("expect no encoder creating error, got: %s", err)
    87  			}
    88  
    89  			err = enc.Encode(expParams)
    90  			if err != nil {
    91  				t.Fatalf("expect no encoding error, got: %s", err)
    92  			}
    93  
    94  			gotParams := make(map[string]string)
    95  			err = tc.NewDecoder(&buf).Decode(&gotParams)
    96  			if err != nil {
    97  				t.Fatalf("expect no decoding error, got: %s", err)
    98  			}
    99  
   100  			if !reflect.DeepEqual(expParams, gotParams) {
   101  				t.Fatalf("expected: %+v, actual: %+v", expParams, gotParams)
   102  			}
   103  		})
   104  	}
   105  }