Rename v2 to v2alpha.
Any incremental development on this should be clearly labeled as unstable.
This commit is contained in:
140
v2alpha/soap/client/client.go
Normal file
140
v2alpha/soap/client/client.go
Normal file
@ -0,0 +1,140 @@
|
||||
// Package client provides a basic SOAP client.
|
||||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/huin/goupnp/v2alpha/soap/envelope"
|
||||
)
|
||||
|
||||
// HttpClient defines the interface required of an HTTP client. It is a subset of *http.Client.
|
||||
type HttpClient interface {
|
||||
Do(req *http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
// Option is the type for optional configuration of a Client.
|
||||
type Option func(*options)
|
||||
|
||||
// WithHTTPClient specifies an *http.Client to use instead of
|
||||
// http.DefaultClient.
|
||||
func WithHTTPClient(httpClient HttpClient) Option {
|
||||
return func(o *options) {
|
||||
o.httpClient = httpClient
|
||||
}
|
||||
}
|
||||
|
||||
type options struct {
|
||||
httpClient HttpClient
|
||||
}
|
||||
|
||||
// Client is a SOAP client, attached to a specific SOAP endpoint.
|
||||
// the zero value is not usable, use NewClient() to create an instance.
|
||||
type Client struct {
|
||||
httpClient HttpClient
|
||||
endpointURL string
|
||||
maxErrorResponseBytes int
|
||||
}
|
||||
|
||||
// New creates a new SOAP client, which will POST its requests to the
|
||||
// given URL.
|
||||
func New(endpointURL string, opts ...Option) *Client {
|
||||
co := options{
|
||||
httpClient: http.DefaultClient,
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(&co)
|
||||
}
|
||||
return &Client{
|
||||
httpClient: co.httpClient,
|
||||
endpointURL: endpointURL,
|
||||
}
|
||||
}
|
||||
|
||||
// PerformAction makes a SOAP request, with the given action values to provide
|
||||
// arguments (`args`) and capture the `reply` into. If `client` is nil, then
|
||||
// http.DefaultClient is used.
|
||||
func (c *Client) PerformAction(
|
||||
ctx context.Context,
|
||||
args, reply *envelope.Action,
|
||||
) error {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpointURL, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := SetRequestAction(req, args); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("SOAP request got HTTP %s (%d)",
|
||||
resp.Status, resp.StatusCode)
|
||||
}
|
||||
|
||||
return ParseResponseAction(resp, reply)
|
||||
}
|
||||
|
||||
// SetRequestAction updates fields in `req` with the given SOAP action.
|
||||
// Specifically it sets Body, ContentLength, Method, and the SOAPACTION and
|
||||
// CONTENT-TYPE headers.
|
||||
func SetRequestAction(req *http.Request, args *envelope.Action) error {
|
||||
buf := &bytes.Buffer{}
|
||||
err := envelope.Write(buf, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("encoding envelope: %w", err)
|
||||
}
|
||||
|
||||
req.Body = io.NopCloser(buf)
|
||||
req.ContentLength = int64(buf.Len())
|
||||
req.Method = http.MethodPost
|
||||
req.Header["SOAPACTION"] = []string{fmt.Sprintf(
|
||||
`"%s#%s"`, args.XMLName.Space, args.XMLName.Local)}
|
||||
req.Header["CONTENT-TYPE"] = []string{`text/xml; charset="utf-8"`}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseResponse extracts a parsed action from an HTTP response.
|
||||
// The caller is responsible for calling resp.Body.Close(), but this function
|
||||
// will consume the entire response body.
|
||||
func ParseResponseAction(resp *http.Response, reply *envelope.Action) error {
|
||||
if resp.Body == nil {
|
||||
return errors.New("missing response body")
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
if _, err := io.Copy(buf, resp.Body); err != nil {
|
||||
return fmt.Errorf("reading response body: %w", err)
|
||||
}
|
||||
|
||||
if err := envelope.Read(buf, reply); err != nil {
|
||||
if _, ok := err.(*envelope.Fault); ok {
|
||||
// Parsed cleanly, got SOAP fault.
|
||||
return err
|
||||
}
|
||||
// Parsing problem, provide some information for context.
|
||||
dispLen := buf.Len()
|
||||
truncMessage := ""
|
||||
if dispLen > 1024 {
|
||||
dispLen = 1024
|
||||
truncMessage = fmt.Sprintf("first %d bytes: ", dispLen)
|
||||
}
|
||||
return fmt.Errorf(
|
||||
"parsing response body (%s%q): %w",
|
||||
truncMessage, buf.Bytes()[:dispLen],
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
115
v2alpha/soap/client/client_test.go
Normal file
115
v2alpha/soap/client/client_test.go
Normal file
@ -0,0 +1,115 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/huin/goupnp/v2alpha/soap/envelope"
|
||||
)
|
||||
|
||||
type ActionArgs struct {
|
||||
Name string
|
||||
}
|
||||
type ActionReply struct {
|
||||
Greeting string
|
||||
}
|
||||
|
||||
type actionKey struct {
|
||||
endpointURL string
|
||||
action string
|
||||
}
|
||||
|
||||
var _ http.Handler = &fakeSoapServer{}
|
||||
|
||||
type fakeSoapServer struct {
|
||||
responses map[actionKey]*envelope.Action
|
||||
errors []error
|
||||
}
|
||||
|
||||
func (fss *fakeSoapServer) badRequest(w http.ResponseWriter, err error) {
|
||||
fss.errors = append(fss.errors, err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
_, _ = w.Write([]byte(err.Error()))
|
||||
}
|
||||
|
||||
func (fss *fakeSoapServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
fss.badRequest(w, fmt.Errorf("want POST, got %q", r.Method))
|
||||
return
|
||||
}
|
||||
actions := r.Header.Values("SOAPACTION")
|
||||
if len(actions) != 1 {
|
||||
fss.badRequest(w, fmt.Errorf("want exactly 1 SOAPACTION, got %d: %q", len(actions), actions))
|
||||
return
|
||||
}
|
||||
headerAction := actions[0]
|
||||
key := actionKey{
|
||||
endpointURL: r.URL.Path,
|
||||
action: headerAction,
|
||||
}
|
||||
response, ok := fss.responses[key]
|
||||
if !ok {
|
||||
fss.badRequest(w, fmt.Errorf("no response known for %#v", key))
|
||||
return
|
||||
}
|
||||
|
||||
reqArgs := &ActionArgs{}
|
||||
reqAction := envelope.Action{Args: reqArgs}
|
||||
if err := envelope.Read(r.Body, &reqAction); err != nil {
|
||||
fss.badRequest(w, fmt.Errorf("reading envelope from request: %w", err))
|
||||
return
|
||||
}
|
||||
envelopeAction := fmt.Sprintf("\"%s#%s\"", reqAction.XMLName.Space, reqAction.XMLName.Local)
|
||||
if envelopeAction != headerAction {
|
||||
fss.badRequest(w, fmt.Errorf("mismatch in header/envelope action: %q/%q", headerAction, envelopeAction))
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("CONTENT-TYPE", `text/xml; charset="utf-8"`)
|
||||
if err := envelope.Write(w, response); err != nil {
|
||||
fss.errors = append(fss.errors, fmt.Errorf("writing envelope: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPerformAction(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
t.Cleanup(cancel)
|
||||
|
||||
service := &fakeSoapServer{
|
||||
responses: map[actionKey]*envelope.Action{
|
||||
{"/endpointpath", "\"http://example.com/endpointns#Foo\""}: {
|
||||
Args: &ActionReply{Greeting: "Hello, World!"},
|
||||
},
|
||||
},
|
||||
}
|
||||
ts := httptest.NewServer(service)
|
||||
t.Cleanup(ts.Close)
|
||||
|
||||
c := New(ts.URL + "/endpointpath")
|
||||
|
||||
reqAction := &envelope.Action{
|
||||
XMLName: xml.Name{Space: "http://example.com/endpointns", Local: "Foo"},
|
||||
Args: &ActionArgs{
|
||||
Name: "World",
|
||||
},
|
||||
}
|
||||
reply := &ActionReply{}
|
||||
replyAction := &envelope.Action{Args: reply}
|
||||
|
||||
if err := c.PerformAction(ctx, reqAction, replyAction); err != nil {
|
||||
t.Errorf("got error: %v, want success", err)
|
||||
} else {
|
||||
if got, want := reply.Greeting, "Hello, World!"; got != want {
|
||||
t.Errorf("got %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
for _, err := range service.errors {
|
||||
t.Errorf("Service error: %v", err)
|
||||
}
|
||||
}
|
141
v2alpha/soap/envelope/envelope.go
Normal file
141
v2alpha/soap/envelope/envelope.go
Normal file
@ -0,0 +1,141 @@
|
||||
// Package envelope is responsible for encoding and decoding SOAP envelopes.
|
||||
package envelope
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// FaultDetail carries XML-encoded application-specific Fault details.
|
||||
type FaultDetail struct {
|
||||
Raw []byte `xml:",innerxml"`
|
||||
}
|
||||
|
||||
// Fault implements error, and contains SOAP fault information.
|
||||
type Fault struct {
|
||||
Code string `xml:"faultcode"`
|
||||
String string `xml:"faultstring"`
|
||||
Actor string `xml:"faultactor"`
|
||||
Detail FaultDetail `xml:"detail"`
|
||||
}
|
||||
|
||||
func (fe *Fault) Error() string {
|
||||
return fmt.Sprintf("SOAP fault code=%s: %s", fe.Code, fe.String)
|
||||
}
|
||||
|
||||
// Various "constant" bytes used in the written envelope.
|
||||
var (
|
||||
envOpen = []byte(xml.Header + `<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body>`)
|
||||
env1 = []byte(`<u:`)
|
||||
env2 = []byte(` xmlns:u="`)
|
||||
env3 = []byte(`">`)
|
||||
env4 = []byte(`</u:`)
|
||||
env5 = []byte(`>`)
|
||||
envClose = []byte(`</s:Body></s:Envelope>`)
|
||||
)
|
||||
|
||||
// Action wraps a SOAP action to be read or written as part of a SOAP envelope.
|
||||
type Action struct {
|
||||
// XMLName specifies the XML element namespace (URI) and name. Together
|
||||
// these identify the SOAP action.
|
||||
XMLName xml.Name
|
||||
// Args is an arbitrary struct containing fields for encoding or decoding
|
||||
// arguments. See https://pkg.go.dev/encoding/xml@go1.17.1#Marshal and
|
||||
// https://pkg.go.dev/encoding/xml@go1.17.1#Unmarshal for details on
|
||||
// annotating fields in the structure.
|
||||
Args interface{} `xml:",any"`
|
||||
}
|
||||
|
||||
// Write marshals a SOAP envelope to the writer. Errors can be from the writer
|
||||
// or XML encoding.
|
||||
func Write(w io.Writer, action *Action) error {
|
||||
// Experiments with one router have shown that it 500s for requests where
|
||||
// the outer default xmlns is set to the SOAP namespace, and then
|
||||
// reassigning the default namespace within that to the service namespace.
|
||||
// Most of the code in this function is hand-coding the outer XML to
|
||||
// workaround this.
|
||||
// Resolving https://github.com/golang/go/issues/9519 might remove the need
|
||||
// for this workaround.
|
||||
|
||||
_, err := w.Write(envOpen)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(env1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = xml.EscapeText(w, []byte(action.XMLName.Local))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(env2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = xml.EscapeText(w, []byte(action.XMLName.Space))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(env3)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
enc := xml.NewEncoder(w)
|
||||
err = enc.Encode(action.Args)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = enc.Flush()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(env4)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
xml.EscapeText(w, []byte(action.XMLName.Local))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(env5)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write(envClose)
|
||||
return err
|
||||
}
|
||||
|
||||
// Read unmarshals a SOAP envelope from the reader. Errors can either be from
|
||||
// the reader, XML decoding, or a *Fault.
|
||||
func Read(r io.Reader, action *Action) error {
|
||||
env := envelope{
|
||||
Body: body{
|
||||
Action: action,
|
||||
},
|
||||
}
|
||||
|
||||
dec := xml.NewDecoder(r)
|
||||
err := dec.Decode(&env)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if env.Body.Fault != nil {
|
||||
return env.Body.Fault
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type envelope struct {
|
||||
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
|
||||
EncodingStyle string `xml:"http://schemas.xmlsoap.org/soap/envelope/ encodingStyle,attr"`
|
||||
Body body `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
|
||||
}
|
||||
|
||||
type body struct {
|
||||
Fault *Fault `xml:"Fault"`
|
||||
Action *Action `xml:",any"`
|
||||
}
|
78
v2alpha/soap/envelope/envelope_test.go
Normal file
78
v2alpha/soap/envelope/envelope_test.go
Normal file
@ -0,0 +1,78 @@
|
||||
package envelope
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWriteRead(t *testing.T) {
|
||||
type Args struct {
|
||||
Foo string `xml:"foo"`
|
||||
Bar string `xml:"bar"`
|
||||
}
|
||||
|
||||
sendAction := &Action{
|
||||
XMLName: xml.Name{Space: "http://example.com/namespace", Local: "MyAction"},
|
||||
Args: &Args{
|
||||
Foo: "foo-1",
|
||||
Bar: "bar-2",
|
||||
},
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
err := Write(buf, sendAction)
|
||||
if err != nil {
|
||||
t.Errorf("Write want success, got err=%v", err)
|
||||
}
|
||||
|
||||
recvAction := &Action{Args: &Args{}}
|
||||
|
||||
err = Read(buf, recvAction)
|
||||
if err != nil {
|
||||
t.Errorf("Read want success, got err=%v", err)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(sendAction, recvAction) {
|
||||
t.Errorf("want recvAction=%+v, got %+v", sendAction, recvAction)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadFault(t *testing.T) {
|
||||
env := []byte(xml.Header + `
|
||||
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"
|
||||
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
|
||||
<s:Body>
|
||||
<s:Fault>
|
||||
<faultcode>dummy code</faultcode>
|
||||
<faultstring>dummy string</faultstring>
|
||||
<faultactor>dummy actor</faultactor>
|
||||
<detail>dummy detail</detail>
|
||||
</s:Fault>
|
||||
</s:Body>
|
||||
</s:Envelope>
|
||||
`)
|
||||
|
||||
type args struct{}
|
||||
|
||||
err := Read(bytes.NewBuffer(env), &Action{Args: &args{}})
|
||||
if err == nil {
|
||||
t.Fatal("want err != nil, got nil")
|
||||
}
|
||||
|
||||
gotFault, ok := err.(*Fault)
|
||||
if !ok {
|
||||
t.Fatalf("want *Fault, got %T", err)
|
||||
}
|
||||
|
||||
wantFault := &Fault{
|
||||
Code: "dummy code",
|
||||
String: "dummy string",
|
||||
Actor: "dummy actor",
|
||||
Detail: FaultDetail{Raw: []byte("dummy detail")},
|
||||
}
|
||||
if !reflect.DeepEqual(wantFault, gotFault) {
|
||||
t.Errorf("want %+v, got %+v", wantFault, gotFault)
|
||||
}
|
||||
}
|
1145
v2alpha/soap/types/types.go
Normal file
1145
v2alpha/soap/types/types.go
Normal file
File diff suppressed because it is too large
Load Diff
538
v2alpha/soap/types/types_test.go
Normal file
538
v2alpha/soap/types/types_test.go
Normal file
@ -0,0 +1,538 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var dummyLoc = time.FixedZone("DummyTZ", 6*3600)
|
||||
|
||||
func newFixed14_4Parts(t testing.TB, intPart int64, fracPart int16) *Fixed14_4 {
|
||||
t.Helper()
|
||||
v, err := Fixed14_4FromParts(intPart, fracPart)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return &v
|
||||
}
|
||||
|
||||
type isEqual func(got, want SOAPValue) bool
|
||||
|
||||
type typeTestCase struct {
|
||||
makeValue func() SOAPValue
|
||||
isEqual isEqual
|
||||
marshalTests []marshalCase
|
||||
marshalErrs []SOAPValue
|
||||
unmarshalTests []unmarshalCase
|
||||
unmarshalErrs []string
|
||||
}
|
||||
|
||||
type marshalCase struct {
|
||||
input SOAPValue
|
||||
want string
|
||||
}
|
||||
|
||||
type unmarshalCase struct {
|
||||
input string
|
||||
want SOAPValue
|
||||
}
|
||||
|
||||
func Test(t *testing.T) {
|
||||
badNumbers := []string{"", " ", "abc"}
|
||||
|
||||
typeTestCases := []typeTestCase{
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(UI1) },
|
||||
isEqual: func(got, want SOAPValue) bool { return *got.(*UI1) == *want.(*UI1) },
|
||||
marshalTests: []marshalCase{
|
||||
{NewUI1(0), "0"},
|
||||
{NewUI1(1), "1"},
|
||||
{NewUI1(255), "255"},
|
||||
},
|
||||
unmarshalErrs: append([]string{"-1", "256"}, badNumbers...),
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(UI2) },
|
||||
isEqual: func(got, want SOAPValue) bool { return *got.(*UI2) == *want.(*UI2) },
|
||||
marshalTests: []marshalCase{
|
||||
{NewUI2(0), "0"},
|
||||
{NewUI2(1), "1"},
|
||||
{NewUI2(65535), "65535"},
|
||||
},
|
||||
unmarshalErrs: append([]string{"-1", "65536"}, badNumbers...),
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(UI4) },
|
||||
isEqual: func(got, want SOAPValue) bool { return *got.(*UI4) == *want.(*UI4) },
|
||||
marshalTests: []marshalCase{
|
||||
{NewUI4(0), "0"},
|
||||
{NewUI4(1), "1"},
|
||||
{NewUI4(4294967295), "4294967295"},
|
||||
},
|
||||
unmarshalErrs: append([]string{"-1", "4294967296"}, badNumbers...),
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(UI8) },
|
||||
isEqual: func(got, want SOAPValue) bool { return *got.(*UI8) == *want.(*UI8) },
|
||||
marshalTests: []marshalCase{
|
||||
{NewUI8(0), "0"},
|
||||
{NewUI8(1), "1"},
|
||||
{NewUI8(18446744073709551615), "18446744073709551615"},
|
||||
},
|
||||
unmarshalErrs: append([]string{"-1", "18446744073709551616"}, badNumbers...),
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(I1) },
|
||||
isEqual: func(got, want SOAPValue) bool { return *got.(*I1) == *want.(*I1) },
|
||||
marshalTests: []marshalCase{
|
||||
{NewI1(0), "0"},
|
||||
{NewI1(1), "1"},
|
||||
{NewI1(-1), "-1"},
|
||||
{NewI1(127), "127"},
|
||||
{NewI1(-128), "-128"},
|
||||
},
|
||||
unmarshalErrs: append([]string{"-129", "128"}, badNumbers...),
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(I2) },
|
||||
isEqual: func(got, want SOAPValue) bool { return *got.(*I2) == *want.(*I2) },
|
||||
marshalTests: []marshalCase{
|
||||
{NewI2(0), "0"},
|
||||
{NewI2(1), "1"},
|
||||
{NewI2(-1), "-1"},
|
||||
{NewI2(32767), "32767"},
|
||||
{NewI2(-32768), "-32768"},
|
||||
},
|
||||
unmarshalErrs: append([]string{"-32769", "32768"}, badNumbers...),
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(I4) },
|
||||
isEqual: func(got, want SOAPValue) bool { return *got.(*I4) == *want.(*I4) },
|
||||
marshalTests: []marshalCase{
|
||||
{NewI4(0), "0"},
|
||||
{NewI4(1), "1"},
|
||||
{NewI4(-1), "-1"},
|
||||
{NewI4(2147483647), "2147483647"},
|
||||
{NewI4(-2147483648), "-2147483648"},
|
||||
},
|
||||
unmarshalErrs: append([]string{"-2147483649", "2147483648"}, badNumbers...),
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(I8) },
|
||||
isEqual: func(got, want SOAPValue) bool { return *got.(*I8) == *want.(*I8) },
|
||||
marshalTests: []marshalCase{
|
||||
{NewI8(0), "0"},
|
||||
{NewI8(1), "1"},
|
||||
{NewI8(-1), "-1"},
|
||||
{NewI8(9223372036854775807), "9223372036854775807"},
|
||||
{NewI8(-9223372036854775808), "-9223372036854775808"},
|
||||
},
|
||||
unmarshalErrs: append([]string{"-9223372036854775809", "9223372036854775808"}, badNumbers...),
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return &Fixed14_4{} },
|
||||
isEqual: func(got, want SOAPValue) bool {
|
||||
return got.(*Fixed14_4).Fractional == want.(*Fixed14_4).Fractional
|
||||
},
|
||||
marshalTests: []marshalCase{
|
||||
{newFixed14_4Parts(t, 0, 0), "0.0000"},
|
||||
{newFixed14_4Parts(t, 1, 2), "1.0002"},
|
||||
{newFixed14_4Parts(t, 1, 20), "1.0020"},
|
||||
{newFixed14_4Parts(t, 1, 200), "1.0200"},
|
||||
{newFixed14_4Parts(t, 1, 2000), "1.2000"},
|
||||
{newFixed14_4Parts(t, -1, -2), "-1.0002"},
|
||||
{newFixed14_4Parts(t, 1234, 5678), "1234.5678"},
|
||||
{newFixed14_4Parts(t, -1234, -5678), "-1234.5678"},
|
||||
{newFixed14_4Parts(t, 9999_99999_99999, 9999), "99999999999999.9999"},
|
||||
{newFixed14_4Parts(t, -9999_99999_99999, -9999), "-99999999999999.9999"},
|
||||
},
|
||||
unmarshalErrs: append([]string{
|
||||
"", ".", "0.00000000abc", "0.-5",
|
||||
}, badNumbers...),
|
||||
unmarshalTests: []unmarshalCase{
|
||||
{"010", newFixed14_4Parts(t, 10, 0)},
|
||||
{"0", newFixed14_4Parts(t, 0, 0)},
|
||||
{"0.", newFixed14_4Parts(t, 0, 0)},
|
||||
{"0.000005", newFixed14_4Parts(t, 0, 0)},
|
||||
{"1.2", newFixed14_4Parts(t, 1, 2000)},
|
||||
{"1.20", newFixed14_4Parts(t, 1, 2000)},
|
||||
{"1.200", newFixed14_4Parts(t, 1, 2000)},
|
||||
{"1.02", newFixed14_4Parts(t, 1, 200)},
|
||||
{"1.020", newFixed14_4Parts(t, 1, 200)},
|
||||
{"1.002", newFixed14_4Parts(t, 1, 20)},
|
||||
{"1.00200005", newFixed14_4Parts(t, 1, 20)},
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(Char) },
|
||||
isEqual: func(got, want SOAPValue) bool { return *got.(*Char) == *want.(*Char) },
|
||||
marshalTests: []marshalCase{
|
||||
{NewChar('a'), "a"},
|
||||
{NewChar('z'), "z"},
|
||||
{NewChar('\u1234'), "\u1234"},
|
||||
},
|
||||
marshalErrs: []SOAPValue{NewChar(0)},
|
||||
unmarshalErrs: []string{"aa", ""},
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(TimeOfDay) },
|
||||
isEqual: func(got, want SOAPValue) bool {
|
||||
return got.(*TimeOfDay).equal(*want.(*TimeOfDay))
|
||||
},
|
||||
marshalTests: []marshalCase{
|
||||
{&TimeOfDay{}, "00:00:00"},
|
||||
// ISO8601 special case
|
||||
{&TimeOfDay{Hour: 24}, "24:00:00"},
|
||||
},
|
||||
unmarshalTests: []unmarshalCase{
|
||||
{"000000", &TimeOfDay{}},
|
||||
},
|
||||
unmarshalErrs: []string{
|
||||
// Misformatted values:
|
||||
"foo 01:02:03", "foo\n01:02:03", "01:02:03 foo", "01:02:03\nfoo", "01:02:03Z",
|
||||
"01:02:03+01", "01:02:03+01:23", "01:02:03+0123", "01:02:03-01", "01:02:03-01:23",
|
||||
"01:02:03-0123",
|
||||
// Values out of range:
|
||||
"24:01:00",
|
||||
"24:00:01",
|
||||
"25:00:00",
|
||||
"00:60:00",
|
||||
"00:00:60",
|
||||
// Unexpected timezone component:
|
||||
"01:02:03Z",
|
||||
"01:02:03+01:23",
|
||||
"01:02:03+01:23",
|
||||
"01:02:03-01:23",
|
||||
"01:02:03-01:23",
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(TimeOfDayTZ) },
|
||||
isEqual: func(got, want SOAPValue) bool {
|
||||
return got.(*TimeOfDayTZ).equal(*want.(*TimeOfDayTZ))
|
||||
},
|
||||
marshalTests: []marshalCase{
|
||||
{&TimeOfDayTZ{}, "00:00:00"},
|
||||
// ISO8601 special case
|
||||
{&TimeOfDayTZ{TimeOfDay{24, 0, 0}, TZD{}}, "24:00:00"},
|
||||
{&TimeOfDayTZ{TimeOfDay{1, 2, 3}, TZDOffset(0)}, "01:02:03Z"},
|
||||
{&TimeOfDayTZ{TimeOfDay{1, 2, 3}, TZDOffset(3600 + 23*60)}, "01:02:03+01:23"},
|
||||
{&TimeOfDayTZ{TimeOfDay{1, 2, 3}, TZDOffset(-(3600 + 23*60))}, "01:02:03-01:23"},
|
||||
},
|
||||
unmarshalTests: []unmarshalCase{
|
||||
{"010203+01:23", &TimeOfDayTZ{TimeOfDay{1, 2, 3}, TZDOffset(3600 + 23*60)}},
|
||||
{"010203-01:23", &TimeOfDayTZ{TimeOfDay{1, 2, 3}, TZDOffset(-(3600 + 23*60))}},
|
||||
},
|
||||
unmarshalErrs: []string{
|
||||
// Misformatted values:
|
||||
"foo 01:02:03", "foo\n01:02:03", "01:02:03 foo", "01:02:03\nfoo",
|
||||
// Values out of range:
|
||||
"24:01:00",
|
||||
"24:00:01",
|
||||
"25:00:00",
|
||||
"00:60:00",
|
||||
"00:00:60",
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(Date) },
|
||||
isEqual: func(got, want SOAPValue) bool {
|
||||
a, b := got.(*Date), want.(*Date)
|
||||
return a.Year == b.Year && a.Month == b.Month && a.Day == b.Day
|
||||
},
|
||||
marshalTests: []marshalCase{
|
||||
{&Date{2013, 10, 8}, "2013-10-08"},
|
||||
},
|
||||
unmarshalTests: []unmarshalCase{
|
||||
{"20131008", &Date{2013, 10, 8}},
|
||||
},
|
||||
unmarshalErrs: []string{"", "-1"},
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(DateTime) },
|
||||
isEqual: func(got, want SOAPValue) bool {
|
||||
return got.(*DateTime).equal(*want.(*DateTime))
|
||||
},
|
||||
marshalTests: []marshalCase{
|
||||
{DateTimeFromTime(time.Date(2013, 10, 8, 0, 0, 0, 0, dummyLoc)).ptr(), "2013-10-08T00:00:00"},
|
||||
{DateTimeFromTime(time.Date(2013, 10, 8, 10, 30, 50, 0, dummyLoc)).ptr(), "2013-10-08T10:30:50"},
|
||||
},
|
||||
unmarshalTests: []unmarshalCase{
|
||||
{"20131008", DateTimeFromTime(time.Date(2013, 10, 8, 0, 0, 0, 0, dummyLoc)).ptr()},
|
||||
},
|
||||
unmarshalErrs: []string{
|
||||
// Unexpected timezone component.
|
||||
"2013-10-08T10:30:50+01:00",
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(DateTimeTZ) },
|
||||
isEqual: func(got, want SOAPValue) bool {
|
||||
return got.(*DateTimeTZ).equal(*want.(*DateTimeTZ))
|
||||
},
|
||||
marshalTests: []marshalCase{
|
||||
{DateTimeTZFromTime(time.Date(2013, 10, 8, 0, 0, 0, 0, dummyLoc)).ptr(), "2013-10-08T00:00:00+06:00"},
|
||||
{DateTimeTZFromTime(time.Date(2013, 10, 8, 10, 30, 50, 0, dummyLoc)).ptr(), "2013-10-08T10:30:50+06:00"},
|
||||
{DateTimeTZFromTime(time.Date(2013, 10, 8, 0, 0, 0, 0, time.UTC)).ptr(), "2013-10-08T00:00:00Z"},
|
||||
{DateTimeTZFromTime(time.Date(2013, 10, 8, 10, 30, 50, 0, time.UTC)).ptr(), "2013-10-08T10:30:50Z"},
|
||||
{DateTimeTZFromTime(time.Date(2013, 10, 8, 10, 30, 50, 0, time.FixedZone("+01:23", 3600+23*60))).ptr(), "2013-10-08T10:30:50+01:23"},
|
||||
{DateTimeTZFromTime(time.Date(2013, 10, 8, 10, 30, 50, 0, time.FixedZone("-01:23", -(3600+23*60)))).ptr(), "2013-10-08T10:30:50-01:23"},
|
||||
},
|
||||
unmarshalTests: []unmarshalCase{
|
||||
{"2013-10-08T10:30:50", &DateTimeTZ{Date{2013, 10, 8}, TimeOfDay{10, 30, 50}, TZD{}}},
|
||||
{"2013-10-08T10:30:50+00:00", DateTimeTZFromTime(time.Date(2013, 10, 8, 10, 30, 50, 0, time.UTC)).ptr()},
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(Boolean) },
|
||||
isEqual: func(got, want SOAPValue) bool {
|
||||
return *got.(*Boolean) == *want.(*Boolean)
|
||||
},
|
||||
marshalTests: []marshalCase{
|
||||
{NewBoolean(true), "1"},
|
||||
{NewBoolean(false), "0"},
|
||||
},
|
||||
unmarshalTests: []unmarshalCase{
|
||||
{"true", NewBoolean(true)},
|
||||
{"false", NewBoolean(false)},
|
||||
{"yes", NewBoolean(true)},
|
||||
{"no", NewBoolean(false)},
|
||||
},
|
||||
unmarshalErrs: []string{"", "2", "-1"},
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(BinBase64) },
|
||||
isEqual: func(got, want SOAPValue) bool {
|
||||
return bytes.Equal(*got.(*BinBase64), *want.(*BinBase64))
|
||||
},
|
||||
marshalTests: []marshalCase{
|
||||
{&BinBase64{}, ""},
|
||||
{NewBinBase64([]byte("a")), "YQ=="},
|
||||
{NewBinBase64([]byte("Longer String.")), "TG9uZ2VyIFN0cmluZy4="},
|
||||
{NewBinBase64([]byte("Longer Aligned.")), "TG9uZ2VyIEFsaWduZWQu"},
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(BinHex) },
|
||||
isEqual: func(got, want SOAPValue) bool {
|
||||
return bytes.Equal(*got.(*BinHex), *want.(*BinHex))
|
||||
},
|
||||
marshalTests: []marshalCase{
|
||||
{&BinHex{}, ""},
|
||||
{NewBinHex([]byte("a")), "61"},
|
||||
{NewBinHex([]byte("Longer String.")), "4c6f6e67657220537472696e672e"},
|
||||
},
|
||||
unmarshalTests: []unmarshalCase{
|
||||
{"4C6F6E67657220537472696E672E", NewBinHex([]byte("Longer String."))},
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
makeValue: func() SOAPValue { return new(URI) },
|
||||
isEqual: func(got, want SOAPValue) bool {
|
||||
return got.(*URI).ToURL().String() == want.(*URI).ToURL().String()
|
||||
},
|
||||
marshalTests: []marshalCase{
|
||||
{&URI{Scheme: "http", Host: "example.com", Path: "/path"}, "http://example.com/path"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range typeTestCases {
|
||||
tt := tt
|
||||
|
||||
// Convert marshalTests into additional round trip equivalent unmarshalTests
|
||||
for _, mt := range tt.marshalTests {
|
||||
tt.unmarshalTests = append(tt.unmarshalTests, unmarshalCase{
|
||||
input: mt.want,
|
||||
want: mt.input,
|
||||
})
|
||||
}
|
||||
|
||||
t.Run(fmt.Sprintf("%T", tt.makeValue()), func(t *testing.T) {
|
||||
for i, mt := range tt.marshalTests {
|
||||
mt := mt
|
||||
t.Run(fmt.Sprintf("marshalTest#%d_%v", i, mt.input), func(t *testing.T) {
|
||||
gotBytes, err := mt.input.MarshalText()
|
||||
if err != nil {
|
||||
t.Errorf("got unexpected error: %v", err)
|
||||
}
|
||||
got := string(gotBytes)
|
||||
if got != mt.want {
|
||||
t.Errorf("got %q, want: %q", got, mt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
for i, input := range tt.marshalErrs {
|
||||
input := input
|
||||
t.Run(fmt.Sprintf("marshalErr#%d_%v", i, input), func(t *testing.T) {
|
||||
got, err := input.MarshalText()
|
||||
if err == nil {
|
||||
t.Errorf("got %q, want error", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
for i, ut := range tt.unmarshalTests {
|
||||
ut := ut
|
||||
t.Run(fmt.Sprintf("unmarshalTest#%d_%q", i, ut.input), func(t *testing.T) {
|
||||
got := tt.makeValue()
|
||||
if err := got.UnmarshalText([]byte(ut.input)); err != nil {
|
||||
t.Errorf("got unexpected error: %v", err)
|
||||
}
|
||||
if !tt.isEqual(got, ut.want) {
|
||||
t.Errorf("got %v, want %v", got, ut.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
for i, input := range tt.unmarshalErrs {
|
||||
input := input
|
||||
t.Run(fmt.Sprintf("unmarshalErrs#%d_%q", i, input), func(t *testing.T) {
|
||||
got := tt.makeValue()
|
||||
if err := got.UnmarshalText([]byte(input)); err == nil {
|
||||
t.Errorf("got %v, want error", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFixed14_4(t *testing.T) {
|
||||
t.Run("Parts", func(t *testing.T) {
|
||||
tests := []struct {
|
||||
intPart int64
|
||||
fracPart int16
|
||||
fractional int64
|
||||
}{
|
||||
{0, 0, 0},
|
||||
{1, 2, 1_0002},
|
||||
{-1, -2, -1_0002},
|
||||
{1234, 5678, 1234_5678},
|
||||
{-1234, -5678, -1234_5678},
|
||||
{9999_99999_99999, 9999, 9999_99999_99999_9999},
|
||||
{-9999_99999_99999, -9999, -9999_99999_99999_9999},
|
||||
}
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
t.Run(fmt.Sprintf("FromParts(%d,%d)", test.intPart, test.fracPart), func(t *testing.T) {
|
||||
got, err := Fixed14_4FromParts(test.intPart, test.fracPart)
|
||||
if err != nil {
|
||||
t.Errorf("got error %v, want success", err)
|
||||
}
|
||||
if got.Fractional != test.fractional {
|
||||
t.Errorf("got %d, want %d", got.Fractional, test.fractional)
|
||||
}
|
||||
})
|
||||
t.Run(fmt.Sprintf("%d.Parts()", test.fractional), func(t *testing.T) {
|
||||
v, err := Fixed14_4FromFractional(test.fractional)
|
||||
if err != nil {
|
||||
t.Errorf("got error %v, want success", err)
|
||||
}
|
||||
gotIntPart, gotFracPart := v.Parts()
|
||||
if gotIntPart != test.intPart {
|
||||
t.Errorf("got %d, want %d", gotIntPart, test.intPart)
|
||||
}
|
||||
if gotFracPart != test.fracPart {
|
||||
t.Errorf("got %d, want %d", gotFracPart, test.fracPart)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
t.Run("Float", func(t *testing.T) {
|
||||
tests := []struct {
|
||||
flt float64
|
||||
fix *Fixed14_4
|
||||
}{
|
||||
{0, newFixed14_4Parts(t, 0, 0)},
|
||||
{1234.5678, newFixed14_4Parts(t, 1234, 5678)},
|
||||
{-1234.5678, newFixed14_4Parts(t, -1234, -5678)},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("To/FromFloat(%v)", test.fix), func(t *testing.T) {
|
||||
gotFix, err := Fixed14_4FromFloat(test.flt)
|
||||
if err != nil {
|
||||
t.Errorf("got error %v, want success", err)
|
||||
}
|
||||
if gotFix.Fractional != test.fix.Fractional {
|
||||
t.Errorf("got %v, want %v", gotFix, test.fix)
|
||||
}
|
||||
|
||||
gotFlt := test.fix.Float64()
|
||||
if math.Abs(gotFlt-test.flt) > 1e-6 {
|
||||
t.Errorf("got %f, want %f", gotFlt, test.flt)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
errTests := []float64{
|
||||
1e50,
|
||||
-1e50,
|
||||
1e14,
|
||||
-1e14,
|
||||
math.NaN(),
|
||||
math.Inf(1),
|
||||
math.Inf(-1),
|
||||
}
|
||||
for _, test := range errTests {
|
||||
t.Run(fmt.Sprintf("ErrorFromFloat(%f)", test), func(t *testing.T) {
|
||||
got, err := Fixed14_4FromFloat(test)
|
||||
if err == nil {
|
||||
t.Errorf("got success and %v, want error", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// methods only used in testing:
|
||||
|
||||
func (v TimeOfDay) equal(o TimeOfDay) bool {
|
||||
return v.Hour == o.Hour && v.Minute == o.Minute && v.Second == o.Second
|
||||
}
|
||||
|
||||
func (v TimeOfDayTZ) equal(o TimeOfDayTZ) bool {
|
||||
return v.TimeOfDay.equal(o.TimeOfDay) && v.TZ.equal(o.TZ)
|
||||
}
|
||||
|
||||
func (d Date) equal(o Date) bool {
|
||||
return d.Year == o.Year && d.Month == o.Month && d.Day == o.Day
|
||||
}
|
||||
|
||||
func (dtz DateTime) ptr() *DateTime { return &dtz }
|
||||
|
||||
func (dt DateTime) equal(o DateTime) bool {
|
||||
return dt.Date.equal(o.Date) && dt.TimeOfDay.equal(o.TimeOfDay)
|
||||
}
|
||||
|
||||
func (dtz DateTimeTZ) ptr() *DateTimeTZ { return &dtz }
|
||||
|
||||
func (dtz DateTimeTZ) equal(o DateTimeTZ) bool {
|
||||
return dtz.Date.equal(o.Date) &&
|
||||
dtz.TimeOfDay.equal(o.TimeOfDay) &&
|
||||
dtz.TZ.equal(o.TZ)
|
||||
}
|
||||
|
||||
func (tzd TZD) equal(o TZD) bool {
|
||||
return tzd.Offset == o.Offset && tzd.HasTZ == o.HasTZ
|
||||
}
|
Reference in New Issue
Block a user