1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
//go:build go1.7
// +build go1.7
package rest
import (
"bytes"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"io/ioutil"
"math"
"net/http"
"reflect"
"testing"
)
func TestUnmarshalFloat64(t *testing.T) {
cases := map[string]struct {
Headers http.Header
OutputFn func() (interface{}, func(*testing.T, interface{}))
WantErr bool
}{
"header float values": {
OutputFn: func() (interface{}, func(*testing.T, interface{})) {
type output struct {
Float *float64 `location:"header" locationName:"x-amz-float"`
FloatInf *float64 `location:"header" locationName:"x-amz-float-inf"`
FloatNegInf *float64 `location:"header" locationName:"x-amz-float-neg-inf"`
FloatNaN *float64 `location:"header" locationName:"x-amz-float-nan"`
}
return &output{}, func(t *testing.T, out interface{}) {
o, ok := out.(*output)
if !ok {
t.Errorf("expect %T, got %T", (*output)(nil), out)
}
if e, a := aws.Float64(123456789.123), o.Float; !reflect.DeepEqual(e, a) {
t.Errorf("expect %v, got %v", e, a)
}
if a := aws.Float64Value(o.FloatInf); !math.IsInf(a, 1) {
t.Errorf("expect infinity, got %v", a)
}
if a := aws.Float64Value(o.FloatNegInf); !math.IsInf(a, -1) {
t.Errorf("expect infinity, got %v", a)
}
if a := aws.Float64Value(o.FloatNaN); !math.IsNaN(a) {
t.Errorf("expect infinity, got %v", a)
}
}
},
Headers: map[string][]string{
"X-Amz-Float": {"123456789.123"},
"X-Amz-Float-Inf": {"Infinity"},
"X-Amz-Float-Neg-Inf": {"-Infinity"},
"X-Amz-Float-Nan": {"NaN"},
},
},
}
for name, tt := range cases {
t.Run(name, func(t *testing.T) {
output, expectFn := tt.OutputFn()
req := &request.Request{
Data: output,
HTTPResponse: &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(nil)),
Header: tt.Headers,
},
}
if (req.Error != nil) != tt.WantErr {
t.Fatalf("WantErr(%v) != %v", tt.WantErr, req.Error)
}
if tt.WantErr {
return
}
UnmarshalMeta(req)
expectFn(t, output)
})
}
}
|