github.com/grpc-ecosystem/grpc-gateway/v2@v2.19.1/runtime/convert_test.go (about)

     1  package runtime_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
     7  	"google.golang.org/protobuf/proto"
     8  	"google.golang.org/protobuf/types/known/durationpb"
     9  	"google.golang.org/protobuf/types/known/timestamppb"
    10  )
    11  
    12  func TestConvertTimestamp(t *testing.T) {
    13  	specs := []struct {
    14  		name    string
    15  		input   string
    16  		output  *timestamppb.Timestamp
    17  		wanterr bool
    18  	}{
    19  		{
    20  			name:  "a valid RFC3339 timestamp",
    21  			input: `"2016-05-10T10:19:13.123Z"`,
    22  			output: &timestamppb.Timestamp{
    23  				Seconds: 1462875553,
    24  				Nanos:   123000000,
    25  			},
    26  			wanterr: false,
    27  		},
    28  		{
    29  			name:  "a valid RFC3339 timestamp without double quotation",
    30  			input: "2016-05-10T10:19:13.123Z",
    31  			output: &timestamppb.Timestamp{
    32  				Seconds: 1462875553,
    33  				Nanos:   123000000,
    34  			},
    35  			wanterr: false,
    36  		},
    37  		{
    38  			name:    "invalid timestamp",
    39  			input:   `"05-10-2016T10:19:13.123Z"`,
    40  			output:  nil,
    41  			wanterr: true,
    42  		},
    43  		{
    44  			name:    "JSON number",
    45  			input:   "123",
    46  			output:  nil,
    47  			wanterr: true,
    48  		},
    49  		{
    50  			name:    "JSON bool",
    51  			input:   "true",
    52  			output:  nil,
    53  			wanterr: true,
    54  		},
    55  	}
    56  
    57  	for _, spec := range specs {
    58  		t.Run(spec.name, func(t *testing.T) {
    59  			ts, err := runtime.Timestamp(spec.input)
    60  			switch {
    61  			case err != nil && !spec.wanterr:
    62  				t.Errorf("got unexpected error\n%#v", err)
    63  			case err == nil && spec.wanterr:
    64  				t.Errorf("did not error when expecte")
    65  			case !proto.Equal(ts, spec.output):
    66  				t.Errorf(
    67  					"when testing %s; got\n%#v\nexpected\n%#v",
    68  					spec.name,
    69  					ts,
    70  					spec.output,
    71  				)
    72  			}
    73  		})
    74  	}
    75  }
    76  
    77  func TestConvertDuration(t *testing.T) {
    78  	specs := []struct {
    79  		name    string
    80  		input   string
    81  		output  *durationpb.Duration
    82  		wanterr bool
    83  	}{
    84  		{
    85  			name:  "a valid duration",
    86  			input: `"123.456s"`,
    87  			output: &durationpb.Duration{
    88  				Seconds: 123,
    89  				Nanos:   456000000,
    90  			},
    91  			wanterr: false,
    92  		},
    93  		{
    94  			name:  "a valid duration without double quotation",
    95  			input: "123.456s",
    96  			output: &durationpb.Duration{
    97  				Seconds: 123,
    98  				Nanos:   456000000,
    99  			},
   100  			wanterr: false,
   101  		},
   102  		{
   103  			name:    "invalid duration",
   104  			input:   `"123years"`,
   105  			output:  nil,
   106  			wanterr: true,
   107  		},
   108  		{
   109  			name:    "JSON number",
   110  			input:   "123",
   111  			output:  nil,
   112  			wanterr: true,
   113  		},
   114  		{
   115  			name:    "JSON bool",
   116  			input:   "true",
   117  			output:  nil,
   118  			wanterr: true,
   119  		},
   120  	}
   121  
   122  	for _, spec := range specs {
   123  		t.Run(spec.name, func(t *testing.T) {
   124  			ts, err := runtime.Duration(spec.input)
   125  			switch {
   126  			case err != nil && !spec.wanterr:
   127  				t.Errorf("got unexpected error\n%#v", err)
   128  			case err == nil && spec.wanterr:
   129  				t.Errorf("did not error when expecte")
   130  			case !proto.Equal(ts, spec.output):
   131  				t.Errorf(
   132  					"when testing %s; got\n%#v\nexpected\n%#v",
   133  					spec.name,
   134  					ts,
   135  					spec.output,
   136  				)
   137  			}
   138  		})
   139  	}
   140  }