From 3c28ba0b35f4762933070931fbfd0c7132e00ad7 Mon Sep 17 00:00:00 2001 From: John Beisley Date: Sun, 27 Oct 2013 20:17:00 +0000 Subject: [PATCH] Implement SOAP "bin.base64" marshalling. --- soap/types.go | 11 +++++++++++ soap/types_test.go | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/soap/types.go b/soap/types.go index a2d4a13..abb110a 100644 --- a/soap/types.go +++ b/soap/types.go @@ -1,6 +1,7 @@ package soap import ( + "encoding/base64" "errors" "fmt" "regexp" @@ -396,3 +397,13 @@ func UnmarshalBoolean(s string) (bool, error) { } return false, fmt.Errorf("soap boolean: %q is not a valid boolean value", s) } + +// MarshalBinBase64 marshals []byte to SOAP "bin.base64" type. +func MarshalBinBase64(v []byte) (string, error) { + return base64.StdEncoding.EncodeToString(v), nil +} + +// UnmarshalBinBase64 unmarshals []byte from the SOAP "bin.base64" type. +func UnmarshalBinBase64(s string) ([]byte, error) { + return base64.StdEncoding.DecodeString(s) +} diff --git a/soap/types_test.go b/soap/types_test.go index 3af92bd..2a48311 100644 --- a/soap/types_test.go +++ b/soap/types_test.go @@ -1,6 +1,7 @@ package soap import ( + "bytes" "math" "testing" "time" @@ -146,6 +147,18 @@ func (v BooleanTest) Equal(result interface{}) bool { return bool(v) == result.(bool) } +type BinBase64Test []byte + +func (v BinBase64Test) Marshal() (string, error) { + return MarshalBinBase64([]byte(v)) +} +func (v BinBase64Test) Unmarshal(s string) (interface{}, error) { + return UnmarshalBinBase64(s) +} +func (v BinBase64Test) Equal(result interface{}) bool { + return bytes.Equal([]byte(v), result.([]byte)) +} + func Test(t *testing.T) { const time010203 time.Duration = (1*3600 + 2*60 + 3) * time.Second const time0102 time.Duration = (1*3600 + 2*60) * time.Second @@ -257,6 +270,12 @@ func Test(t *testing.T) { {str: "other", value: BooleanTest(false), noMarshal: true, wantUnmarshalErr: true}, {str: "2", value: BooleanTest(false), noMarshal: true, wantUnmarshalErr: true}, {str: "-1", value: BooleanTest(false), noMarshal: true, wantUnmarshalErr: true}, + + // bin.base64 + {str: "", value: BinBase64Test{}}, + {str: "YQ==", value: BinBase64Test("a")}, + {str: "TG9uZ2VyIFN0cmluZy4=", value: BinBase64Test("Longer String.")}, + {str: "TG9uZ2VyIEFsaWduZWQu", value: BinBase64Test("Longer Aligned.")}, } // Generate extra test cases from convTests that implement duper.