Implement SOAP bin.hex marshalling.

This commit is contained in:
John Beisley 2013-10-27 20:21:44 +00:00
parent 3c28ba0b35
commit e86d73d8f4
2 changed files with 29 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package soap
import ( import (
"encoding/base64" "encoding/base64"
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"regexp" "regexp"
@ -407,3 +408,13 @@ func MarshalBinBase64(v []byte) (string, error) {
func UnmarshalBinBase64(s string) ([]byte, error) { func UnmarshalBinBase64(s string) ([]byte, error) {
return base64.StdEncoding.DecodeString(s) return base64.StdEncoding.DecodeString(s)
} }
// MarshalBinHex marshals []byte to SOAP "bin.hex" type.
func MarshalBinHex(v []byte) (string, error) {
return hex.EncodeToString(v), nil
}
// UnmarshalBinHex unmarshals []byte from the SOAP "bin.hex" type.
func UnmarshalBinHex(s string) ([]byte, error) {
return hex.DecodeString(s)
}

View File

@ -159,6 +159,18 @@ func (v BinBase64Test) Equal(result interface{}) bool {
return bytes.Equal([]byte(v), result.([]byte)) return bytes.Equal([]byte(v), result.([]byte))
} }
type BinHexTest []byte
func (v BinHexTest) Marshal() (string, error) {
return MarshalBinHex([]byte(v))
}
func (v BinHexTest) Unmarshal(s string) (interface{}, error) {
return UnmarshalBinHex(s)
}
func (v BinHexTest) Equal(result interface{}) bool {
return bytes.Equal([]byte(v), result.([]byte))
}
func Test(t *testing.T) { func Test(t *testing.T) {
const time010203 time.Duration = (1*3600 + 2*60 + 3) * time.Second const time010203 time.Duration = (1*3600 + 2*60 + 3) * time.Second
const time0102 time.Duration = (1*3600 + 2*60) * time.Second const time0102 time.Duration = (1*3600 + 2*60) * time.Second
@ -276,6 +288,12 @@ func Test(t *testing.T) {
{str: "YQ==", value: BinBase64Test("a")}, {str: "YQ==", value: BinBase64Test("a")},
{str: "TG9uZ2VyIFN0cmluZy4=", value: BinBase64Test("Longer String.")}, {str: "TG9uZ2VyIFN0cmluZy4=", value: BinBase64Test("Longer String.")},
{str: "TG9uZ2VyIEFsaWduZWQu", value: BinBase64Test("Longer Aligned.")}, {str: "TG9uZ2VyIEFsaWduZWQu", value: BinBase64Test("Longer Aligned.")},
// bin.hex
{str: "", value: BinHexTest{}},
{str: "61", value: BinHexTest("a")},
{str: "4c6f6e67657220537472696e672e", value: BinHexTest("Longer String.")},
{str: "4C6F6E67657220537472696E672E", value: BinHexTest("Longer String."), noMarshal: true},
} }
// Generate extra test cases from convTests that implement duper. // Generate extra test cases from convTests that implement duper.