First draft of v2/soap/envelope package.
This commit is contained in:
parent
8223a6b37c
commit
a16ad1252e
@ -7,17 +7,14 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FaultDetail carries XML-encoded application-specific Fault details.
|
|
||||||
type FaultDetail struct {
|
|
||||||
Raw []byte `xml:",innerxml"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fault implements error, and contains SOAP fault information.
|
// Fault implements error, and contains SOAP fault information.
|
||||||
type Fault struct {
|
type Fault struct {
|
||||||
Code string `xml:"faultcode"`
|
Code string `xml:"faultcode"`
|
||||||
String string `xml:"faultstring"`
|
String string `xml:"faultstring"`
|
||||||
Actor string `xml:"faultactor"`
|
Actor string `xml:"faultactor"`
|
||||||
Detail FaultDetail `xml:"detail"`
|
Detail struct {
|
||||||
|
Raw []byte `xml:",innerxml"`
|
||||||
|
} `xml:"detail"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fe *Fault) Error() string {
|
func (fe *Fault) Error() string {
|
||||||
@ -35,28 +32,14 @@ var (
|
|||||||
envClose = []byte(`</s:Body></s:Envelope>`)
|
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
|
// Write marshals a SOAP envelope to the writer. Errors can be from the writer
|
||||||
// or XML encoding.
|
// or XML encoding.
|
||||||
func Write(w io.Writer, action *Action) error {
|
func Write(w io.Writer, action *Action) error {
|
||||||
// Experiments with one router have shown that it 500s for requests where
|
// Experiments with one router have shown that it 500s for requests where
|
||||||
// the outer default xmlns is set to the SOAP namespace, and then
|
// the outer default xmlns is set to the SOAP namespace, and then
|
||||||
// reassigning the default namespace within that to the service namespace.
|
// reassigning the default namespace within that to the service namespace.
|
||||||
// Most of the code in this function is hand-coding the outer XML to
|
// Most of the code in this function is hand-coding the outer XML to work
|
||||||
// workaround this.
|
// around this.
|
||||||
// Resolving https://github.com/golang/go/issues/9519 might remove the need
|
|
||||||
// for this workaround.
|
|
||||||
|
|
||||||
_, err := w.Write(envOpen)
|
_, err := w.Write(envOpen)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -139,3 +122,8 @@ type body struct {
|
|||||||
Fault *Fault `xml:"Fault"`
|
Fault *Fault `xml:"Fault"`
|
||||||
Action *Action `xml:",any"`
|
Action *Action `xml:",any"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Action struct {
|
||||||
|
XMLName xml.Name
|
||||||
|
Args interface{} `xml:",any"`
|
||||||
|
}
|
||||||
|
@ -38,41 +38,3 @@ func TestWriteRead(t *testing.T) {
|
|||||||
t.Errorf("want recvAction=%+v, got %+v", 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user